Skip to content

Commit

Permalink
refresh button
Browse files Browse the repository at this point in the history
  • Loading branch information
twodayslate committed Oct 31, 2020
1 parent 182dcd9 commit f4931d8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
10 changes: 6 additions & 4 deletions claw.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = hottest.widgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C6L3992RFB;
INFOPLIST_FILE = hottest.widget/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -826,7 +826,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = hottest.widgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C6L3992RFB;
INFOPLIST_FILE = hottest.widget/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -967,7 +967,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = claw/claw.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"claw/Preview Content\"";
DEVELOPMENT_TEAM = C6L3992RFB;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -995,7 +995,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = claw/claw.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"claw/Preview Content\"";
DEVELOPMENT_TEAM = C6L3992RFB;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1105,6 +1105,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "opener-action/opener-action.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C6L3992RFB;
INFOPLIST_FILE = "opener-action/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
Expand All @@ -1128,6 +1129,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "opener-action/opener-action.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C6L3992RFB;
INFOPLIST_FILE = "opener-action/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>claw.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>deleteme.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -17,7 +17,7 @@
<key>hottest.widgetExtension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>open-action.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -29,6 +29,21 @@
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>share-extension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
</dict>
<key>test-extension copy.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>5</integer>
</dict>
<key>test-extension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
18 changes: 16 additions & 2 deletions claw/Stories/Hottest/HottestFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class HottestFetcher: ObservableObject {
static var cachedStories = [NewestStory]()

@Published var isLoadingMore = false
@Published var isReloading = false

init() {
load()
Expand All @@ -20,7 +21,14 @@ class HottestFetcher: ObservableObject {
private var session: URLSessionTask? = nil
private var moreSession: URLSessionTask? = nil

func load() {
func reload() {
self.session?.cancel()
self.page = 1
self.isReloading = true
self.load(completion: { _ in self.isReloading = false })
}

func load(completion: ((Error?)->Void)? = nil) {
let url = URL(string: "https://lobste.rs/hottest.json?page=\(self.page)")!

self.session = URLSession.shared.dataTask(with: url) {(data,response,error) in
Expand All @@ -31,20 +39,23 @@ class HottestFetcher: ObservableObject {
HottestFetcher.cachedStories = decodedLists
self.stories = decodedLists
self.page += 1
completion?(nil)
}
}else {
print("No Data")
completion?(nil) // todo: actually throw an error
}
} catch {
print ("Error \(error)")
completion?(error)
}
}
self.session?.resume()
}

var page: Int = 1

func more(_ story: NewestStory? = nil) {
func more(_ story: NewestStory? = nil, completion: ((Error?)->Void)? = nil) {
if self.stories.last == story && !isLoadingMore {
self.isLoadingMore = true
let url = URL(string: "https://lobste.rs/hottest.json?page=\(self.page)")!
Expand All @@ -62,12 +73,15 @@ class HottestFetcher: ObservableObject {
}
HottestFetcher.cachedStories = self.stories
self.page += 1
completion?(nil)
}
}else {
print("No Data")
completion?(nil) // todo: throw actual error
}
} catch {
print ("Error \(error)")
completion?(error)
}
DispatchQueue.main.async {
self.isLoadingMore = false
Expand Down
8 changes: 7 additions & 1 deletion claw/Stories/Hottest/HottestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ struct HottestView: View {
}
}
}
}
}.navigationBarItems(trailing: Button(action: { hottest.reload() }, label: {
if self.hottest.isReloading {
ProgressView().progressViewStyle(CircularProgressViewStyle())
} else {
Image(systemName: "arrow.clockwise")
}
}))
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions claw/Stories/Newest/NewestFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class NewestFetcher: ObservableObject {
@Published var stories = NewestFetcher.cachedStories
static var cachedStories = [NewestStory]()
@Published var isLoadingMore = false
@Published var isReloading = false
var page: Int = 1

init() {
Expand All @@ -26,7 +27,17 @@ class NewestFetcher: ObservableObject {
private var session: URLSessionTask? = nil
private var moreSession: URLSessionTask? = nil

func load() {
func reload(completion: ((Error?)->Void)? = nil) {
self.session?.cancel()
self.isReloading = true
self.page = 1
self.load(completion: { error in
self.isReloading = false
completion?(error)
})
}

func load(completion: ((Error?)->Void)? = nil) {
let url = URL(string: "https://lobste.rs/newest.json?page=\(self.page)")!
self.session = URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
Expand All @@ -36,20 +47,23 @@ class NewestFetcher: ObservableObject {
NewestFetcher.cachedStories = decodedLists
self.stories = decodedLists
self.page += 1
completion?(nil)
}
}else {
print("No Data")
completion?(nil) // todo: throw error
}
} catch {
print ("Error \(error)")
completion?(error)
}
}
self.session?.resume()
}

func more(_ story: NewestStory? = nil) {
func more(_ story: NewestStory? = nil, completion: ((Error?)->Void)? = nil) {
if self.stories.last == story && !isLoadingMore {
let url = URL(string: "https://lobste.rs/hottest.json?page=\(self.page)")!
let url = URL(string: "https://lobste.rs/newest.json?page=\(self.page)")!
self.isLoadingMore = true

self.moreSession = URLSession.shared.dataTask(with: url) { (data,response,error) in
Expand All @@ -65,12 +79,15 @@ class NewestFetcher: ObservableObject {
}
NewestFetcher.cachedStories = self.stories
self.page += 1
completion?(nil)
}
}else {
print("No Data")
completion?(nil) // todo: actually do error
}
} catch {
print ("Error \(error)")
completion?(error)
}
DispatchQueue.main.async {
self.isLoadingMore = false
Expand Down
9 changes: 8 additions & 1 deletion claw/Stories/Newest/NewestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct NewestView: View {
@EnvironmentObject var settings: Settings
@Environment(\.didReselect) var didReselect
@State var isVisible = false

var body: some View {
ScrollViewReader { scrollProxy in
ScrollView {
Expand Down Expand Up @@ -45,7 +46,13 @@ struct NewestView: View {
}

}
}
}.navigationBarItems(trailing: Button(action: { newest.reload() }, label: {
if self.newest.isReloading {
ProgressView().progressViewStyle(CircularProgressViewStyle())
} else {
Image(systemName: "arrow.clockwise")
}
}))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion opener-action/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
Expand Down

0 comments on commit f4931d8

Please sign in to comment.