diff --git a/claw/ActiveSheet.swift b/claw/ActiveSheet.swift index 4482ca8..4f14521 100644 --- a/claw/ActiveSheet.swift +++ b/claw/ActiveSheet.swift @@ -1,6 +1,6 @@ import Foundation -enum ActiveSheet: Identifiable { +enum ActiveSheet: Identifiable, Equatable { case share(URL) case safari(URL) case story(id:String) diff --git a/claw/ContentView.swift b/claw/ContentView.swift index 1447f50..fefadea 100644 --- a/claw/ContentView.swift +++ b/claw/ContentView.swift @@ -86,8 +86,8 @@ struct ContentView: View { @Environment(\.sizeCategory) var sizeCategory - @ObservedObject var observableSheet = ObservableActiveSheet() - @ObservedObject var urlToOpen = ObservableURL() + @StateObject var observableSheet = ObservableActiveSheet() + @StateObject var urlToOpen = ObservableURL() var body: some View { let selection = Binding(get: { self._selection }, @@ -125,7 +125,8 @@ struct ContentView: View { Image(systemName: "gear") Text("Settings") }).environmentObject(settings).environment(\.managedObjectContext, viewContext) - }.environment(\.didReselect, didReselect.eraseToAnyPublisher()).accentColor(settings.accentColor).font(Font(.body, sizeModifier: CGFloat(settings.textSizeModifier))).onOpenURL(perform: { url in + }.environment(\.didReselect, didReselect.eraseToAnyPublisher()) + .onOpenURL(perform: { url in let _ = print(url) let openAction = { if url.host == "open", let comps = URLComponents(url: url, resolvingAgainstBaseURL: false), let items = comps.queryItems, let item = items.first, item.name == "url", let itemValue = item.value, let lobsters_url = URL(string: itemValue), lobsters_url.host == "lobste.rs" { @@ -160,20 +161,20 @@ struct ContentView: View { openAction() } }) - .sheet(item: self.observableSheet.bindingSheet, content: { item in + .sheet(item: self.$observableSheet.sheet, content: { item in switch item { case .story(let id): EZPanel{ - StoryView(id) - } + StoryView(id).id(id) + }.id(id) .environmentObject(urlToOpen) .environmentObject(settings) .environmentObject(self.observableSheet) .environment(\.managedObjectContext, viewContext) case .user(let username): EZPanel{ - UserView(username) - } + UserView(username).id(username) + }.id(username) .environmentObject(urlToOpen) .environmentObject(settings) .environmentObject(self.observableSheet) @@ -205,6 +206,8 @@ struct ContentView: View { .environment(\.managedObjectContext, viewContext) .environmentObject(self.observableSheet) .environmentObject(urlToOpen) + .accentColor(settings.accentColor) + .font(Font(.body, sizeModifier: CGFloat(settings.textSizeModifier))) } } diff --git a/claw/ObservableURL.swift b/claw/ObservableURL.swift index 73c0ab8..d9fa828 100644 --- a/claw/ObservableURL.swift +++ b/claw/ObservableURL.swift @@ -3,12 +3,4 @@ import SwiftUI public class ObservableURL: ObservableObject { @Published var url: URL? = nil - - var bindingUrl: Binding { - return Binding(get: { - return self.url - }, set: { newValue in - self.url = newValue - }) - } } diff --git a/claw/Settings/SettingsLinkView.swift b/claw/Settings/SettingsLinkView.swift index 24a8470..b2aab3e 100644 --- a/claw/Settings/SettingsLinkView.swift +++ b/claw/Settings/SettingsLinkView.swift @@ -23,7 +23,7 @@ struct SettingsLinkView: View { ZZLabel(iconBackgroundColor: iconColor, iconColor: .white, systemImage: systemImage, image: image, text: text) }) // this is necessary until multiple sheets can be displayed at one time. See #22 - .fullScreenCover(item: urlToOpen.bindingUrl, content: { url in + .safariView(item: $urlToOpen.url, content: { url in SafariView( url: url, configuration: SafariView.Configuration( diff --git a/claw/Stories/Story.swift b/claw/Stories/Story.swift index 4b24a8a..6b20054 100644 --- a/claw/Stories/Story.swift +++ b/claw/Stories/Story.swift @@ -12,6 +12,7 @@ struct Story: GenericStory, Codable, Hashable, Identifiable { var id: String { return short_id } + var short_id: String var short_id_url: String var created_at: String @@ -85,9 +86,9 @@ struct Comment: Codable, Hashable, Identifiable { class StoryFetcher: ObservableObject { @Published var story: Story? = nil - var short_id: String + public var short_id: String? = nil - init(_ short_id: String) { + init(_ short_id: String? = nil) { self.short_id = short_id } @@ -104,13 +105,25 @@ class StoryFetcher: ObservableObject { self.isReloading = true self.load() } + + func loadIfEmpty() { + if let _ = self.story { + return + } + self.load() + } func load() { - if let cachedStory = StoryFetcher.cachedStories.first(where: {$0.short_id == self.short_id}) { + guard let short_id = self.short_id else { + return + } + if let cachedStory = StoryFetcher.cachedStories.first(where: {$0.short_id == short_id}) { self.story = cachedStory } let url = URL(string: "https://lobste.rs/s/\(short_id).json")! - + + self.session?.cancel() + self.session = URLSession.shared.dataTask(with: url) {(data,response,error) in DispatchQueue.main.async { self.isReloading = false @@ -120,7 +133,7 @@ class StoryFetcher: ObservableObject { let decodedLists = try JSONDecoder().decode(Story.self, from: d) DispatchQueue.main.async { self.story = decodedLists - StoryFetcher.cachedStories.removeAll(where: {$0.short_id == self.short_id}) + StoryFetcher.cachedStories.removeAll(where: {$0.short_id == short_id}) StoryFetcher.cachedStories.append(decodedLists) if StoryFetcher.cachedStories.count > 10 { StoryFetcher.cachedStories.removeFirst() diff --git a/claw/Stories/StoryListCellView.swift b/claw/Stories/StoryListCellView.swift index 141a073..946bd2e 100644 --- a/claw/Stories/StoryListCellView.swift +++ b/claw/Stories/StoryListCellView.swift @@ -3,14 +3,6 @@ import SwiftUI public class ObservableActiveSheet: ObservableObject { @Published var sheet: ActiveSheet? = nil - - var bindingSheet: Binding { - return Binding(get: { - return self.sheet - }, set: { newValue in - self.sheet = newValue - }) - } } struct StoryListCellView: View { diff --git a/claw/Stories/StoryView.swift b/claw/Stories/StoryView.swift index 25a7748..b767648 100644 --- a/claw/Stories/StoryView.swift +++ b/claw/Stories/StoryView.swift @@ -6,21 +6,19 @@ struct StoryView: View { var from_newest: NewestStory? @Environment(\.didReselect) var didReselect @Environment(\.presentationMode) var presentationMode: Binding - @ObservedObject var story: StoryFetcher + @StateObject var story = StoryFetcher() init(_ short_id: String) { self.short_id = short_id - self.story = StoryFetcher(short_id) } init(_ story: NewestStory) { self.from_newest = story self.short_id = story.short_id - self.story = StoryFetcher(short_id) } var title: String { - if let story = story.story{ + if let story = self.story.story{ if story.comment_count == 1 { return "1 comment" } @@ -50,7 +48,7 @@ struct StoryView: View { @EnvironmentObject var urlToOpen: ObservableURL @EnvironmentObject var observableSheet: ObservableActiveSheet - + var body: some View { ScrollViewReader { scrollReader in @@ -140,12 +138,13 @@ struct StoryView: View { } } }.onAppear(perform: { + self.story.short_id = self.short_id self.story.load() let contains = viewedItems.contains { element in element.short_id == story.short_id && element.isStory } if !contains { - viewContext.insert(ViewedItem(context: viewContext, short_id: story.short_id, isStory: true, isComment: false)) + viewContext.insert(ViewedItem(context: viewContext, short_id: story.short_id!, isStory: true, isComment: false)) try? viewContext.save() } }).navigationBarItems(trailing: Button(action: {self.story.reload() }, label: { @@ -155,16 +154,19 @@ struct StoryView: View { Image(systemName: "arrow.clockwise") } })) - } - // this is necessary until multiple sheets can be displayed at one time. See #22 - .fullScreenCover(item: urlToOpen.bindingUrl, content: { url in + } // scrollviewreader + .safariView(item: $urlToOpen.url, + content: + { url in SafariView( url: url, configuration: SafariView.Configuration( entersReaderIfAvailable: settings.readerModeEnabled, barCollapsingEnabled: true ) - ).preferredControlAccentColor(settings.accentColor).dismissButtonStyle(.close) + ) + .preferredControlAccentColor(settings.accentColor) + .dismissButtonStyle(.close) }) } } diff --git a/claw/User/UserView.swift b/claw/User/UserView.swift index 4d2321b..cc09ff3 100644 --- a/claw/User/UserView.swift +++ b/claw/User/UserView.swift @@ -110,7 +110,7 @@ struct UserView: View { self.userFetcher.load() }) // this is necessary until multiple sheets can be displayed at one time. See #22 - .fullScreenCover(item: urlToOpen.bindingUrl, content: { url in + .safariView(item: $urlToOpen.url, content: { url in SafariView( url: url, configuration: SafariView.Configuration(