Group transactions by date and show description in list

This commit is contained in:
William Brawner 2021-11-30 06:43:03 -07:00
parent fc63684993
commit 5fd73bf0bc
5 changed files with 65 additions and 12 deletions

View file

@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 52;
objects = {
/* Begin PBXBuildFile section */
@ -47,6 +47,7 @@
543ECE42233E82A40018A9D9 /* AuthenticationDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 543ECE41233E82A40018A9D9 /* AuthenticationDataStore.swift */; };
8043EB84271F26ED00498E73 /* CategoryDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8043EB83271F26ED00498E73 /* CategoryDetailsView.swift */; };
806C7850272B700B00FA1375 /* TwigsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 806C784F272B700B00FA1375 /* TwigsApp.swift */; };
8094A9C327567CAC006C6C62 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = 8094A9C227567CAC006C6C62 /* Collections */; };
809B942327221EC800B1DAE2 /* CategoryFormSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 809B942227221EC800B1DAE2 /* CategoryFormSheet.swift */; };
/* End PBXBuildFile section */
@ -124,6 +125,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
8094A9C327567CAC006C6C62 /* Collections in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -310,6 +312,9 @@
dependencies = (
);
name = Twigs;
packageProductDependencies = (
8094A9C227567CAC006C6C62 /* Collections */,
);
productName = Budget;
productReference = 28AC94EA233C373900BFB70A /* Twigs.app */;
productType = "com.apple.product-type.application";
@ -383,6 +388,9 @@
es,
);
mainGroup = 28AC94E1233C373900BFB70A;
packageReferences = (
8094A9C127567CAC006C6C62 /* XCRemoteSwiftPackageReference "swift-collections" */,
);
productRefGroup = 28AC94EB233C373900BFB70A /* Products */;
projectDirPath = "";
projectRoot = "";
@ -817,6 +825,25 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
8094A9C127567CAC006C6C62 /* XCRemoteSwiftPackageReference "swift-collections" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-collections.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
8094A9C227567CAC006C6C62 /* Collections */ = {
isa = XCSwiftPackageProductDependency;
package = 8094A9C127567CAC006C6C62 /* XCRemoteSwiftPackageReference "swift-collections" */;
productName = Collections;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 28AC94E2233C373900BFB70A /* Project object */;
}

View file

@ -2,6 +2,6 @@
<Workspace
version = "1.0">
<FileRef
location = "self:/Users/billy/Projects/Budget/Twigs.xcodeproj">
location = "self:">
</FileRef>
</Workspace>

View file

@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
"version": "1.0.2"
}
}
]
},
"version": 1
}

View file

@ -8,11 +8,12 @@
import Foundation
import Combine
import Collections
class TransactionDataStore: ObservableObject {
private var currentRequest: AnyCancellable? = nil
private var sumRequests: [String:AnyCancellable] = [:]
@Published var transactions: [String:Result<[Transaction], NetworkError>] = ["": .failure(.loading)]
@Published var transactions: [String:Result<OrderedDictionary<String, [Transaction]>, NetworkError>] = ["": .failure(.loading)]
@Published var transaction: Result<Transaction, NetworkError> = .failure(.unknown)
@ -45,7 +46,8 @@ class TransactionDataStore: ObservableObject {
self.transactions[requestId] = .failure(error)
}
}, receiveValue: { (transactions) in
self.transactions[requestId] = .success(transactions)
let groupedTransactions = OrderedDictionary<String,[Transaction]>(grouping: transactions, by: { $0.date.toLocaleString() })
self.transactions[requestId] = .success(groupedTransactions)
})
return requestId

View file

@ -18,9 +18,15 @@ struct TransactionListView: View {
var body: some View {
switch transactionDataStore.transactions[requestId] {
case .success(let transactions):
Section {
List(transactions) { transaction in
TransactionListItemView(transaction)
List {
ForEach(transactions.keys, id: \.self) { (key: String) in
Group {
Section(header: Text(key)) {
ForEach(transactions[key]!) { transaction in
TransactionListItemView(transaction)
}
}
}
}
}
.sheet(isPresented: $isAddingTransaction, content: {
@ -75,11 +81,13 @@ struct TransactionListItemView: View {
Text(verbatim: transaction.title)
.lineLimit(1)
.font(.headline)
Text(verbatim: transaction.date.toLocaleString())
.lineLimit(1)
.font(.subheadline)
.foregroundColor(.secondary)
.multilineTextAlignment(.trailing)
if let description = transaction.description?.trimmingCharacters(in: CharacterSet([" "])), !description.isEmpty {
Text(verbatim: description)
.lineLimit(1)
.font(.subheadline)
.foregroundColor(.secondary)
.multilineTextAlignment(.trailing)
}
}
Spacer()
VStack(alignment: .trailing) {