Skip to content

Commit

Permalink
Merge pull request Dimillian#335 from renaudjenny/fix-334-today-birth…
Browse files Browse the repository at this point in the history
…day-infinite-loading

fix(Today Birthdays): handle when there is no birthday this day
  • Loading branch information
Dimillian authored Nov 10, 2020
2 parents 6077d19 + 22a3e49 commit 0595dde
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
16 changes: 11 additions & 5 deletions ACHNBrowserUI/ACHNBrowserUI/viewModels/VillagersViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class VillagersViewModel: ObservableObject {
@Published var searchText = ""
@Published var todayBirthdays: [Villager] = []
@Published var sortedVillagers: [Villager] = []
@Published var isLoading = false

// MARK: - Private properties

Expand All @@ -34,25 +35,28 @@ class VillagersViewModel: ObservableObject {
}

private let collection: UserCollection
private let currentDate: Date
private var villagersCancellable: AnyCancellable?

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

// MARK: - Life cycle

init(collection: UserCollection = .shared) {
init(collection: UserCollection = .shared, currentDate: Date) {
self.collection = collection
self.currentDate = currentDate

self.villagersCancellable = Publishers.CombineLatest(collection.$villagers, collection.$residents)
.sink { [weak self] (villagers, residents) in
self?.residents = residents
self?.liked = villagers
}


isLoading = false
searchCancellable = $searchText
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
Expand All @@ -69,8 +73,10 @@ class VillagersViewModel: ObservableObject {
.map{ $0.map{ $0.1}.sorted(by: { $0.id > $1.id }) }
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] in
self?.villagers = $0
self?.todayBirthdays = $0.filter( { $0.birthday == self?.today })
guard let self = self else { return }
self.villagers = $0
self.todayBirthdays = $0.filter( { $0.birthday == self.today })
self.isLoading = false
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import Backend
import UI

struct TodayBirthdaysSection: View {
@StateObject private var viewModel = VillagersViewModel()
@Environment(\.currentDate) private static var currentDate
@StateObject private var viewModel = VillagersViewModel(currentDate: Self.currentDate)

var headerText: String {
viewModel.todayBirthdays.count > 1 ? "Today's Birthdays" : "Today's Birthday"
}

var body: some View {
Group {
if viewModel.todayBirthdays.isEmpty {
if viewModel.isLoading {
RowLoadingView()
} else if viewModel.todayBirthdays.isEmpty {
Text("No Birthday today...")
.font(.system(.caption, design: .rounded))
.fontWeight(.bold)
.foregroundColor(.acText)
} else {
ForEach(viewModel.todayBirthdays, id: \.id) { villager in
NavigationLink(destination: LazyView(VillagerDetailView(villager: villager))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ struct VillagersListView: View {
private enum Filter: String, CaseIterable {
case all, liked, residents
}

@StateObject var viewModel = VillagersViewModel()

@Environment(\.currentDate) private static var currentDate
@StateObject var viewModel = VillagersViewModel(currentDate: Self.currentDate)
@State private var currentFilter = Filter.all

var currentVillagers: [Villager] {
Expand Down

0 comments on commit 0595dde

Please sign in to comment.