Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jul 23, 2024
1 parent dac2136 commit 272bac6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 101 deletions.
176 changes: 80 additions & 96 deletions Examples/SupaDrive/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import IdentifiedCollections
@MainActor
@Observable
final class AppModel {
var panels: IdentifiedArrayOf<PanelModel> {
let root: PanelModel

var panels: IdentifiedArrayOf<PanelModel> = [] {
didSet {
bindPanelModels()
}
}

init(panels: IdentifiedArrayOf<PanelModel>) {
self.panels = panels
init(root: PanelModel) {
self.root = root
bindPanelModels()
}

Expand All @@ -33,22 +35,15 @@ final class AppModel {
}

var selectedFile: FileObject? {
panels.last?.selectedItem
nil// panels.last?.selectedItem
}

private func bindPanelModels() {
for panel in panels {
for panel in [root] + panels {
panel.onSelectItem = { [weak self, weak panel] item in
guard let self, let panel else { return }

// self.panels.append(PanelModel(path: self.path.appending))
//
// if let name = item.name, item.id == nil {
// self.panels.replaceSubrange(
// (index + 1)...,
// with: [PanelModel(path: self.path.appending("/\(name)"))]
// )
// }
self.panels.append(PanelModel(path: panel.path.appending("/\(item.name!)")))
}
}
}
Expand All @@ -58,35 +53,11 @@ struct AppView: View {
@Bindable var model: AppModel

var body: some View {
VStack(alignment: .leading, spacing: 0) {
breadcrump

ScrollView(.horizontal) {
HStack {
ForEach(model.panels) { panel in
PanelView(
model: panel
// model: PanelModel(path: path[0 ... pathIndex].joined(separator: "/"))
// path: path[0 ... pathIndex].joined(separator: "/"),
// selectedItem: Binding(
// get: {
// selectedItemPerPath[path[pathIndex]]
// },
// set: { newValue in
// selectedItemPerPath[path[pathIndex]] = newValue
//
// if let newValue, let name = newValue.name, newValue.id == nil {
// path.replaceSubrange((pathIndex + 1)..., with: [name])
// } else {
// path.replaceSubrange((pathIndex + 1)..., with: [])
// }
// }
// )
)
.frame(width: 200)
}
NavigationStack(path: $model.panels) {
PanelView(model: model.root)
.navigationDestination(for: PanelModel.self) { model in
PanelView(model: model)
}
}
}
.overlay(alignment: .trailing) {
if let selectedFile = model.selectedFile {
Expand All @@ -110,24 +81,8 @@ struct AppView: View {
.transition(.move(edge: .trailing))
}
}
.animation(.default, value: model.path)
.animation(.default, value: model.selectedFile)
}

var breadcrump: some View {
HStack {
ForEach(Array(zip(model.pathComponents.indices, model.pathComponents)), id: \.0) { idx, path in
Button(path) {
// self.path.replaceSubrange((idx + 1)..., with: [])
}
.buttonStyle(.plain)

// if idx != self.path.indices.last {
// Text(">")
// }
}
}
.padding()
// .animation(.default, value: model.path)
// .animation(.default, value: model.selectedFile)
}
}

Expand All @@ -138,9 +93,17 @@ struct DragValue: Codable {

@MainActor
@Observable
final class PanelModel: Identifiable {
final class PanelModel: Identifiable, Hashable {
nonisolated func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}

nonisolated static func == (lhs: PanelModel, rhs: PanelModel) -> Bool {
lhs === rhs
}

let path: String
var selectedItem: FileObject?
var selectedItem: FileObject.ID?

var items: [FileObject] = []

Expand All @@ -160,15 +123,21 @@ final class PanelModel: Identifiable {
}
}

func didSelectItem(_ item: FileObject) {
self.selectedItem = item
func onPrimaryAction(_ itemID: FileObject.ID) {
guard let item = items.first(where: { $0.id == itemID }) else { return }
onSelectItem(item)
}

// func didSelectItem(_ item: FileObject) {
// self.selectedItem = item
// onSelectItem(item)
// }

func newFolderButtonTapped() async {
do {
try await supabase.storage.from("main")
.upload(path: "\(path)/Untiltled/.dummy", file: Data())
await load()
} catch {

}
Expand All @@ -181,6 +150,7 @@ final class PanelModel: Identifiable {
let file = try Data(contentsOf: url)
try await supabase.storage.from("main")
.upload(path: "\(self.path)/\(path)", file: file)
await load()
} catch {}
}
}
Expand All @@ -191,33 +161,58 @@ struct PanelView: View {
@State private var isDraggingOver = false

var body: some View {
List {
ForEach(model.items) { item in
Text(item.name ?? "")
.bold(model.selectedItem == item)
.onTapGesture {
model.didSelectItem(item)
}
.onDrag {
let data = try! JSONEncoder().encode(DragValue(path: model.path, object: item))
let string = String(decoding: data, as: UTF8.self)
return NSItemProvider(object: string as NSString)
}
Table(model.items, selection: $model.selectedItem) {
TableColumn("Name") { item in
Text(item.name ?? "No name")
}
.onInsert(of: ["public.text"]) { index, items in
for item in items {
Task {
guard let data = try await item.loadItem(forTypeIdentifier: "public.text") as? Data,
let value = try? JSONDecoder().decode(DragValue.self, from: data) else {
return
}

self.model.items.insert(value.object, at: index)
}
TableColumn("Date modified") { item in
if let lastModifiedStringValue = item.metadata?["lastModified"]?.stringValue,
let lastModified = try? Date(lastModifiedStringValue, strategy: .iso8601.day().month().year().dateTimeSeparator(.standard).time(includingFractionalSeconds: true))
{
Text(lastModified.formatted(date: .abbreviated, time: .shortened))
} else {
Text("-")
}
}

TableColumn("Size") { item in
if let sizeRawValue = item.metadata?["size"]?.intValue {
Text(sizeRawValue.formatted(.byteCount(style: .file)))
} else {
Text("-")
}
print(index, items)
}
}
.contextMenu(
forSelectionType: FileObject.ID.self,
menu: { items in
Button("New folder") {
Task {
await model.newFolderButtonTapped()
}
}
},
primaryAction: { items in
guard let item = items.first else { return }

model.onPrimaryAction(item)
}
)
// .onInsert(of: ["public.text"]) { index, items in
// for item in items {
// Task {
// guard let data = try await item.loadItem(forTypeIdentifier: "public.text") as? Data,
// let value = try? JSONDecoder().decode(DragValue.self, from: data) else {
// return
// }
//
// self.model.items.insert(value.object, at: index)
// }
// }
// print(index, items)
// }
.navigationTitle(model.path)
.task {
await model.load()
}
Expand All @@ -240,16 +235,5 @@ struct PanelView: View {
Color.gray.opacity(0.2)
}
}
.contextMenu {
Button("New folder") {
Task {
await model.newFolderButtonTapped()
}
}
}
}
}

#Preview {
AppView(model: AppModel(panels: []))
}
20 changes: 15 additions & 5 deletions Examples/SupaDrive/SupaDriveApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@

import Supabase
import SwiftUI
import OSLog

let supabase = SupabaseClient(
supabaseURL: URL(string: "http://127.0.0.1:54321")!,
supabaseKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0",
options: SupabaseClientOptions(global: .init(logger: AppLogger()))
options: SupabaseClientOptions(global: .init(logger: Logger.supabase))
)

struct AppLogger: SupabaseLogger {
func log(message: SupabaseLogMessage) {
print(message.description)
extension Logger: @retroactive SupabaseLogger {
static let supabase = Logger(subsystem: "supadrive", category: "supabase")

public func log(message: SupabaseLogMessage) {
let logType: OSLogType = switch message.level {
case .debug: .debug
case .error: .error
case .verbose: .info
case .warning: .fault
}

self.log(level: logType, "\(message)")
}
}

Expand All @@ -26,7 +36,7 @@ struct SupaDriveApp: App {
WindowGroup {
AuthView { session in
AppView(
model: AppModel(panels: [PanelModel(path: session.user.id.uuidString.lowercased())])
model: AppModel(root: PanelModel(path: session.user.id.uuidString.lowercased()))
)
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/IntegrationTests/supabase/.branches/_current_branch
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
1 change: 1 addition & 0 deletions Tests/IntegrationTests/supabase/.temp/cli-latest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.183.5

0 comments on commit 272bac6

Please sign in to comment.