Skip to content

Commit

Permalink
Add widget support (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-e authored Jan 29, 2025
1 parent 53e8c55 commit c2420a2
Show file tree
Hide file tree
Showing 15 changed files with 684 additions and 48 deletions.
199 changes: 196 additions & 3 deletions ios/HackerNews.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

23 changes: 0 additions & 23 deletions ios/HackerNews/AppViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,6 @@ import Foundation
import SwiftData
import SwiftUI

enum FeedType: CaseIterable {
case top
case new
case best
case ask
case show

var title: String {
switch self {
case .top:
return "Top"
case .new:
return "New"
case .best:
return "Best"
case .ask:
return "Ask"
case .show:
return "Show"
}
}
}

struct StoryContent {
var id: Int64
var title: String
Expand Down
29 changes: 29 additions & 0 deletions ios/HackerNews/Feed/FeedType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// FeedType.swift
// HackerNews
//
// Created by Trevor Elkins on 1/28/25.
//

enum FeedType: CaseIterable {
case top
case new
case best
case ask
case show

var title: String {
switch self {
case .top:
return "Top"
case .new:
return "New"
case .best:
return "Best"
case .ask:
return "Ask"
case .show:
return "Show"
}
}
}
33 changes: 29 additions & 4 deletions ios/HackerNews/HNApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import SwiftUI

@main
struct Hacker_NewsApp: App {
@State private var appModel = AppViewModel(bookmarkStore: LiveBookmarksDataStore.shared)
@State private var appModel = AppViewModel(
bookmarkStore: LiveBookmarksDataStore.shared)
@State private var theme = Theme()

init() {
UINavigationBar.appearance().backgroundColor = .clear
UICollectionView.appearance().backgroundColor = .clear

EMGReaper.sharedInstance().start(withAPIKey: "f77fb081-cfc2-4d15-acb5-18bad59c9376")
EMGReaper.sharedInstance().start(
withAPIKey: "f77fb081-cfc2-4d15-acb5-18bad59c9376")

SentrySDK.start { options in
options.dsn =
Expand All @@ -37,13 +39,16 @@ struct Hacker_NewsApp: App {

ContentView(model: $appModel)
}
.navigationDestination(for: AppViewModel.AppNavigation.self) { appNavigation in
.navigationDestination(for: AppViewModel.AppNavigation.self) {
appNavigation in
switch appNavigation {
case .webLink(let url, let title):
WebViewContainer(url: url, title: title)
.ignoresSafeArea()
case .storyComments(let story):
let commentModel = CommentsViewModel(story: story, auth: appModel.authState) {
let commentModel = CommentsViewModel(
story: story, auth: appModel.authState
) {
destination in
switch destination {
case .back:
Expand All @@ -65,6 +70,26 @@ struct Hacker_NewsApp: App {
}
}
.environment(theme)
.onOpenURL { url in
handleDeepLink(url)
}
}
}

private func handleDeepLink(_ url: URL) {
guard url.scheme == "hackernews",
url.host == "story",
let storyId = Int64(url.lastPathComponent)
else {
return
}

Task {
let stories = await HNApi().fetchPage(page: Page(ids: [storyId]))
if let story = stories.first {
appModel.navigationPath
.append(AppViewModel.AppNavigation.storyComments(story: story))
}
}
}
}
11 changes: 11 additions & 0 deletions ios/HackerNews/Hacker-News-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
<false/>
<key>LSMinimumSystemVersion</key>
<string>14.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>hackernews</string>
</array>
<key>CFBundleURLName</key>
<string>com.emerge.hackernews</string>
</dict>
</array>
<key>UIAppFonts</key>
<array>
<string>ibm_plex_sans_regular.ttf</string>
Expand Down
33 changes: 25 additions & 8 deletions ios/HackerNews/Utils/Theme.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import SwiftUI
import WidgetKit

enum ThemeContext {
case app
case widget
}

@MainActor
@Observable
Expand All @@ -13,9 +19,12 @@ final class Theme {
static let maxCommentFontSize: Double = 18

static let defaultTitleFontSize: Double = 16
static let defaultWidgetTitleFontSize: Double = 13
static let minTitleFontSize: Double = 14
static let maxTitleFontSize: Double = 22

private let context: ThemeContext

var useSystemFont: Bool {
didSet {
UserDefaults.standard.set(useSystemFont, forKey: Self.useSystemFontKey)
Expand All @@ -30,11 +39,13 @@ final class Theme {

var commentFontSize: Double {
didSet {
let clamped = commentFontSize.clamped(to: Self.minCommentFontSize...Self.maxCommentFontSize)
let clamped = commentFontSize.clamped(
to: Self.minCommentFontSize...Self.maxCommentFontSize)
if clamped != commentFontSize {
commentFontSize = clamped
}
UserDefaults.standard.set(commentFontSize, forKey: Self.commentFontSizeKey)
UserDefaults.standard.set(
commentFontSize, forKey: Self.commentFontSizeKey)
}
}

Expand All @@ -44,16 +55,18 @@ final class Theme {

var titleFontSize: Double {
didSet {
let clamped = titleFontSize.clamped(to: Self.minTitleFontSize...Self.maxTitleFontSize)
let clamped = titleFontSize.clamped(
to: Self.minTitleFontSize...Self.maxTitleFontSize)
if clamped != titleFontSize {
titleFontSize = clamped
}
UserDefaults.standard.set(titleFontSize, forKey: Self.titleFontSizeKey)
}
}

var titleFont: Font {
userMonoFont(size: titleFontSize, weight: .bold)
let size = context == .app ? titleFontSize : Self.defaultWidgetTitleFontSize
return userMonoFont(size: size, weight: .bold)
}

var commentTextFont: Font {
Expand Down Expand Up @@ -103,10 +116,14 @@ final class Theme {
}
}

init() {
self.useSystemFont = UserDefaults.standard.object(forKey: Self.useSystemFontKey) as? Bool ?? false
init(context: ThemeContext = .app) {
self.context = context
self.useSystemFont =
UserDefaults.standard.object(forKey: Self.useSystemFontKey) as? Bool
?? false
self.useMonospaced =
UserDefaults.standard.object(forKey: Self.useMonospacedKey) as? Bool ?? true
UserDefaults.standard.object(forKey: Self.useMonospacedKey) as? Bool
?? true
self.commentFontSize =
UserDefaults.standard.object(forKey: Self.commentFontSizeKey) as? Double
?? Self.defaultCommentFontSize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "tinted"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions ios/HackerNewsHomeWidget/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit c2420a2

Please sign in to comment.