Skip to content

Commit

Permalink
Factor out view for a single project in list and add logic to change …
Browse files Browse the repository at this point in the history
…the selected default payer
  • Loading branch information
Hustenbonbon committed Feb 8, 2023
1 parent 0a22072 commit b2c0783
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
4 changes: 4 additions & 0 deletions PayForMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
654754C8252899FF00A82EB6 /* FancyButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 654754C7252899FF00A82EB6 /* FancyButton.swift */; };
654754E42528B29F00A82EB6 /* AddProjectManuallyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 654754E32528B29F00A82EB6 /* AddProjectManuallyTests.swift */; };
654754F22528BB7700A82EB6 /* ShareProjectQRCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 654754F12528BB7600A82EB6 /* ShareProjectQRCode.swift */; };
655DD2AE2993DACB00E06675 /* ProjectListEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 655DD2AD2993DACB00E06675 /* ProjectListEntry.swift */; };
65662A0323D7320000303207 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65662A0223D7320000303207 /* AppDelegate.swift */; };
65662A0523D7320000303207 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65662A0423D7320000303207 /* SceneDelegate.swift */; };
65662A0723D7320000303207 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65662A0623D7320000303207 /* ContentView.swift */; };
Expand Down Expand Up @@ -104,6 +105,7 @@
654754E32528B29F00A82EB6 /* AddProjectManuallyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddProjectManuallyTests.swift; sourceTree = "<group>"; };
654754E52528B29F00A82EB6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
654754F12528BB7600A82EB6 /* ShareProjectQRCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareProjectQRCode.swift; sourceTree = "<group>"; };
655DD2AD2993DACB00E06675 /* ProjectListEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectListEntry.swift; sourceTree = "<group>"; };
656629FF23D7320000303207 /* PayForMe.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PayForMe.app; sourceTree = BUILT_PRODUCTS_DIR; };
65662A0223D7320000303207 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
65662A0423D7320000303207 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -324,6 +326,7 @@
6596D4E323ED9C8B0079DD5B /* Projects */ = {
isa = PBXGroup;
children = (
655DD2AD2993DACB00E06675 /* ProjectListEntry.swift */,
6523FC55255818E000BCD843 /* QRCodes */,
65662A2523D8546900303207 /* ProjectList.swift */,
489995D923F6EC6F008B7E38 /* OnboardingView.swift */,
Expand Down Expand Up @@ -566,6 +569,7 @@
658FC54B25272639004A49AA /* AddProjectQRView.swift in Sources */,
481FB4FD23EAD78F003BD108 /* AddProjectManualViewModel.swift in Sources */,
65662A2423D83ED100303207 /* StorageService.swift in Sources */,
655DD2AE2993DACB00E06675 /* ProjectListEntry.swift in Sources */,
656E8CB823D9FDA300B3ED10 /* BillListViewModel.swift in Sources */,
65C9E2D023E1B96B00814B37 /* BalanceViewModel.swift in Sources */,
65662A0723D7320000303207 /* ContentView.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions PayForMe/Model/Person.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ let previewPersons = [
3: Person(id: 3, weight: 1, name: "Bisasam", activated: true),
4: Person(id: 4, weight: 1, name: "Glumanda", activated: true),
]
let previewManyPersons = [
1: previewPerson,
2: Person(id: 2, weight: 1, name: "Schiggy", activated: true, color: PersonColor(r: 60, g: 110, b: 186)),
3: Person(id: 3, weight: 1, name: "Bisasam", activated: true),
4: Person(id: 4, weight: 1, name: "Glumanda", activated: true),
5: Person(id: 5, weight: 1, name: "Fukano", activated: true),
6: Person(id: 6, weight: 1, name: "Relaxo", activated: true),
]
32 changes: 1 addition & 31 deletions PayForMe/Views/Projects/ProjectList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ProjectList: View {
VStack {
List {
ForEach(manager.projects) { project in
listRow(project: project)
ProjectListEntry(project: project, currentProject: manager.currentProject, shareProject: self.$shareProject)
}
.onDelete(perform: deleteProject)
}
Expand Down Expand Up @@ -56,33 +56,6 @@ struct ProjectList: View {
.navigationViewStyle(StackNavigationViewStyle())
}

private func listRow(project: Project) -> some View {
Button(action: {
self.manager.setCurrentProject(project)
}, label: {
HStack {
VStack(alignment: .leading) {
Text(project.name)
Text(project.backend == .cospend ? "Cospend" : "iHateMoney").font(.caption).foregroundColor(Color.gray)
}
if self.manager.currentProject == project {
Image(systemName: "checkmark").padding(.trailing)
}
Spacer()
if project.backend == .cospend {
Button(action: {
self.shareProject = project
}, label: {
HStack(spacing: 5) {
Image(systemName: "square.and.arrow.up")
Image(systemName: "qrcode")
}
})
}
}
})
}

private enum AddingProjectMethod: Int, CaseIterable, Identifiable {
var id: Int {
switch self {
Expand Down Expand Up @@ -110,9 +83,6 @@ struct ProjectList: View {

struct ServerList_Previews: PreviewProvider {
static var previews: some View {
try! ProjectManager.shared.addProject(previewProjects[0])
try! ProjectManager.shared.addProject(previewProjects[1])
try! ProjectManager.shared.addProject(previewProjects[2])
return ProjectList()
}
}
76 changes: 76 additions & 0 deletions PayForMe/Views/Projects/ProjectListEntry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// ProjectListEntry.swift
// PayForMe
//
// Created by Max Tharr on 08.02.23.
// Copyright © 2023 Mayflower GmbH. All rights reserved.
//

import SwiftUI

struct ProjectListEntry: View {
let project: Project
let manager = ProjectManager.shared
let currentProject: Project

@Binding var shareProject: Project?

@State var edit = false
@State var me = 0

var body: some View {
Button(action: {
self.manager.setCurrentProject(project)
}, label: {
VStack {
HStack {
VStack(alignment: .leading) {
Text(project.name)
Text(project.backend == .cospend ? "Cospend" : "iHateMoney").font(.caption).foregroundColor(Color.gray)
}
Spacer()
if project == currentProject {
Button(action: {
self.edit.toggle()
}, label: {
Image(systemName: "pencil")
})
.padding(.trailing, 5)
}
if project.backend == .cospend {
Button(action: {
self.shareProject = project
}, label: {
HStack(spacing: 5) {
Image(systemName: "square.and.arrow.up")
Image(systemName: "qrcode")
}
})
}
}
.foregroundColor(self.currentProject == project ? .accentColor : .primary)
}
if edit {
WhoPaidView(members: Array(project.members.values), selectedPayer: self.$me)
.animation(.spring(), value: edit)
}
})
.onAppear {
me = project.me ?? 0
}
.onChange(of: me) { newValue in
project.me = newValue
manager.updateProject(project: project)
edit.toggle()
}
}
}

struct ProjectListEntry_Previews: PreviewProvider {
static var previews: some View {
previewProject.members = previewManyPersons
return List {
ProjectListEntry(project: previewProject, currentProject: previewProject, shareProject: .constant(nil))
}
}
}

0 comments on commit b2c0783

Please sign in to comment.