Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GoMoney.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
083EA336290659660079605F /* TransactionTag.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083EA335290659660079605F /* TransactionTag.swift */; };
083EA339290676EB0079605F /* StatViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083EA338290676EB0079605F /* StatViewModel.swift */; };
083EA33B2906933A0079605F /* StatLineChartCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083EA33A2906933A0079605F /* StatLineChartCell.swift */; };
083EDBAE292A3ED50058F5D7 /* GMAuthViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083EDBAD292A3ED40058F5D7 /* GMAuthViewController.swift */; };
085F7526291215B80094A026 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 085F7525291215B80094A026 /* SettingsViewController.swift */; };
085F752A2912490E0094A026 /* SettingsTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 085F75292912490E0094A026 /* SettingsTableViewCell.swift */; };
085F752C29124CEC0094A026 /* BlockerToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 085F752B29124CEC0094A026 /* BlockerToggle.swift */; };
Expand Down Expand Up @@ -203,6 +204,7 @@
083EA335290659660079605F /* TransactionTag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionTag.swift; sourceTree = "<group>"; };
083EA338290676EB0079605F /* StatViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatViewModel.swift; sourceTree = "<group>"; };
083EA33A2906933A0079605F /* StatLineChartCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatLineChartCell.swift; sourceTree = "<group>"; };
083EDBAD292A3ED40058F5D7 /* GMAuthViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GMAuthViewController.swift; sourceTree = "<group>"; };
085F7525291215B80094A026 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
085F75292912490E0094A026 /* SettingsTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsTableViewCell.swift; sourceTree = "<group>"; };
085F752B29124CEC0094A026 /* BlockerToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockerToggle.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -703,6 +705,7 @@
08DB56E528F8FC7D00170C60 /* View */,
08C8731728F865B200DC859D /* SignUp */,
08C872EC28F6C5DE00DC859D /* SignIn */,
083EDBAD292A3ED40058F5D7 /* GMAuthViewController.swift */,
);
path = Auth;
sourceTree = "<group>";
Expand Down Expand Up @@ -1128,6 +1131,7 @@
083EA339290676EB0079605F /* StatViewModel.swift in Sources */,
082BF96828FD1D510094FE94 /* Podfile in Sources */,
088D691C29015865003D0660 /* AddExpenseField.swift in Sources */,
083EDBAE292A3ED50058F5D7 /* GMAuthViewController.swift in Sources */,
08F0688D2921DBEE005C58EC /* ExchangeCell.swift in Sources */,
083B14DF2914EF300058E36E /* SyncManager.swift in Sources */,
08C6894D2913B01C002071CC /* TrackingService.swift in Sources */,
Expand Down
12 changes: 12 additions & 0 deletions GoMoney/Models/Expense.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ struct DateAmount {
let date: Date
var totalAmount: Double
}

/// Transaction structure on firestore.
struct RemoteTransaction: Codable {
var _id: String
var type: String
var tag: String
var amount: Double
var note: String
var occuredOn: Date
var createdAt: Date?
var updatedAt: Date?
}
72 changes: 72 additions & 0 deletions GoMoney/Scences/Auth/GMAuthViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import UIKit

class GMAuthViewController: GMViewController {
func initData(completion: @escaping (String?) -> Void) {
GMLoadingView.shared.startLoadingAnimation(with: "Initing data ...")
TagService.shared.createDefaultDb { err in
GMLoadingView.shared.endLoadingAnimation()
if let err = err {
completion(err.localizedDescription)
} else {
completion(nil)
}
}
}

func initDataAndGoHome() {
initData { [weak self] err in
if let err = err {
self?.errorAlert(message: err)
} else {
self?.navigateToMainVC()
}
}
}

func navigateToMainVC() {
let homeVC = GMTabBarViewController()
if let delegate = view.window?.windowScene?.delegate as? SceneDelegate {
if let window = delegate.window {
window.rootViewController = homeVC

let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.5
UIView.transition(
with: window,
duration: duration,
options: options,
animations: {},
completion: { _ in })
}
}
}

func restoreData(completion: @escaping (Error?) -> Void) {
GMLoadingView.shared.startLoadingAnimation(with: "Restoring data ...")
AuthService.shared.restoreUserData(completion: { err in
GMLoadingView.shared.endLoadingAnimation()
completion(err)
})
}

func restoreDataAndGoHome() {
restoreData { [weak self] err in
if let err = err {
self?.errorAlert(message: err.localizedDescription)
} else {
self?.navigateToMainVC()
}
}
}

func checkIfNewUser(completion: @escaping (Bool) -> Void) {
RemoteService.shared.checkIfUserExist { result in
switch result {
case .success(let exist):
completion(!exist)
case .failure(let err):
self.errorAlert(message: err.localizedDescription)
}
}
}
}
15 changes: 4 additions & 11 deletions GoMoney/Scences/Auth/SignIn/SignInPasswordVC.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class SignInPasswordVC: GMViewController {
class SignInPasswordVC: GMAuthViewController {
// MARK: - Content

private enum Content {
Expand Down Expand Up @@ -116,25 +116,18 @@ class SignInPasswordVC: GMViewController {
errorLabel.text = error
} else {
errorLabel.text = ""
GMLoadingView.shared.startLoadingAnimation()
GMLoadingView.shared.startLoadingAnimation(with: "Logging in ...")
viewModel.signInWithEmailAndPassword(email: email, password: password) { [weak self] error in
GMLoadingView.shared.endLoadingAnimation()
if error != nil {
GMLoadingView.shared.endLoadingAnimation()
self?.errorLabel.text = error?.localizedDescription
} else {
self?.onSuccessLogin()
self?.restoreDataAndGoHome()
}
}
}
}
}

private func navigateToMainVC() {
let homeVC = GMTabBarViewController()
if let delegate = view.window?.windowScene?.delegate as? SceneDelegate {
delegate.window?.rootViewController = homeVC
}
}
}

