Last active
July 7, 2021 17:47
-
-
Save seyhunak/22a08418909dba7db44eb0daf5fadb8c to your computer and use it in GitHub Desktop.
HomeScreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // HomeRecap.swift | |
| // iosApp | |
| // | |
| import SwiftUI | |
| import shared | |
| struct HomeRecap: View { | |
| var balanceRecap: BalanceRecap | |
| var body: some View { | |
| VStack { | |
| // TODO: localize | |
| HStack { | |
| Text("€") | |
| .font(AppFonts.h5) | |
| Text("\(balanceRecap.totalBalance.formatTwoDigit())") | |
| .font(AppFonts.h3) | |
| } | |
| Text("Total Balance") | |
| .font(AppFonts.subtitle2) | |
| Spacer() | |
| .frame(height: AppMargins.medium) | |
| HStack { | |
| HStack { | |
| UpArrowCircleIcon(size: 28) | |
| .padding(.trailing, AppMargins.small) | |
| VStack(alignment: .leading) { | |
| Text("+\(balanceRecap.monthlyIncome.formatTwoDigit()) €") | |
| .font(AppFonts.h5) | |
| Text("Income") | |
| .font(AppFonts.subtitle2) | |
| } | |
| } | |
| Spacer() | |
| HStack { | |
| DownArrowCircleIcon(size: 28) | |
| .padding(.trailing, AppMargins.small) | |
| VStack(alignment: .trailing) { | |
| Text("-\(balanceRecap.monthlyExpenses.formatTwoDigit()) €") | |
| .font(AppFonts.h5) | |
| Text("Expenses") | |
| .font(AppFonts.subtitle2) | |
| } | |
| } | |
| } | |
| } | |
| .padding() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // HomeScreen.swift | |
| // iosApp | |
| import SwiftUI | |
| import shared | |
| struct HomeScreen: View { | |
| @EnvironmentObject var appState: AppState | |
| @StateObject var viewModel: HomeViewModel = HomeViewModel() | |
| @State private var showAddTransaction = false | |
| var body: some View { | |
| VStack { | |
| if (viewModel.homeModel is HomeModel.Loading) { | |
| Loader() | |
| } else if (viewModel.homeModel is HomeModel.HomeState) { | |
| let homeState = (viewModel.homeModel as! HomeModel.HomeState) | |
| HomeRecap(balanceRecap: homeState.balanceRecap) | |
| HeaderNavigator() | |
| if homeState.latestTransactions.isEmpty { | |
| Spacer() | |
| VStack { | |
| Text("¯\\_(ツ)_/¯") | |
| .padding(.bottom, AppMargins.small) | |
| Text("Your wallet is empty") | |
| } | |
| Spacer() | |
| } else { | |
| List { | |
| ForEach(homeState.latestTransactions, id: \.self) { transaction in | |
| TransactionCard(transaction: transaction) | |
| .listRowInsets(EdgeInsets()) | |
| .contextMenu { | |
| Button(action: { | |
| viewModel.deleteTransaction(transactionId: transaction.id) | |
| }) { | |
| Text("Delete") | |
| Image(systemName: "trash") | |
| } | |
| } | |
| } | |
| } | |
| .listStyle(PlainListStyle()) | |
| } | |
| } | |
| } | |
| .navigationTitle("My Wallet") | |
| .navigationBarItems(/*leading: refreshButton,*/ trailing: Button(action: { | |
| self.showAddTransaction.toggle() | |
| }) { | |
| // TODO: localize | |
| Image(systemName: "plus") | |
| } | |
| ) | |
| .sheet(isPresented: self.$showAddTransaction) { | |
| AddTransactionScreen(showSheet: self.$showAddTransaction) | |
| } | |
| .onAppear { | |
| self.viewModel.startObserving() | |
| }.onDisappear { | |
| self.viewModel.stopObserving() | |
| }.onReceive(self.appState.$reloadDatabase) { value in | |
| if value { | |
| self.viewModel.stopObserving() | |
| self.viewModel.startObserving() | |
| self.appState.reloadDatabase = false | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // HomeViewModel.swift | |
| // iosApp | |
| // | |
| import shared | |
| import Combine | |
| class HomeViewModel: ObservableObject { | |
| @Published var homeModel: HomeModel = HomeModel.Loading() | |
| private var subscriptions = Set<AnyCancellable>() | |
| private var homeUseCase: HomeUseCaseIos = DIContainer.instance.getHomeUseCase() | |
| func startObserving() { | |
| createPublisher(homeUseCase.getMoneySummary()) | |
| .eraseToAnyPublisher() | |
| .receive(on: DispatchQueue.global(qos: .userInitiated)) | |
| .sink(receiveCompletion: { completion in | |
| if case let .failure(error) = completion { | |
| self.homeModel = HomeModel.Error(message: "Something wrong here :(") | |
| } | |
| }, receiveValue: { genericResponse in | |
| onMainThread { | |
| self.homeModel = HomeModel.HomeState( | |
| balanceRecap: genericResponse.balanceRecap, | |
| latestTransactions: genericResponse.latestTransactions | |
| ) | |
| } | |
| }) | |
| .store(in: &self.subscriptions) | |
| } | |
| func deleteTransaction(transactionId: Int64) { | |
| homeUseCase.deleteTransaction(transactionId: transactionId) | |
| } | |
| func stopObserving() { | |
| self.useCase?.onDestroy() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment