Skip to content

Commit

Permalink
Fix #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed Jun 7, 2020
1 parent 63f81c5 commit 8ad2a60
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 58 deletions.
12 changes: 8 additions & 4 deletions ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
696B454C247A948A00E16252 /* MusicPlayerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 696B454B247A948A00E16252 /* MusicPlayerManager.swift */; };
69855149248A281300F7BDBA /* TabbarViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69855148248A281300F7BDBA /* TabbarViewModel.swift */; };
6985514C248A297400F7BDBA /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6985514B248A297400F7BDBA /* PlayerView.swift */; };
6985514E248CC31A00F7BDBA /* ResidentButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6985514D248CC31A00F7BDBA /* ResidentButton.swift */; };
69867DF1247C136900C6A759 /* CollectionProgressRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69867DF0247C136900C6A759 /* CollectionProgressRowViewModel.swift */; };
69CCD28A247D9FD000DF381B /* ItemBellsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CCD289247D9FD000DF381B /* ItemBellsView.swift */; };
69CCD28C247DA4D100DF381B /* ItemStyleBadgeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CCD28B247DA4D100DF381B /* ItemStyleBadgeView.swift */; };
Expand Down Expand Up @@ -311,6 +312,7 @@
697E63FF244AD66E008FB710 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
69855148248A281300F7BDBA /* TabbarViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabbarViewModel.swift; sourceTree = "<group>"; };
6985514B248A297400F7BDBA /* PlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerView.swift; sourceTree = "<group>"; };
6985514D248CC31A00F7BDBA /* ResidentButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResidentButton.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>"; };
Expand Down Expand Up @@ -801,6 +803,7 @@
69875F4F24652A4700B1D46C /* Sheet.swift */,
693E2BBD2465B65D00B85CB8 /* RowLoadingView.swift */,
4C16FB40247AC9B0009F24E3 /* GridStack.swift */,
6985514D248CC31A00F7BDBA /* ResidentButton.swift */,
);
path = shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -1234,6 +1237,7 @@
69157BC62471A5A1005B9002 /* SubscribeView.swift in Sources */,
69157BC72471A5A1005B9002 /* TurnipsFormView.swift in Sources */,
69157BC82471A5A1005B9002 /* TurnipsChartMinMaxCurves.swift in Sources */,
6985514E248CC31A00F7BDBA /* ResidentButton.swift in Sources */,
69157BC92471A5A1005B9002 /* TurnipsChartGrid.swift in Sources */,
4C7F555D248B91C80089F26C /* TodayVillagerVisitsSection.swift in Sources */,
69157BCA2471A5A1005B9002 /* CollectionRowView.swift in Sources */,
Expand Down Expand Up @@ -1324,7 +1328,7 @@
CODE_SIGN_ENTITLEMENTS = ACHNBrowserUI/ACHNBrowserUI.entitlements;
CODE_SIGN_IDENTITY = "Apple Development: Thomas Ricouard (7MB55D6BJ5)";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 060532020;
CURRENT_PROJECT_VERSION = 060612020;
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
ENABLE_PREVIEWS = YES;
Expand All @@ -1334,7 +1338,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.6;
MARKETING_VERSION = 1.6.7;
PRODUCT_BUNDLE_IDENTIFIER = com.thomasricouard.ACNH;
PRODUCT_NAME = "AC Helper";
PROVISIONING_PROFILE_SPECIFIER = "AC Dev";
Expand All @@ -1355,7 +1359,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 060532020;
CURRENT_PROJECT_VERSION = 060612020;
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
ENABLE_PREVIEWS = YES;
Expand All @@ -1365,7 +1369,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.6;
MARKETING_VERSION = 1.6.7;
PRODUCT_BUNDLE_IDENTIFIER = com.thomasricouard.ACNH;
PRODUCT_NAME = "AC Helper";
PROVISIONING_PROFILE_SPECIFIER = "AC Dev";
Expand Down
41 changes: 27 additions & 14 deletions ACHNBrowserUI/ACHNBrowserUI/viewModels/LikeButtonViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import SwiftUI
import Combine
import Backend

