Skip to content

Commit

Permalink
Refactor yet again how collection progress is published
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed May 25, 2020
1 parent dbc0167 commit 112349a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
4 changes: 4 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
696B454824791EDE00E16252 /* islanddevelopment in Resources */ = {isa = PBXBuildFile; fileRef = 696B454724791EDE00E16252 /* islanddevelopment */; };
696B454A247A633D00E16252 /* TodayMusicPlayerSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 696B4549247A633D00E16252 /* TodayMusicPlayerSection.swift */; };
696B454C247A948A00E16252 /* MusicPlayerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 696B454B247A948A00E16252 /* MusicPlayerManager.swift */; };
69867DF1247C136900C6A759 /* CollectionProgressRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69867DF0247C136900C6A759 /* CollectionProgressRowViewModel.swift */; };
69CFCAC9245ADC780059C067 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CFCAC8245ADC780059C067 /* AppDelegate.swift */; };
69CFCACD245ADC7A0059C067 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 69CFCACC245ADC7A0059C067 /* Assets.xcassets */; };
69CFCAD0245ADC7A0059C067 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 69CFCACF245ADC7A0059C067 /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -268,6 +269,7 @@
697E63FA2449DB0C008FB710 /* VillagerDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VillagerDetailView.swift; sourceTree = "<group>"; };
697E63FC244AD42D008FB710 /* CategoryRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryRowView.swift; sourceTree = "<group>"; };
697E63FF244AD66E008FB710 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
69867DF0247C136900C6A759 /* CollectionProgressRowViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionProgressRowViewModel.swift; sourceTree = "<group>"; };
69875F4F24652A4700B1D46C /* Sheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sheet.swift; sourceTree = "<group>"; };
69875F5424654EB900B1D46C /* UserListFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserListFormView.swift; sourceTree = "<group>"; };
69875F5624654F7100B1D46C /* UserListFormViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserListFormViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -653,6 +655,7 @@
697E63F62449A7BA008FB710 /* VillagersViewModel.swift */,
6941F3872452BDDD00EC2E02 /* CategoriesSearchViewModel.swift */,
695D45FA24671B1800B5C313 /* VillagerDetailViewModel.swift */,
69867DF0247C136900C6A759 /* CollectionProgressRowViewModel.swift */,
);
path = viewModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -1020,6 +1023,7 @@
69157B932471A5A1005B9002 /* TabbarView.swift in Sources */,
69157B942471A5A1005B9002 /* ActivityControllerView.swift in Sources */,
69157B952471A5A1005B9002 /* ItemsCrosslineSectionView.swift in Sources */,
69867DF1247C136900C6A759 /* CollectionProgressRowViewModel.swift in Sources */,
69157B962471A5A1005B9002 /* TurnipsChartTopLegendView.swift in Sources */,
69157B972471A5A1005B9002 /* TurnipsChartView.swift in Sources */,
696B454C247A948A00E16252 /* MusicPlayerManager.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public class UserCollection: ObservableObject {
}

// MARK: - Items management
public func itemsIn(category: Category, items: [Item]) -> Int {
let allItems = Items.shared.categories[category] ?? []
let inCollection = items.count(where: { allItems.contains($0) && !$0.name.contains("(fake)") })
return inCollection
}

public func itemsIn(category: Category) -> Int {
let items = Items.shared.categories[category] ?? []
var caught = self.critters.count(where: { items.contains($0) } )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// CollectionProgressRowViewModel.swift
// ACHNBrowserUI
//
// Created by Thomas Ricouard on 25/05/2020.
// Copyright © 2020 Thomas Ricouard. All rights reserved.
//

import Foundation
import Combine
import Backend

class CollectionProgressRowViewModel: ObservableObject {
let category: Backend.Category

@Published var total: Int = 0
@Published var inCollection: Int = 0

private var cancellable: AnyCancellable?

init(category: Backend.Category) {
self.category = category
if category == .fish || category == .bugs || category == .fossils {
cancellable = UserCollection.shared.$critters
.subscribe(on: DispatchQueue.global())
.removeDuplicates()
.receive(on: DispatchQueue.global())
.sink(receiveValue: { [weak self] items in
let inCollection = UserCollection.shared.itemsIn(category: category, items: items)
let total = Items.shared.categories[category]?.count ?? 0
self?.set(total: total, inCollection: inCollection)
})
} else {
cancellable = UserCollection.shared.$items
.subscribe(on: DispatchQueue.global())
.removeDuplicates()
.receive(on: DispatchQueue.global())
.sink(receiveValue: { [weak self] items in
let inCollection = UserCollection.shared.itemsIn(category: category, items: items)
let total = Items.shared.categories[category]?.filter({ !$0.name.contains("(fake)") }).count ?? 0
self?.set(total: total, inCollection: inCollection)
})
}
}

private func set(total: Int, inCollection: Int) {
DispatchQueue.main.async {
if total != self.total {
self.total = total
}
if inCollection != self.inCollection {
self.inCollection = inCollection
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import Backend
import UI

struct CollectionProgressRow: View {
@EnvironmentObject private var items: Items
@EnvironmentObject private var collection: UserCollection

@State private var inCollection = 0
@State private var total = 0

let category: Backend.Category
let barHeight: CGFloat

@ObservedObject private var viewModel: CollectionProgressRowViewModel

init(category: Backend.Category, barHeight: CGFloat) {
self.category = category
self.viewModel = CollectionProgressRowViewModel(category: category)
self.barHeight = barHeight
}

var body: some View {
HStack {
Image(category.iconName())
Expand All @@ -28,32 +30,18 @@ struct CollectionProgressRow: View {
.frame(height: barHeight + 12)

Group {
ProgressView(progress: CGFloat(inCollection) / CGFloat(total),
ProgressView(progress: CGFloat(viewModel.inCollection) / CGFloat(viewModel.total),
trackColor: .acText,
progressColor: .acHeaderBackground,
height: barHeight)
}
.frame(height: self.barHeight)
.frame(height: barHeight)

Text("\(inCollection) / \(total)")
Text("\(viewModel.inCollection) / \(viewModel.total)")
.font(Font.system(size: 12,
weight: Font.Weight.semibold,
design: Font.Design.rounded).monospacedDigit())
.foregroundColor(.acText)
}.onAppear {
DispatchQueue.global().async {
let caught = self.collection.itemsIn(category: self.category)
var total = 0
if self.category == .art {
total = self.items.categories[self.category]?.filter({ !$0.name.contains("(fake)") }).count ?? 0
} else {
total = self.items.categories[self.category]?.count ?? 0
}
DispatchQueue.main.async {
self.inCollection = caught
self.total = total
}
}
}
}
}
Expand All @@ -62,8 +50,6 @@ struct CollectionProgressRow_Previews: PreviewProvider {
static var previews: some View {
List {
CollectionProgressRow(category: .housewares, barHeight: 12)
.environmentObject(UserCollection.shared)
.environmentObject(Items.shared)
}
}
}

0 comments on commit 112349a

Please sign in to comment.