Fix the fetch request used by the CardDeckContainerView

This commit is contained in:
CypherPoet 2020-01-22 17:42:49 -06:00
parent 4d9d45f3e1
commit 934e328595
4 changed files with 34 additions and 9 deletions

View File

@ -96,12 +96,13 @@ I'm currently seeking freelance, remote opportunities as an iOS developer! If yo
- **Day 87:** [_Project 17: Flashzilla (Part Two)_](./day-087/)
- **Day 88:** [_Project 17: Flashzilla (Part Three)_](./day-088/)
- **Day 89:** [_Project 17: Flashzilla (Part Four)_](./day-089/)
- **Day 90:** [_Project 17: Flashzilla (Part Five)_](./day-090/)
</details>
**Latest Day:**
- **Day 90:** [_Project 17: Flashzilla (Part Five)_](./day-090/)
- **Day 91:** [_Project 17: Flashzilla (Part Six)_](./day-091/)

View File

@ -25,12 +25,12 @@ extension Card {
public enum Predicate {
public static func cards(in cardDeck: CardDeck) -> NSPredicate {
let keyword = NSComparisonPredicate.keyword(for: .contains)
let keyword = NSComparisonPredicate.keyword(for: .in)
let predicate = NSPredicate(
format: "%K \(keyword) %@",
#keyPath(Card.decks),
cardDeck.objectID
format: "%@ \(keyword) %K",
cardDeck.objectID,
#keyPath(Card.decks)
)
return predicate

View File

@ -19,6 +19,7 @@ extension CardDeckContainerView {
typealias FetchedResult = Card
lazy var fetchRequest: NSFetchRequest<Card> = Card.fetchRequest(forCardsIn: cardDeck)
internal lazy var fetchedResultsController: FetchedResultsController = makeFetchedResultsController()
private var subscriptions = Set<AnyCancellable>()
@ -80,7 +81,7 @@ extension CardDeckContainerView.ViewModel {
private var visibleCardsPublisher: AnyPublisher<[Card], Never> {
$cards
.map { $0.filter { $0.answerState == .unanswered } }
// .print("visibleCardsPublisher")
.print("visibleCardsPublisher")
.eraseToAnyPublisher()
}
}
@ -108,7 +109,7 @@ extension CardDeckContainerView.ViewModel {
var unansweredCountText: String {
let count = cardDeck.unansweredCount
return "\(count) \(count == 1 ? "Card" : "Cards") Unattempted"
return "\(count) \(count == 1 ? "Card" : "Cards") Unattempted"
}
}
@ -126,7 +127,10 @@ extension CardDeckContainerView.ViewModel {
func resetDeck() {
cards.forEach { $0.answerState = .unanswered }
self.isTimerActive = true
timeRemaining = roundDuration
isTimerActive = true
CurrentApp.coreDataManager.saveContexts()
}

View File

@ -14,6 +14,7 @@ struct CardDeckContainerView {
@ObservedObject var viewModel: ViewModel
@State private var isShowingEditView = false
@State private var isShowingSettingsView = false
}
@ -31,7 +32,7 @@ extension CardDeckContainerView: View {
// 📝
// It can be tricky having `CardDeckContainerView` contain the timer,
// because SwiftUI will re-render it on every tick.
//
//
// Perhaps it would be better to have `CountdownTimerView` own its timer
// and drive it with `timeRemaining`?
CountdownTimerView(
@ -53,6 +54,8 @@ extension CardDeckContainerView: View {
Spacer()
VStack {
self.settingsButton
Spacer()
if self.viewModel.isDeckEmpty || self.viewModel.isTimeExpired {
@ -82,6 +85,9 @@ extension CardDeckContainerView: View {
.onAppear {
self.viewModel.isTimerActive = true
}
.onDisappear {
self.viewModel.isTimerActive = false
}
}
}
@ -114,6 +120,20 @@ extension CardDeckContainerView {
}
private var settingsButton: some View {
Button(action: {
self.viewModel.pauseRound()
self.isShowingSettingsView = true
}) {
Image(systemName: "gear")
.padding()
.background(Color("Accent1"))
.clipShape(Circle())
.foregroundColor(.primary)
}
}
private var editDeckButton: some View {
Button(action: {
self.viewModel.pauseRound()