GRDBDemo cleanup

This commit is contained in:
Gwendal Roué committed 2024-09-22 00:19:05 +02:00
1 parent 4463374ff3
commit 0af8415a22
7 files changed
+51 -23

No files matched your search

@@ -11,15 +11,8 @@ struct GRDBDemoApp: App {
// MARK: - Give SwiftUI access to the database
private struct AppDatabaseKey: EnvironmentKey {
static var defaultValue: AppDatabase { .empty() }
}
extension EnvironmentValues {
var appDatabase: AppDatabase {
get { self[AppDatabaseKey.self] }
set { self[AppDatabaseKey.self] = newValue }
}
@Entry var appDatabase = AppDatabase.empty()
}
extension View {
@@ -20,6 +20,12 @@
"state" : "new",
"value" : "%lld Players"
}
},
"zero" : {
"stringUnit" : {
"state" : "translated",
"value" : "No Player"
}
}
}
}
@@ -30,3 +30,9 @@ struct PlayerCreationSheet: View {
dismiss()
}
}
// MARK: - Previews
#Preview {
PlayerCreationSheet()
}
@@ -33,9 +33,10 @@ struct PlayerEditionView: View {
}
}
// MARK: - Previews
#Preview {
NavigationStack {
PlayerEditionView(player: .makeRandom())
.appDatabase(.empty())
}
}
@@ -43,10 +43,20 @@ struct PlayerForm {
var score: Int?
}
#Preview {
// MARK: - Previews
#Preview("Prefilled") {
@Previewable @State var form = PlayerForm(name: "John", score: 100)
Form {
PlayerFormView(form: $form)
}
}
#Preview("Empty") {
@Previewable @State var form = PlayerForm(name: "", score: nil)
Form {
PlayerFormView(form: $form)
}
}
@@ -1,16 +1,7 @@
import SwiftUI
struct PlayerListView<EmptyListActions: View>: View {
struct PlayerListView: View {
@Bindable var model: PlayerListModel
var emptyListActions: EmptyListActions
init(
model: PlayerListModel,
@ViewBuilder emptyListActions: () -> EmptyListActions = { EmptyView() }
) {
self.model = model
self.emptyListActions = emptyListActions()
}
var body: some View {
List {
@@ -52,10 +43,24 @@ struct PlayerRow: View {
}
}
// MARK: - Previews
#Preview {
struct Preview: View {
@State var model = PlayerListModel(appDatabase: .random())
@Environment(\.appDatabase) var appDatabase
var body: some View {
ContentView(appDatabase: appDatabase)
}
}
struct ContentView: View {
@State var model: PlayerListModel
init(appDatabase: AppDatabase) {
_model = State(initialValue: PlayerListModel(appDatabase: appDatabase))
}
var body: some View {
NavigationStack {
PlayerListView(model: model)
@@ -64,5 +69,5 @@ struct PlayerRow: View {
}
}
return Preview()
return Preview().appDatabase(.random())
}
@@ -165,7 +165,14 @@ private struct ToggleOrderingButton: View {
}
}
#Preview {
// MARK: - Previews
#Preview("Populated") {
PlayerNavigationView()
.appDatabase(.random())
}
#Preview("Empty") {
PlayerNavigationView()
.appDatabase(.empty())
}