diff --git a/BeReal/UI/Views/ContentView.swift b/BeReal/UI/Views/ContentView.swift index 8a840d9..07830f8 100644 --- a/BeReal/UI/Views/ContentView.swift +++ b/BeReal/UI/Views/ContentView.swift @@ -32,17 +32,12 @@ struct ContentView: View { // MARK: Body var body: some View { - NavigationView { - BrowseView(folder: .init( - id: user?.rootFolder.id, - name: user?.rootFolder.name - )) { - // ... - } uploadFile: { - // ... - } showProfile: { - showSheet = .profile - } + Container(user: $user) { + // TODO: create a new folder + } uploadFile: { + // TODO: upload a new file + } showProfile: { + showSheet = .profile } .sheet(item: $showSheet) { sheet in switch sheet { @@ -65,6 +60,41 @@ struct ContentView: View { } +// MARK: - Views + +private extension ContentView { + struct Container: View { + + // MARK: Properties + + @Binding var user: User? + let createFolder: ActionClosure + let uploadFile: ActionClosure + let showProfile: ActionClosure + + // MARK: Body + + var body: some View { + if let user { + NavigationView { + BrowseView( + folder: .init( + id: user.rootFolder.id, + name: user.rootFolder.name + ), + createFolder: createFolder, + uploadFile: uploadFile, + showProfile: showProfile + ) + } + } else { + EmptyView() + } + } + + } +} + // MARK: - Helpers private extension ContentView { diff --git a/Modules/Sources/Browse/Logic/Models/Folder.swift b/Modules/Sources/Browse/Logic/Models/Folder.swift index 55054ef..557c9f2 100644 --- a/Modules/Sources/Browse/Logic/Models/Folder.swift +++ b/Modules/Sources/Browse/Logic/Models/Folder.swift @@ -28,3 +28,7 @@ public struct Folder { // MARK: - ModelIdentifiable extension Folder: ModelIdentifiable {} + +// MARK: - Equatable + +extension Folder: Equatable {} diff --git a/Modules/Sources/Browse/UI/Views/BrowseView.swift b/Modules/Sources/Browse/UI/Views/BrowseView.swift index b13754a..1c6764a 100644 --- a/Modules/Sources/Browse/UI/Views/BrowseView.swift +++ b/Modules/Sources/Browse/UI/Views/BrowseView.swift @@ -7,10 +7,19 @@ // import DataModels +import KeychainStorage import SwiftUI public struct BrowseView: View { + // MARK: Storages + + @KeychainStorage(key: .KeychainStorage.account) private var account: Account? + + // MARK: States + + @State private var items: [any ModelIdentifiable] = [] + // MARK: Properties private let folder: Folder @@ -18,6 +27,8 @@ public struct BrowseView: View { private let uploadFile: ActionClosure private let showProfile: ActionClosure + private let getItems: GetItemsUseCase = .init() + // MARK: Initialisers public init( @@ -36,92 +47,102 @@ public struct BrowseView: View { public var body: some View { List { - Group { - Group { - FolderItem(name: "Some folder #1 name") - FolderItem(name: "Some folder #2 name") - FolderItem(name: "Some folder #3 name") - FolderItem(name: "Some folder #4 name") - FolderItem(name: "Some folder #5 name") - FolderItem(name: "Some folder #6 name") - FolderItem(name: "Some folder #7 name") - } - Group { + ForEach(items, id: \.id) { item in + if let folder = item as? Folder { + FolderItem(name: folder.name) + } else if let file = item as? File { DocumentItem( - name: "Some document #1 name", - lastModified: "3 months ago", - fileSize: "1,23 Mbytes" - ) - DocumentItem( - name: "Some document #2 name", - lastModified: "2 years ago", - fileSize: "123 Kbytes" - ) - DocumentItem( - name: "Some document #3 name", - lastModified: "13 days ago", - fileSize: "12 bytes" - ) - DocumentItem( - name: "Some document #4 name", - lastModified: "13 hours ago", - fileSize: "12,3 Gbytes" - ) - DocumentItem( - name: "Some document #5 name", - lastModified: "13 minutes ago", - fileSize: "123 Tbytes" - ) - DocumentItem( - name: "Some document #6 name", - lastModified: "13 seconds ago", - fileSize: "123 Tbytes" - ) - DocumentItem( - name: "Some document #7 name", - lastModified: "13 nanoseconds ago", - fileSize: "123 Tbytes" + name: file.name, + lastModified: "-", + fileSize: "-" ) } } - .swipeActions( - edge: .trailing, - allowsFullSwipe: true - ) { - Button { - // TODO: Implement the removal of the item from the API. - } label: { - Label { - Text( - "browse.swipe_action.delete_item.text", - bundle: .module, - comment: "Delete item swipe action text." - ) - } icon: { - Image.trash - } - } - .tint(.red) - - // TODO: allow download only if item is a file. - Button { - // TODO: Implement the downloading of the data of the item from the API into the device. - } label: { - Label { - Text( - "browse.swipe_action.download_item.text", - bundle: .module, - comment: "Download item swipe action text." - ) - } icon: { - Image.download - } - } - .tint(.orange) - } +// Group { +// Group { +// FolderItem(name: "Some folder #1 name") +// FolderItem(name: "Some folder #2 name") +// FolderItem(name: "Some folder #3 name") +// FolderItem(name: "Some folder #4 name") +// FolderItem(name: "Some folder #5 name") +// FolderItem(name: "Some folder #6 name") +// FolderItem(name: "Some folder #7 name") +// } +// Group { +// DocumentItem( +// name: "Some document #1 name", +// lastModified: "3 months ago", +// fileSize: "1,23 Mbytes" +// ) +// DocumentItem( +// name: "Some document #2 name", +// lastModified: "2 years ago", +// fileSize: "123 Kbytes" +// ) +// DocumentItem( +// name: "Some document #3 name", +// lastModified: "13 days ago", +// fileSize: "12 bytes" +// ) +// DocumentItem( +// name: "Some document #4 name", +// lastModified: "13 hours ago", +// fileSize: "12,3 Gbytes" +// ) +// DocumentItem( +// name: "Some document #5 name", +// lastModified: "13 minutes ago", +// fileSize: "123 Tbytes" +// ) +// DocumentItem( +// name: "Some document #6 name", +// lastModified: "13 seconds ago", +// fileSize: "123 Tbytes" +// ) +// DocumentItem( +// name: "Some document #7 name", +// lastModified: "13 nanoseconds ago", +// fileSize: "123 Tbytes" +// ) +// } +// } +// .swipeActions( +// edge: .trailing, +// allowsFullSwipe: true +// ) { +// Button { +// // TODO: Implement the removal of the item from the API. +// } label: { +// Label { +// Text( +// "browse.swipe_action.delete_item.text", +// bundle: .module, +// comment: "Delete item swipe action text." +// ) +// } icon: { +// Image.trash +// } +// } +// .tint(.red) +// +// // TODO: allow download only if item is a file. +// Button { +// // TODO: Implement the downloading of the data of the item from the API into the device. +// } label: { +// Label { +// Text( +// "browse.swipe_action.download_item.text", +// bundle: .module, +// comment: "Download item swipe action text." +// ) +// } icon: { +// Image.download +// } +// } +// .tint(.orange) +// } } .listStyle(.inset) - .background(Color.red) .navigationTitle(folder.name) .toolbar { BrowseToolbar( @@ -130,6 +151,29 @@ public struct BrowseView: View { showProfile: showProfile ) } + .task(id: folder) { + await getItemsOrStop() + } + } +} + +// MARK: - Helpers + +private extension BrowseView { + func getItemsOrStop() async { + guard let account else { return } + + do { + items = try await getItems( + id: folder.id, + username: account.username, + password: account.password + ) + } catch let error { + print(folder) + print(error) + // TODO: Handle the error. + } } }