Merge pull request #18 from rock-n-code/improvements/browse-pull-to-refresh

Improvement: Pull to refresh in the Browse view
This commit is contained in:
Javier Cicchelli 2022-12-18 15:04:27 +01:00 committed by GitHub
commit 011401a855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 24 deletions

View File

@ -10,18 +10,28 @@ import SwiftUI
struct LoadingView: View { struct LoadingView: View {
var body: some View { var body: some View {
VStack(spacing: 24) { VStack {
ProgressView() Spacer()
.controlSize(.large)
.tint(.red)
Text( VStack(spacing: 24) {
"loading.loading_data.text", ProgressView()
bundle: .module .controlSize(.large)
) .tint(.red)
.font(.body)
.fontWeight(.semibold) Text(
"loading.loading_data.text",
bundle: .module
)
.font(.body)
.fontWeight(.semibold)
}
.padding(24)
.background(Material.ultraThin)
.cornerRadius(16)
Spacer()
} }
.background(Color.clear)
.ignoresSafeArea() .ignoresSafeArea()
} }
} }

View File

@ -61,6 +61,11 @@ public struct BrowseView: View {
showProfile: showProfile showProfile: showProfile
) )
} }
.overlay {
if status == .loading {
LoadingView()
}
}
.sheet(item: $showSheet) { sheet in .sheet(item: $showSheet) { sheet in
switch sheet { switch sheet {
case let .upload(id): case let .upload(id):
@ -80,16 +85,12 @@ public struct BrowseView: View {
isPresenting: $showCreateFolder, isPresenting: $showCreateFolder,
id: folder.id id: folder.id
) { ) {
Task { Task { await loadItems() }
await loadItems()
}
} }
.delete(item: $itemToDelete) { .delete(item: $itemToDelete) {
Task { Task { await loadItems() }
await loadItems()
}
} }
.task(id: folder) { .task {
await loadItems() await loadItems()
} }
} }
@ -110,9 +111,8 @@ private extension BrowseView {
) )
case .notSupported: case .notSupported:
EmptyView() EmptyView()
case .loading: case .loading,
LoadingView() .loaded:
case .loaded:
List(items, id: \.id) { item in List(items, id: \.id) { item in
switch item { switch item {
case is Folder: case is Folder:
@ -124,6 +124,9 @@ private extension BrowseView {
} }
} }
.listStyle(.inset) .listStyle(.inset)
.refreshable {
Task { await loadItems() }
}
case .empty: case .empty:
MessageView( MessageView(
type: .empty, type: .empty,
@ -133,9 +136,7 @@ private extension BrowseView {
) )
case .error: case .error:
MessageView(type: .error) { MessageView(type: .error) {
Task { Task { await loadItems() }
await loadItems()
}
} }
} }
} }
@ -212,6 +213,9 @@ private extension BrowseView {
password: account.password password: account.password
) )
// Added some throttle (1 second) not to hide the loading indicator right away.
try await Task.sleep(nanoseconds: .Constants.secondInNanoseconds)
if loadedItems.isEmpty { if loadedItems.isEmpty {
status = .empty status = .empty
} else { } else {
@ -235,13 +239,21 @@ private extension BrowseView {
} }
} }
// MARK: - UInt64+Constants
private extension UInt64 {
enum Constants {
static let secondInNanoseconds = UInt64(1 * Double(NSEC_PER_SEC))
}
}
// MARK: - Previews // MARK: - Previews
struct BrowseView_Previews: PreviewProvider { struct BrowseView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
NavigationView { NavigationView {
BrowseView(folder: .init( BrowseView(folder: .init(
id: UUID().uuidString, id: "1234567890",
name: "Some folder name" name: "Some folder name"
)) { )) {
// show profile closure. // show profile closure.