Skip to content

Commit

Permalink
Merge pull request #45 from twodayslate/safari-swipe
Browse files Browse the repository at this point in the history
Allow swipe back for in-app safari
  • Loading branch information
twodayslate authored Sep 14, 2021
2 parents 4877ddb + 8c62db7 commit d5db27c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion claw/ActiveSheet.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

enum ActiveSheet: Identifiable {
enum ActiveSheet: Identifiable, Equatable {
case share(URL)
case safari(URL)
case story(id:String)
Expand Down
19 changes: 11 additions & 8 deletions claw/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)))
}
}

Expand Down
8 changes: 0 additions & 8 deletions claw/ObservableURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,4 @@ import SwiftUI

public class ObservableURL: ObservableObject {
@Published var url: URL? = nil

var bindingUrl: Binding<URL?> {
return Binding(get: {
return self.url
}, set: { newValue in
self.url = newValue
})
}
}
2 changes: 1 addition & 1 deletion claw/Settings/SettingsLinkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 18 additions & 5 deletions claw/Stories/Story.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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()
Expand Down
8 changes: 0 additions & 8 deletions claw/Stories/StoryListCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import SwiftUI

public class ObservableActiveSheet: ObservableObject {
@Published var sheet: ActiveSheet? = nil

var bindingSheet: Binding<ActiveSheet?> {
return Binding(get: {
return self.sheet
}, set: { newValue in
self.sheet = newValue
})
}
}

struct StoryListCellView: View {
Expand Down
22 changes: 12 additions & 10 deletions claw/Stories/StoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ struct StoryView: View {
var from_newest: NewestStory?
@Environment(\.didReselect) var didReselect
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@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"
}
Expand Down Expand Up @@ -50,7 +48,7 @@ struct StoryView: View {

@EnvironmentObject var urlToOpen: ObservableURL
@EnvironmentObject var observableSheet: ObservableActiveSheet

var body: some View {
ScrollViewReader { scrollReader in

Expand Down Expand Up @@ -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: {
Expand All @@ -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)
})
}
}
2 changes: 1 addition & 1 deletion claw/User/UserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit d5db27c

Please sign in to comment.