twigs-ios/Twigs/Recurring Transactions/RecurringTransactionsListView.swift

87 lines
2.7 KiB
Swift
Raw Normal View History

//
// RecurringTransactionView.swift
// Twigs
//
// Created by William Brawner on 12/6/21.
// Copyright © 2021 William Brawner. All rights reserved.
//
import SwiftUI
2022-01-03 17:56:43 +00:00
import TwigsCore
struct RecurringTransactionsListView: View {
@EnvironmentObject var dataStore: DataStore
var body: some View {
2022-01-03 17:56:43 +00:00
InlineLoadingView(
data: $dataStore.recurringTransactions,
action: { await self.dataStore.getRecurringTransactions() },
2022-01-03 17:56:43 +00:00
errorTextLocalizedStringKey: "Failed to load recurring transactions"
) { (transactions: [RecurringTransaction]) in
List {
ForEach(transactions) { transaction in
RecurringTransactionsListItemView(transaction)
}
}
}
}
}
#if DEBUG
struct RecurringTransactionView_Previews: PreviewProvider {
static var previews: some View {
RecurringTransactionsListView()
}
}
#endif
struct RecurringTransactionsListItemView: View {
@EnvironmentObject var dataStore: DataStore
let transaction: RecurringTransaction
init (_ transaction: RecurringTransaction) {
self.transaction = transaction
}
var body: some View {
NavigationLink(
tag: transaction,
selection: $dataStore.selectedRecurringTransaction,
destination: {
RecurringTransactionDetailsView()
.navigationBarTitle("details", displayMode: .inline)
}
) {
HStack {
VStack(alignment: .leading) {
Text(verbatim: transaction.title)
.lineLimit(1)
.font(.headline)
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) {
Text(verbatim: transaction.amount.toCurrencyString())
.foregroundColor(transaction.expense ? .red : .green)
.multilineTextAlignment(.trailing)
}
.padding(.leading)
}.padding(5.0)
}
}
}
#if DEBUG
struct RecurringTransactionsListItemView_Previews: PreviewProvider {
static var previews: some View {
RecurringTransactionsListItemView(MockRecurringTransactionRepository.transaction)
}
}
#endif