class LikeButtonViewModel: ObservableObject {
Expand All @@ -19,27 +20,39 @@ class LikeButtonViewModel: ObservableObject {
@Published var isInCollection = false

private let collection: UserCollection
private var cancellable: AnyCancellable?

init(item: Item? = nil, variant: Variant? = nil, villager: Villager? = nil, collection: UserCollection = .shared) {
init(item: Item? = nil,
variant: Variant? = nil,
villager: Villager? = nil,
collection: UserCollection = .shared) {
self.item = item
self.variant = variant
self.villager = villager
self.collection = collection
self.isInCollection = computeIsInCollection()
}

private func computeIsInCollection() -> Bool {
if let item = item {
if let variant = variant {
return collection.variantIn(item: item, variant: variant)
}
return collection.items.contains(item) || collection.critters.contains(item)
} else if let villager = villager {
return collection.villagers.contains(villager)
self.cancellable = Publishers.CombineLatest4(collection.$items,
collection.$critters,
collection.$variants,
collection.$villagers)
.sink{ [weak self] items, critters, variants, villagers in
guard let self = self else { return }
if let item = self.item {
if let variant = variant {
self.isInCollection = collection.variantIn(item: item,
variant: variant)
return
}
self.isInCollection = items.contains(item) || critters.contains(item)
return
} else if let villager = villager {
self.isInCollection = villagers.contains(villager)
return
}

}
return false
}

func toggleCollection() -> Bool {
var added = false
if let item = self.item {
Expand All @@ -49,13 +62,13 @@ class LikeButtonViewModel: ObservableObject {
default:
if let variant = self.variant {
added = self.collection.toggleVariant(item: item, variant: variant)
return added
}
added = self.collection.toggleItem(item: item)
}
} else if let villager = self.villager {
added = self.collection.toggleVillager(villager: villager)
}
isInCollection = computeIsInCollection()
return added
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,9 @@ public class VillagerDetailViewModel: ObservableObject {
@Published var villagerItems: [Item]?
@Published var preferredItems: [Item]?
@Published var likes: [String]?
@Published var isResident: Bool = false

private let collection: UserCollection
private var residentsCancellable: AnyCancellable?

init(villager: Villager, collection: UserCollection = .shared) {
self.villager = villager
self.collection = collection

residentsCancellable = collection.$residents.sink
{ [weak self] in
self?.isResident = $0.contains(villager)
}
}

func toggleResident() {
_ = collection.toggleResident(villager: villager)
init(villager: Villager) {
self.villager = villager
}

func fetchItems() {
Expand Down
33 changes: 33 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI/views/shared/ResidentButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// ResidentButton.swift
// ACHNBrowserUI
//
// Created by Thomas Ricouard on 07/06/2020.
// Copyright © 2020 Thomas Ricouard. All rights reserved.
//

import SwiftUI
import Backend

struct ResidentButton: View {
@EnvironmentObject private var collection: UserCollection
let villager: Villager

var body: some View {
Button(action: {
_ = self.collection.toggleResident(villager: self.villager)
FeedbackGenerator.shared.triggerNotification(type: .success)
}) {
Image(systemName: collection.residents.contains(villager) ? "house.fill" : "house")
.foregroundColor(.acTabBarBackground)
.imageScale(.medium)
}
.buttonStyle(BorderlessButtonStyle())
}
}

struct ResidentButton_Previews: PreviewProvider {
static var previews: some View {
ResidentButton(villager: static_villager).environmentObject(UserCollection.shared)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ struct VillagerDetailView: View {
}
.safeHoverEffectBarItem(position: .trailing)
}

private var residentButton: some View {
Button(action: {
self.viewModel.toggleResident()
FeedbackGenerator.shared.triggerNotification(type: .success)
}) {
Image(systemName: viewModel.isResident ? "house.fill" : "house")
.foregroundColor(.acTabBarBackground)
}
.safeHoverEffectBarItem(position: .trailing)
}


private var navButtons: some View {
HStack(spacing: 8) {
residentButton
ResidentButton(villager: villager)
.safeHoverEffectBarItem(position: .trailing)
LikeButtonView(villager: villager)
.safeHoverEffectBarItem(position: .trailing)
shareButton.padding(.top, -6)
Expand Down Expand Up @@ -109,7 +99,12 @@ struct VillagerDetailView: View {
.padding()
makeInfoCell(title: "Personality", value: villager.personality).padding()
makeInfoCell(title: "Birthday", value: villager.formattedBirthday ?? "Unknown").padding()
makeInfoCell(title: "Like", value: viewModel.likes?.map{ $0.capitalized }.joined(separator: ", ") ?? "Unknown").padding()
makeInfoCell(title: "Like",
value: viewModel.likes?
.map{ $0.capitalized }
.sorted()
.joined(separator: ", ") ?? "Unknown")
.padding()
makeInfoCell(title: "Species", value: villager.species).padding()
makeInfoCell(title: "Gender", value: villager.gender).padding()
makeInfoCell(title: "Catch phrase", value: villager.localizedCatchPhrase.capitalized).padding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct VillagerRowView: View {

// MARK: - Properties

@EnvironmentObject private var collection: UserCollection
private let villager: Villager
private let style: Style
private let action: Action
Expand All @@ -44,15 +43,7 @@ struct VillagerRowView: View {
if action == .like {
LikeButtonView(villager: villager)
} else {
Button(action: {
_ = self.collection.toggleResident(villager: self.villager)
FeedbackGenerator.shared.triggerNotification(type: .success)
}) {
Image(systemName: collection.residents.contains(villager) ? "house.fill" : "house")
.foregroundColor(.acTabBarBackground)
.imageScale(.medium)
}
.buttonStyle(BorderlessButtonStyle())
ResidentButton(villager: villager)
}
}
}
Expand Down

0 comments on commit 8ad2a60

Please sign in to comment.