Skip to content

Commit

Permalink
fixing API
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 11, 2024
1 parent b93cace commit 9b8cd52
Show file tree
Hide file tree
Showing 19 changed files with 512 additions and 360 deletions.
2 changes: 0 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ identifier_name:
excluded:
- DerivedData
- .build
- Package.swift
- Package
indentation_width:
indentation_width: 2
file_name:
Expand Down
92 changes: 51 additions & 41 deletions Example/Sources/ContentObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,47 @@
// Created by Leo Dion on 10/10/24.
//

import Combine
import DataThespian
import Foundation
import SwiftData
import DataThespian
import Combine



@Observable
@MainActor
class ContentObject {
internal class ContentObject {
internal let databaseChangePublisher = PassthroughSubject<any DatabaseChangeSet, Never>()
private var databaseChangeCancellable : AnyCancellable?
private var databaseChangeCancellable: AnyCancellable?
private var databaseChangeSubscription: AnyCancellable?
private var database : (any Database)?
private var database: (any Database)?
internal private(set) var items = [ItemModel]()
internal var selectedItemsID: Set<ItemModel.ID> = []


var selectedItems : [ItemModel] {
private var newItem: AnyCancellable?
internal var error: (any Error)?

internal var selectedItems: [ItemModel] {
let selectedItemsID = self.selectedItemsID
return try! self.items.filter(#Predicate<ItemModel> {
selectedItemsID.contains($0.id)
})
let items: [ItemModel]
do {
items = try self.items.filter(
#Predicate<ItemModel> {
selectedItemsID.contains($0.id)
}
)
} catch {
assertionFailure("Unable to filter selected items: \(error.localizedDescription)")
self.error = error
items = []
}
assert(items.count == selectedItemsID.count)
return items
}
private var newItem: AnyCancellable?
var error: (any Error)?


internal init() {
self.databaseChangeSubscription = self.databaseChangePublisher.sink { _ in
self.beginUpdateItems()
}
}

private func beginUpdateItems() {
Task {
do {
Expand All @@ -41,7 +55,8 @@ class ContentObject {
}
}
}
fileprivate func updateItems() async throws {

private func updateItems() async throws {
guard let database else {
return
}
Expand All @@ -50,25 +65,21 @@ class ContentObject {
return items.map(ItemModel.init)
})
}

internal init () {
self.databaseChangeSubscription = self.databaseChangePublisher.sink { _ in
self.beginUpdateItems()
}
}

internal func initialize(withDatabase database: any Database, databaseChangePublisher: DatabaseChangePublicist) {

internal func initialize(
withDatabase database: any Database, databaseChangePublisher: DatabaseChangePublicist
) {
self.database = database
self.databaseChangeCancellable = databaseChangePublisher(id: "contentView")
self.databaseChangeCancellable = databaseChangePublisher(id: "contentView")
.subscribe(self.databaseChangePublisher)
self.beginUpdateItems()
}


fileprivate static func deleteModels( _ models: [ModelID<Item>], from database: (any Database)) async throws {

fileprivate static func deleteModels(_ models: [ModelID<Item>], from database: (any Database))
async throws
{
try await database.withModelContext { modelContext in
let items : [Item] = models.compactMap{
let items: [Item] = models.compactMap {
modelContext.model(for: $0.persistentIdentifier) as? Item
}
dump(items.first?.persistentModelID)
Expand All @@ -81,21 +92,21 @@ class ContentObject {
}
internal func deleteSelectedItems() {
let models = self.selectedItems.map {
ModelID<Item>.init(persistentIdentifier: $0.id)
ModelID<Item>(persistentIdentifier: $0.id)
}
self.deleteItems(models)
}
internal func deleteItems(offsets: IndexSet) {

let models = offsets
.compactMap{items[$0].id}
.map(ModelID<Item>.init(persistentIdentifier: ))
let models =
offsets
.compactMap { items[$0].id }
.map(ModelID<Item>.init(persistentIdentifier:))

assert(models.count == offsets.count)

self.deleteItems(models)
}

internal func deleteItems(_ models: [ModelID<Item>]) {
guard let database else {
return
Expand All @@ -104,14 +115,13 @@ class ContentObject {
try await Self.deleteModels(models, from: database)
}
}

internal func addItem(withDate date: Date = .init()) {
guard let database else {
return
}
Task {
try await database.withModelContext { modelContext in

let newItem = Item(timestamp: date)
modelContext.insert(newItem)
dump(newItem.persistentModelID)
Expand Down
38 changes: 21 additions & 17 deletions Example/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
// Created by Leo Dion on 10/10/24.
//

import SwiftUI
import SwiftData
import DataThespian
import Combine


import DataThespian
import SwiftData
import SwiftUI

struct ContentView: View {
@State var object = ContentObject()
@Environment(\.database) var database
@Environment(\.databaseChangePublicist) var databaseChangePublisher




var body: some View {
NavigationSplitView {
List(selection: self.$object.selectedItemsID) {
Expand All @@ -29,12 +25,10 @@ struct ContentView: View {
}
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
.toolbar {

ToolbarItem {

Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
Label("Add Item", systemImage: "plus")
}
}
ToolbarItem {
Button(action: object.deleteSelectedItems) {
Expand All @@ -51,21 +45,31 @@ struct ContentView: View {
} else {
Text("Select an item")
}

}.onAppear {
self.object.initialize(withDatabase: database, databaseChangePublisher: databaseChangePublisher)
self.object.initialize(
withDatabase: database, databaseChangePublisher: databaseChangePublisher)
}
}

private func addItem() {
self.addItem(withDate: .init())
}
private func addItem(withDate date: Date ) {
private func addItem(withDate date: Date) {
self.object.addItem(withDate: .init())
}

}

#Preview {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
// swiftlint:disable:next force_try
let modelContainer = try! ModelContainer(for: Item.self, configurations: config)

ContentView()
.environment(
\.databaseChangePublicist,
DatabaseChangePublicist(
dbWatcher: DataMonitor.shared
)
)
.database(BackgroundDatabase(modelContainer: modelContainer))
}
33 changes: 19 additions & 14 deletions Example/Sources/DataThespianExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
// Created by Leo Dion on 10/10/24.
//

import SwiftUI
import SwiftData
import DataThespian
import SwiftData
import SwiftUI

@main
struct DataThespianExampleApp: App {
private static let databaseChangePublicist = DatabaseChangePublicist(dbWatcher: DataMonitor.shared)
private static let database = try! BackgroundDatabase(modelContainer: .init(for: Item.self), autosaveEnabled: true)

init () {
internal struct DataThespianExampleApp: App {
private static let databaseChangePublicist = DatabaseChangePublicist( dbWatcher: DataMonitor.shared)
// swiftlint:disable:next force_try
private static let database = try! BackgroundDatabase(
modelContainer: .init(for: Item.self),
autosaveEnabled: true
)

internal var body: some Scene {
WindowGroup {
ContentView()
}
.database(Self.database)
.environment(\.databaseChangePublicist, Self.databaseChangePublicist)
}

internal init() {
DataMonitor.shared.begin(with: [])
}
var body: some Scene {
WindowGroup {
ContentView()
}
.database(Self.database)
.environment(\.databaseChangePublicist, Self.databaseChangePublicist)
}
}
10 changes: 5 additions & 5 deletions Example/Sources/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import SwiftData

@Model
final class Item {
var timestamp: Date
init(timestamp: Date) {
self.timestamp = timestamp
}
var timestamp: Date

init(timestamp: Date) {
self.timestamp = timestamp
}
}
18 changes: 8 additions & 10 deletions Example/Sources/ItemModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@
// Created by Leo Dion on 10/10/24.
//

import Foundation
import DataThespian
import Foundation
import SwiftData



struct ItemModel : Identifiable {
struct ItemModel: Identifiable {
private init(model: ModelID<Item>, timestamp: Date) {
self.model = model
self.timestamp = timestamp
}
internal init(item : Item) {

internal init(item: Item) {
self.init(model: .init(item), timestamp: item.timestamp)
}
@available(*, deprecated)
internal init(id: PersistentIdentifier, timestamp: Date) {
self.model = .init(persistentIdentifier: id)
self.timestamp = timestamp
}
var id : PersistentIdentifier {
return model.persistentIdentifier

var id: PersistentIdentifier {
model.persistentIdentifier
}
let model : ModelID<Item>
let model: ModelID<Item>
let timestamp: Date
}
4 changes: 2 additions & 2 deletions Scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fi
if [ -z "$CI" ]; then
run_command $MINT_RUN swiftlint --fix
pushd $PACKAGE_DIR
run_command $MINT_RUN swift-format format $SWIFTFORMAT_OPTIONS --recursive --parallel --in-place Sources Tests
run_command $MINT_RUN swift-format format $SWIFTFORMAT_OPTIONS --recursive --parallel --in-place Sources Tests Example/Sources
popd
else
set -e
Expand All @@ -63,6 +63,6 @@ $PACKAGE_DIR/scripts/header.sh -d $PACKAGE_DIR/Sources -c "Leo Dion" -o "Bright
run_command $MINT_RUN swiftlint lint $SWIFTLINT_OPTIONS

pushd $PACKAGE_DIR
run_command $MINT_RUN swift-format lint --recursive --parallel $SWIFTFORMAT_OPTIONS Sources Tests
run_command $MINT_RUN swift-format lint --recursive --parallel $SWIFTFORMAT_OPTIONS Sources Tests Example/Sources
run_command $MINT_RUN periphery scan $PERIPHERY_OPTIONS --disable-update-check
popd
4 changes: 4 additions & 0 deletions Sources/DataThespian/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

public import Foundation

@inlinable internal func assert(isMainThread: Bool, if assertIsBackground: Bool) {
assert(!assertIsBackground || isMainThread == Thread.isMainThread)
}

@inlinable internal func assert(isMainThread: Bool) { assert(isMainThread == Thread.isMainThread) }

@inlinable internal func assertionFailure(
Expand Down
Loading

0 comments on commit 9b8cd52

Please sign in to comment.