extension SignInPasswordVC: UITextFieldDelegate {
Expand Down
18 changes: 9 additions & 9 deletions GoMoney/Scences/Auth/SignIn/SignInViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class SignInViewController: GMViewController {
class SignInViewController: GMAuthViewController {
private lazy var img: UIImageView = .build { view in
view.image = UIImage(named: "onboard_1")
}
Expand Down Expand Up @@ -108,18 +108,18 @@ class SignInViewController: GMViewController {
with: self,
completion: { [weak self] err in
if let err = err {
GMLoadingView.shared.endLoadingAnimation()
self?.errorAlert(message: err)
} else {
self?.navigateToMainVC()
self?.checkIfNewUser { isNew in
if isNew {
self?.initDataAndGoHome()
} else {
self?.restoreDataAndGoHome()
}
}
}
}
)
}

private func navigateToMainVC() {
let homeVC = GMTabBarViewController()
if let delegate = view.window?.windowScene?.delegate as? SceneDelegate {
delegate.window?.rootViewController = homeVC
}
}
}
11 changes: 2 additions & 9 deletions GoMoney/Scences/Auth/SignUp/SignUpPasswordVC.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class SignUpPasswordVC: GMViewController {
class SignUpPasswordVC: GMAuthViewController {
private enum Constant {
static let padding: CGFloat = 16
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class SignUpPasswordVC: GMViewController {
self?.errorLabel.text = error?.localizedDescription
} else {
// TODO: navigateToDetailVC
self?.navigateToMainVC()
self?.initDataAndGoHome()
}
}
}
Expand All @@ -133,13 +133,6 @@ class SignUpPasswordVC: GMViewController {
errorLabel.text = ""
}

private func navigateToMainVC() {
let homeVC = GMTabBarViewController()
if let delegate = view.window?.windowScene?.delegate as? SceneDelegate {
delegate.window?.rootViewController = homeVC
}
}

private func navigateToDetailVC() {
let vc = SignUpDetailViewController()
navigationController?.pushViewController(vc, animated: true)
Expand Down
13 changes: 12 additions & 1 deletion GoMoney/Scences/Profile/ProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ class ProfileViewController: GMMainViewController {
let navVC = UINavigationController(rootViewController: signInVC)

if let delegate = view.window?.windowScene?.delegate as? SceneDelegate {
delegate.window?.rootViewController = navVC
if let window = delegate.window {
window.rootViewController = navVC

let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.5
UIView.transition(
with: window,
duration: duration,
options: options,
animations: {},
completion: { _ in })
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions GoMoney/Service/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,33 @@ class AuthService {
// remove realm
DataService.shared.dropAllTable()
}

func restoreUserData(completion: @escaping (Error?) -> Void) {
RemoteService.shared.getAllTags { result in
switch result {
case .failure(let err):
completion(err)
case .success(let tags):
TagService.shared.setTags(tags: tags) { err in
if let err = err {
completion(err)
}
else {
print("[restore] \(tags.count) tags")
RemoteService.shared.getAllTransactions { result in
switch result {
case .success(let transactions):
print("[restore] \(transactions.count) transactions")
DataService.shared.addTransactions(transactions) { err in
completion(err)
}
case .failure(let err):
completion(err)
}
}
}
}
}
}
}
}
14 changes: 14 additions & 0 deletions GoMoney/Service/DataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum DataError: Error {
case transactionNotFound(_ transaction: Expense)
case noTransactions
case userNotFound
case tagNotFound

var localizedDescription: String {
switch self {
Expand All @@ -22,6 +23,8 @@ enum DataError: Error {
return "There is no transactions!"
case .userNotFound:
return "User not found!"
case .tagNotFound:
return "Tag not found!"
}
}
}
Expand Down Expand Up @@ -111,6 +114,17 @@ class DataService {
}
}

func addTransactions(_ transactions: [Expense], completion: (Error?) -> Void) {
do {
try realm.write {
realm.add(transactions)
}
completion(nil)
} catch {
completion(error)
}
}

func deleteExpense(expense: Expense, completion: ((Error?) -> Void)? = nil) {
do {
try realm.write {
Expand Down
Loading