Fix the way the edit view persists adds and deletes.
This commit is contained in:
parent
a1efa0f6c6
commit
2808a1bacd
|
@ -0,0 +1,88 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1130"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F378A9BA23D438AE00296A76"
|
||||
BuildableName = "Flashzilla.app"
|
||||
BlueprintName = "Flashzilla"
|
||||
ReferencedContainer = "container:Flashzilla.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
</Testables>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F378A9BA23D438AE00296A76"
|
||||
BuildableName = "Flashzilla.app"
|
||||
BlueprintName = "Flashzilla"
|
||||
ReferencedContainer = "container:Flashzilla.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
<CommandLineArguments>
|
||||
<CommandLineArgument
|
||||
argument = "-com.apple.CoreData.SQLDebug 1"
|
||||
isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
<CommandLineArgument
|
||||
argument = "-com.apple.CoreData.Logging.stderr 1"
|
||||
isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
</CommandLineArguments>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "F378A9BA23D438AE00296A76"
|
||||
BuildableName = "Flashzilla.app"
|
||||
BlueprintName = "Flashzilla"
|
||||
ReferencedContainer = "container:Flashzilla.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
|
@ -44,7 +44,6 @@ extension CardDeckContainerView: View {
|
|||
.allowsHitTesting(self.viewModel.timeRemaining > 0)
|
||||
}
|
||||
|
||||
|
||||
HStack {
|
||||
Spacer()
|
||||
|
||||
|
@ -64,7 +63,13 @@ extension CardDeckContainerView: View {
|
|||
.padding()
|
||||
.background(Color("CardDeckBackground"))
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
.sheet(isPresented: self.$isShowingEditView, onDismiss: self.viewModel.resumeRound) {
|
||||
.sheet(
|
||||
isPresented: self.$isShowingEditView,
|
||||
onDismiss: {
|
||||
self.viewModel.fetchCards()
|
||||
self.viewModel.resumeRound()
|
||||
}
|
||||
) {
|
||||
EditDeckView(
|
||||
viewModel: .init(currentDeck: self.viewModel.cardDeck)
|
||||
)
|
||||
|
|
|
@ -17,10 +17,11 @@ extension EditDeckView {
|
|||
private var subscriptions = Set<AnyCancellable>()
|
||||
|
||||
@ObservedObject var newCard: Card
|
||||
var currentDeck: CardDeck
|
||||
|
||||
|
||||
// MARK: - Published Outputs
|
||||
@Published var currentDeck: CardDeck
|
||||
@Published var cards: [Card] = []
|
||||
@Published var canAddNewCard: Bool = false
|
||||
|
||||
|
||||
|
@ -42,6 +43,13 @@ extension EditDeckView {
|
|||
// MARK: - Publishers
|
||||
extension EditDeckView.ViewModel {
|
||||
|
||||
private var cardsPublisher: Publishers.Share<AnyPublisher<[Card], Never>> {
|
||||
currentDeck.publisher(for: \.cards)
|
||||
.map { _ in self.currentDeck.cardsArray }
|
||||
.eraseToAnyPublisher()
|
||||
.share()
|
||||
}
|
||||
|
||||
private var newCardPromptTextPublisher: Publishers.Share<AnyPublisher<String, Never>> {
|
||||
newCard.publisher(for: \.prompt)
|
||||
.debounce(for: .milliseconds(200), scheduler: DispatchQueue.main)
|
||||
|
@ -76,7 +84,6 @@ extension EditDeckView.ViewModel {
|
|||
|
||||
// MARK: - Computeds
|
||||
extension EditDeckView.ViewModel {
|
||||
var cards: [Card] { currentDeck.cardsArray }
|
||||
}
|
||||
|
||||
|
||||
|
@ -84,7 +91,7 @@ extension EditDeckView.ViewModel {
|
|||
extension EditDeckView.ViewModel {
|
||||
|
||||
func addNewCard() {
|
||||
guard let managedObjectContext = currentDeck.managedObjectContext else { fatalError() }
|
||||
guard let managedObjectContext = newCard.managedObjectContext else { fatalError() }
|
||||
|
||||
currentDeck.addToCards(newCard)
|
||||
CurrentApp.coreDataManager.save(managedObjectContext)
|
||||
|
@ -94,13 +101,15 @@ extension EditDeckView.ViewModel {
|
|||
|
||||
|
||||
func removeCards(at offsets: IndexSet) {
|
||||
guard let managedObjectContext = currentDeck.managedObjectContext else { fatalError() }
|
||||
|
||||
for offset in offsets {
|
||||
currentDeck.removeFromCards(cards[offset])
|
||||
for index in offsets {
|
||||
let card = cards[index]
|
||||
|
||||
guard let managedObjectContext = card.managedObjectContext else { fatalError() }
|
||||
|
||||
managedObjectContext.delete(card)
|
||||
}
|
||||
|
||||
CurrentApp.coreDataManager.save(managedObjectContext)
|
||||
CurrentApp.coreDataManager.saveContexts()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,6 +124,11 @@ private extension EditDeckView.ViewModel {
|
|||
|
||||
|
||||
func setupSubscribers() {
|
||||
cardsPublisher
|
||||
.receive(on: DispatchQueue.main)
|
||||
.assign(to: \.cards, on: self)
|
||||
.store(in: &subscriptions)
|
||||
|
||||
canAddNewCardPublisher
|
||||
.receive(on: DispatchQueue.main)
|
||||
.assign(to: \.canAddNewCard, on: self)
|
||||
|
|
Loading…
Reference in New Issue