Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mergesort/Boutique. ✨ A magical persistence library (and so much more) for state-driven iOS and Mac apps ✨ build.ms/boutique/docs
actors appkit cache database ios macos offline-first persistence swift swiftui uikit
Something went wrong. Try again.
4.0 kB · 105 lines
Swift
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import Foundationimport SwiftUI
struct CompactContentView: View { @State private var richNotesController = RichNotesController(store: .notesStore)
@State private var notes: [RichNote] = [] @State private var operation = RichNotesOperation(action: .add) @State private var storeLaunchDuration: TimeInterval = 0.0
var body: some View { VStack(spacing: 0.0) { TerminalView( statistics: [ PerformanceStatisticItem( title: "Stored Objects", measurement: "\(notes.count)" ), PerformanceStatisticItem( title: "Used Memory", measurement: "\(MemoryFormatter.formatted(bytes: notes.projectedByteCount))" ), PerformanceStatisticItem( title: "Store Startup Time", measurement: String(format: "%.3f s", self.operation.action == .loading ? 0.0 : self.storeLaunchDuration) ), PerformanceStatisticItem( title: "Avg Object Size", measurement: "\(MemoryFormatter.formatted(bytes: notes.count == 0 ? 0 : notes.projectedByteCount/notes.count, unit: .bytes))" ) ] ) .padding(16.0)
OperationProgressView(operation: self.operation)
CountButtonContainerView( operation: self.$operation, addItemsAction: { itemCount in self.failableAsyncOperation({ try await richNotesController.addItems(count: itemCount) }) }, removeItemsAction: { itemCount in self.failableAsyncOperation({ try await richNotesController.removeItems(count: itemCount) }) }, removeAllAction: { self.failableAsyncOperation({ try await self.richNotesController.removeAll() }) } ) .padding(.horizontal, 32.0) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) .animation(.easeInOut(duration: 0.15), value: self.operation.action)
Text("Selected Operation") .fontWeight(.bold) .textShadow() .font(.telegramaRaw(style: .title1)) .frame(maxWidth: .infinity) .frame(height: 64.0) .foregroundColor(.white)
HStack(alignment: .center, spacing: 16.0) { RichNotesOperationsView(operation: self.$operation) } .padding(16.0) .padding(.horizontal, 16.0)
Spacer() } .background(Color.palette.appBackground) .onAppear(perform: { self.storeLaunchDuration = ProcessInfo.processInfo.systemUptime self.operation.action = .loading self.operation.isInProgress = true }) .onChange(of: self.richNotesController.notes, initial: true, { if self.operation.action == .loading { self.operation.action = .add let initialMarkerTime = self.storeLaunchDuration self.storeLaunchDuration = ProcessInfo.processInfo.systemUptime - initialMarkerTime self.operation.isInProgress = false }
self.notes = self.richNotesController.notes }) }}
private extension CompactContentView { func failableAsyncOperation(_ action: @escaping () async throws -> Void) { Task { do { try await action() } catch { print("Error running operation", error) self.operation.isInProgress = false } } }}