From c711efc7e0677a6bfbebd3cda49aabef63464534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 17 Oct 2021 08:58:43 +0200 Subject: [PATCH] Combine demo app is iOS15+ --- .../GRDBCombineDemo.xcodeproj/project.pbxproj | 8 +- .../GRDBCombineDemo/Views/AppView.swift | 114 ++++++++++++------ .../Views/PlayerCreationView.swift | 32 ++--- .../Views/PlayerEditionView.swift | 18 +-- .../Views/PlayerFormView.swift | 38 ++++-- .../GRDBCombineDemo/Views/PlayerList.swift | 15 ++- 6 files changed, 145 insertions(+), 80 deletions(-) diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj index d3ad48f6c..530ed6762 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj @@ -574,7 +574,6 @@ BUNDLE_LOADER = "$(TEST_HOST)"; CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = GRDBCombineDemoTests/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 14.3; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -594,7 +593,6 @@ BUNDLE_LOADER = "$(TEST_HOST)"; CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = GRDBCombineDemoTests/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 14.3; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -659,7 +657,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -714,7 +712,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = iphoneos; @@ -734,7 +732,6 @@ DEVELOPMENT_TEAM = ""; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = GRDBCombineDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -756,7 +753,6 @@ DEVELOPMENT_TEAM = ""; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = GRDBCombineDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift index df2ffbdde..a2df4d6ac 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift @@ -8,9 +8,12 @@ struct AppView: View { /// The `players` property is kept up-to-date with the list of players. @Query(PlayerRequest(ordering: .byScore)) private var players: [Player] + /// We'll need to leave edit mode in several occasions. + @State private var editMode = EditMode.inactive + /// Tracks the presentation of the player creation sheet. @State private var newPlayerIsPresented = false - + // If you want to define the query on initialization, you will prefer: // // @Query private var players: [Player] @@ -28,62 +31,103 @@ struct AppView: View { EditButton() newPlayerButton }, - trailing: ToggleOrderingButton(ordering: $players.ordering)) + trailing: ToggleOrderingButton( + ordering: $players.ordering, + willChange: { + // onChange(of: $players.wrappedValue.ordering) + // reveals a bug in SwiftUI: the List remains in + // editing mode if the editMode is changed during + // the animation of the list content. + // Word around: stop editing *before* the ordering + // is changed, and the list content is updated. + stopEditing() + })) .toolbar { toolbarContent } + .onChange(of: players) { players in + if players.isEmpty { + stopEditing() + } + } + .environment(\.editMode, $editMode) } } private var toolbarContent: some ToolbarContent { ToolbarItemGroup(placement: .bottomBar) { - Button( - action: { try? appDatabase.deleteAllPlayers() }, - label: { Image(systemName: "trash").imageScale(.large) }) + Button { + // Don't stopEditing() here because this is + // performed `onChange(of: players)` + try! appDatabase.deleteAllPlayers() + } label: { + Image(systemName: "trash").imageScale(.large) + } + Spacer() - Button( - action: { try? appDatabase.refreshPlayers() }, - label: { Image(systemName: "arrow.clockwise").imageScale(.large) }) + + Button { + stopEditing() + try! appDatabase.refreshPlayers() + } label: { + Image(systemName: "arrow.clockwise").imageScale(.large) + } + + Spacer() + + Button { + stopEditing() + // Perform 50 refreshes in parallel + for _ in 0..<50 { + DispatchQueue.global().async { + try! AppDatabase.shared.refreshPlayers() + } + } + } label: { + Image(systemName: "tornado").imageScale(.large) + } } } /// The button that presents the player creation sheet. private var newPlayerButton: some View { - Button( - action: { newPlayerIsPresented = true }, - label: { Image(systemName: "plus") }) - .accessibility(label: Text("New Player")) - .sheet( - isPresented: $newPlayerIsPresented, - content: { - PlayerCreationView(dismissAction: { - newPlayerIsPresented = false - }) - }) + Button { + stopEditing() + newPlayerIsPresented = true + } label: { + Image(systemName: "plus") + } + .accessibility(label: Text("New Player")) + .sheet(isPresented: $newPlayerIsPresented) { + PlayerCreationView() + } + } + + private func stopEditing() { + withAnimation { + editMode = .inactive + } } } private struct ToggleOrderingButton: View { @Binding var ordering: PlayerRequest.Ordering + let willChange: () -> Void var body: some View { switch ordering { case .byName: - Button( - action: { ordering = .byScore }, - label: { - HStack { - Text("Name") - Image(systemName: "arrowtriangle.up.fill") - } - }) + Button { + willChange() + ordering = .byScore + } label: { + Label("Name", systemImage: "arrowtriangle.up.fill").labelStyle(.titleAndIcon) + } case .byScore: - Button( - action: { ordering = .byName }, - label: { - HStack { - Text("Score") - Image(systemName: "arrowtriangle.down.fill") - } - }) + Button { + willChange() + ordering = .byName + } label: { + Label("Score", systemImage: "arrowtriangle.down.fill").labelStyle(.titleAndIcon) + } } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift index b487d336e..bee16f99a 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift @@ -2,37 +2,39 @@ import SwiftUI /// The view that creates a new player. struct PlayerCreationView: View { - /// Executed when user cancels or saves the new user. - let dismissAction: () -> Void - @Environment(\.appDatabase) private var appDatabase - @State private var name = "" - @State private var score = "" + @Environment(\.dismiss) private var dismiss + @State private var form = PlayerForm(name: "", score: "") @State private var errorAlertIsPresented = false @State private var errorAlertTitle = "" var body: some View { NavigationView { - PlayerFormView(name: $name, score: $score) + PlayerFormView(form: $form) .alert( isPresented: $errorAlertIsPresented, content: { Alert(title: Text(errorAlertTitle)) }) .navigationBarTitle("New Player") .navigationBarItems( - leading: Button( - action: dismissAction, - label: { Text("Cancel") }), - trailing: Button( - action: save, - label: { Text("Save") })) + leading: Button(role: .cancel) { + dismiss() + } label: { + Text("Cancel") + }, + trailing: Button { + save() + } label: { + Text("Save") + }) } } private func save() { do { - var player = Player(id: nil, name: name, score: Int(score) ?? 0) + var player = Player(id: nil, name: "", score: 0) + form.apply(to: &player) try appDatabase.savePlayer(&player) - dismissAction() + dismiss() } catch { errorAlertTitle = (error as? LocalizedError)?.errorDescription ?? "An error occurred" errorAlertIsPresented = true @@ -42,6 +44,6 @@ struct PlayerCreationView: View { struct PlayerCreationSheet_Previews: PreviewProvider { static var previews: some View { - PlayerCreationView(dismissAction: { }) + PlayerCreationView() } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift index eae08c6f5..6175f5273 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift @@ -3,17 +3,21 @@ import SwiftUI /// The view that edits an existing player. struct PlayerEditionView: View { @Environment(\.appDatabase) private var appDatabase - @State var player: Player + private let player: Player + @State private var form: PlayerForm + + init(player: Player) { + self.player = player + self.form = PlayerForm(player) + } var body: some View { - PlayerFormView( - name: $player.name, - score: Binding( - get: { "\(player.score)" }, - set: { player.score = Int($0) ?? 0 })) + PlayerFormView(form: $form) .onDisappear { // save and ignore error - try? appDatabase.savePlayer(&player) + var savedPlayer = player + form.apply(to: &savedPlayer) + try? appDatabase.savePlayer(&savedPlayer) } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerFormView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerFormView.swift index f6b59d48f..bc7883e28 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerFormView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerFormView.swift @@ -3,29 +3,45 @@ import SwiftUI /// The Player editing form, embedded in both /// `PlayerCreationView` and `PlayerEditionView`. struct PlayerFormView: View { - @Binding var name: String - @Binding var score: String - + @Binding var form: PlayerForm + var body: some View { List { - TextField("Name", text: $name) + TextField("Name", text: $form.name) .accessibility(label: Text("Player Name")) - TextField("Score", text: $score).keyboardType(.numberPad) + TextField("Score", text: $form.score).keyboardType(.numberPad) .accessibility(label: Text("Player Score")) } .listStyle(InsetGroupedListStyle()) } } +struct PlayerForm { + var name: String + var score: String +} + +extension PlayerForm { + init(_ player: Player) { + self.name = player.name + self.score = "\(player.score)" + } + + func apply(to player: inout Player) { + player.name = name + player.score = Int(score) ?? 0 + } +} + struct PlayerFormView_Previews: PreviewProvider { static var previews: some View { Group { - PlayerFormView( - name: .constant(""), - score: .constant("")) - PlayerFormView( - name: .constant(Player.randomName()), - score: .constant("\(Player.randomScore())")) + PlayerFormView(form: .constant(PlayerForm( + name: "", + score: ""))) + PlayerFormView(form: .constant(PlayerForm( + name: Player.randomName(), + score: "\(Player.randomScore())"))) } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift index d6acbc553..c6f465c70 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift @@ -11,16 +11,19 @@ struct PlayerList: View { List { ForEach(players) { player in NavigationLink(destination: editionView(for: player)) { - PlayerRow(player: player).animation(nil) + PlayerRow(player: player) + // Don't animate player update + .animation(nil, value: player) } } - .onDelete(perform: { offsets in + .onDelete { offsets in let playerIds = offsets.compactMap { players[$0].id } try? appDatabase.deletePlayers(ids: playerIds) - }) + } } - .animation(.default) - .listStyle(PlainListStyle()) + // Animate list updates + .animation(.default, value: players) + .listStyle(.plain) } /// The view that edits a player in the list. @@ -48,7 +51,7 @@ struct PlayerList_Previews: PreviewProvider { Player(id: 1, name: "Arthur", score: 100), Player(id: 2, name: "Barbara", score: 1000), ]) - .navigationTitle("Preview") + .navigationTitle("Preview") } } }