Open twodayslate opened 3 years ago
diff --git a/claw/ContentView.swift b/claw/ContentView.swift
index e5f4bdd..9ae0190 100644
--- a/claw/ContentView.swift
+++ b/claw/ContentView.swift
@@ -125,7 +125,11 @@ 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())
+ .accentColor(settings.accentColor)
+ .font(Font(.body, sizeModifier: CGFloat(settings.textSizeModifier)))
+ .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" {
@@ -147,9 +151,13 @@ struct ContentView: View {
}
if url.host == "open" && (self.observableSheet.sheet != nil || self.urlToOpen.url != nil) {
- UIApplication.shared.windows.first?.rootViewController?.presentedViewController?.dismiss(animated: true, completion: {
- openAction()
- })
+ DispatchQueue.main.async {
+ UIApplication.shared.windows.first?.rootViewController?.presentedViewController?.dismiss(animated: true, completion: {
+ DispatchQueue.main.async {
+ openAction()
+ }
+ })
+ }
} else {
openAction()
}
diff --git a/claw/Stories/Story.swift b/claw/Stories/Story.swift
index 222041f..d1bdb27 100644
--- a/claw/Stories/Story.swift
+++ b/claw/Stories/Story.swift
@@ -89,7 +89,6 @@ class StoryFetcher: ObservableObject {
init(_ short_id: String) {
self.short_id = short_id
- load()
}
deinit {
@@ -107,9 +106,18 @@ class StoryFetcher: ObservableObject {
}
}
let url = URL(string: "https://lobste.rs/s/\(short_id).json")!
-
+
self.session = URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
+ // rate limited. see #28
+ if (response as? HTTPURLResponse)?.statusCode == 429 {
+ print("We are being rate limited!")
+ if let ratelimit = (response as? HTTPURLResponse)?.allHeaderFields["ratelimit-limit"] as? String,
+ let wait = UInt32(ratelimit) {
+ sleep(wait)
+ return
+ }
+ }
if let d = data {
let decodedLists = try JSONDecoder().decode(Story.self, from: d)
DispatchQueue.main.async {
diff --git a/claw/Stories/StoryCell.swift b/claw/Stories/StoryCell.swift
index 6e8d921..1c44b97 100644
--- a/claw/Stories/StoryCell.swift
+++ b/claw/Stories/StoryCell.swift
@@ -59,6 +59,9 @@ struct StoryCell: View {
}
}
}.opacity(contains ? 0.9 : 1.0)
+ .onAppear(perform: {
+ let _ = StoryFetcher(self.story.short_id)
+ })
}
}
diff --git a/claw/Stories/StoryView.swift b/claw/Stories/StoryView.swift
index b5a2f97..8355fee 100644
--- a/claw/Stories/StoryView.swift
+++ b/claw/Stories/StoryView.swift
@@ -108,6 +108,7 @@ struct StoryView: View {
}
}
}.onAppear(perform: {
+ self.story.load()
let contains = viewedItems.contains { element in
element.short_id == story.short_id && element.isStory
}
Might be good to have a static story fetcher with a queue of stories to load. Once the story is visible then that one goes to the front of the queue. This enables preloading without having two many requests at once.
Perhaps have an advance option to specify your story cache limit.
See https://github.com/lobsters/lobsters/blob/master/config/initializers/rack_attack.rb
I've been getting throttled recently (probably due to #30) and a 1s+ wait may be good.