Skip to content

Commit

Permalink
Fix a crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed May 27, 2020
1 parent 99dc765 commit 710afc7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@
CODE_SIGN_ENTITLEMENTS = ACHNBrowserUI/ACHNBrowserUI.entitlements;
CODE_SIGN_IDENTITY = "Apple Development: Thomas Ricouard (7MB55D6BJ5)";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 052752020;
CURRENT_PROJECT_VERSION = 052772020;
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1252,7 +1252,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 052752020;
CURRENT_PROJECT_VERSION = 052772020;
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
ENABLE_PREVIEWS = YES;
Expand Down
34 changes: 18 additions & 16 deletions ACHNBrowserUI/ACHNBrowserUI/viewModels/VillagersViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ import Backend
class VillagersViewModel: ObservableObject {
private static var cachedVillagers: [Villager] = []

@Published var villagers: [Villager] = [] {
didSet {
Self.cachedVillagers = villagers
let formatter = DateFormatter()
formatter.dateFormat = "d/M"
let today = formatter.string(from: Date())
todayBirthdays = villagers.filter( { $0.birthday == today })
}
}

@Published var villagers: [Villager] = []
@Published var searchResults: [Villager] = []
@Published var searchText = ""
@Published var todayBirthdays: [Villager] = []
Expand All @@ -36,17 +27,24 @@ class VillagersViewModel: ObservableObject {
}
}

private var today: String {
let formatter = DateFormatter()
formatter.dateFormat = "d/M"
return formatter.string(from: Date())
}

init() {
searchCancellable = $searchText
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
.filter { !$0.isEmpty }
.map(villagers(with:))
.sink(receiveValue: { [weak self] in self?.searchResults = $0 })
}

func fetch() {
villagers = Self.cachedVillagers

if villagers.isEmpty {
villagers = Self.cachedVillagers
todayBirthdays = villagers.filter( { $0.birthday == today } )
}
if !villagers.isEmpty {
return
}
Expand All @@ -58,9 +56,13 @@ class VillagersViewModel: ObservableObject {
.subscribe(on: DispatchQueue.global())
.map{ $0.map{ $0.1}.sorted(by: { $0.id > $1.id }) }
.receive(on: DispatchQueue.main)
.assign(to: \.villagers, on: self)
.sink(receiveValue: { [weak self] in
Self.cachedVillagers = $0
self?.villagers = $0
self?.todayBirthdays = $0.filter( { $0.birthday == self?.today })
})
}

private func villagers(with string: String) -> [Villager] {
villagers.filter {
$0.localizedName.lowercased().contains(string.lowercased()) == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ struct TodayBirthdaysSection: View {
RowLoadingView(isLoading: .constant(true))
} else {
ForEach(viewModel.todayBirthdays, id: \.id) { villager in
NavigationLink(destination: VillagerDetailView(villager: villager)) {
NavigationLink(destination: LazyView(VillagerDetailView(villager: villager))) {
self.makeCell(for: villager)
}
}
.padding(.vertical)
}
}.onAppear(perform: viewModel.fetch)
}
}

private func makeCell(for villager: Villager) -> some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct VillagersListView: View {
RowLoadingView(isLoading: .constant(true))
}
}
}.onAppear(perform: viewModel.fetch)
}
}
}

Expand Down

0 comments on commit 710afc7

Please sign in to comment.