-
-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monthly recurring expenses and income #30
Open
comhendrik
wants to merge
12
commits into
sameersyd:dev
Choose a base branch
from
comhendrik:monthly-expenses
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c32e00
Merge pull request #22 from sameersyd/dev
sameersyd 538ba2c
Merge branch 'dev' into main
sameersyd 3117e04
Merge branch 'dev'
sameersyd ca60baf
Initialize MonthlyExpenseCD
comhendrik 3e6b901
option to create monthly expenses and add them automatically
comhendrik b5174d1
Init MonthlyExpensesSettingsView
comhendrik c120265
ui for monthly transactions view
comhendrik 6145beb
Monthly Transaction 1.0
comhendrik e927912
automatic transactions
comhendrik 276880f
first changes for one entity with monthly expenses
comhendrik a6eb61e
monthly Transactions with one entity
comhendrik d0d300c
automatic transaction for ExpenseCD
comhendrik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// MonthlyExpenseCD.swift | ||
// Expenso | ||
// | ||
// Created by Hendrik Steen on 31.05.22. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
public class MonthlyTransactionCD: NSManagedObject, Identifiable { | ||
@NSManaged public var type: String? | ||
@NSManaged public var title: String? | ||
@NSManaged public var tag: String? | ||
@NSManaged public var note: String? | ||
@NSManaged public var amount: Double | ||
@NSManaged public var imageAttached: Data? | ||
@NSManaged public var usingDate: Date? | ||
} | ||
|
||
extension MonthlyTransactionCD { | ||
static func getAllMonthlyExpenseData() -> NSFetchRequest<MonthlyTransactionCD> { | ||
let request: NSFetchRequest<MonthlyTransactionCD> = MonthlyTransactionCD.fetchRequest() as! NSFetchRequest<MonthlyTransactionCD> | ||
request.sortDescriptors = [] | ||
return request | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import CoreData | |
class AddExpenseViewModel: ObservableObject { | ||
|
||
var expenseObj: ExpenseCD? | ||
var monthlyTransactionObj: MonthlyTransactionCD? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update this to use single model (refer above comment). |
||
|
||
@Published var title = "" | ||
@Published var amount = "" | ||
|
@@ -31,36 +32,65 @@ class AddExpenseViewModel: ObservableObject { | |
@Published var showAlert = false | ||
@Published var closePresenter = false | ||
|
||
init(expenseObj: ExpenseCD? = nil) { | ||
|
||
self.expenseObj = expenseObj | ||
self.title = expenseObj?.title ?? "" | ||
if let expenseObj = expenseObj { | ||
self.amount = String(expenseObj.amount) | ||
self.typeTitle = expenseObj.type == TRANS_TYPE_INCOME ? "Income" : "Expense" | ||
init(expenseObj: ExpenseCD? = nil, monthlyTransactionObj: MonthlyTransactionCD? = nil) { | ||
if monthlyTransactionObj != nil { | ||
|
||
self.monthlyTransactionObj = monthlyTransactionObj | ||
self.title = monthlyTransactionObj?.title ?? "" | ||
if let monthlyTransactionObj = monthlyTransactionObj { | ||
self.amount = String(monthlyTransactionObj.amount) | ||
self.typeTitle = monthlyTransactionObj.type == TRANS_TYPE_INCOME ? "Income" : "Expense" | ||
} else { | ||
self.amount = "" | ||
self.typeTitle = "Income" | ||
} | ||
self.occuredOn = monthlyTransactionObj?.usingDate ?? Date() | ||
self.note = monthlyTransactionObj?.note ?? "" | ||
self.tagTitle = getTransTagTitle(transTag: monthlyTransactionObj?.tag ?? TRANS_TAG_TRANSPORT) | ||
self.selectedType = monthlyTransactionObj?.type ?? TRANS_TYPE_INCOME | ||
self.selectedTag = monthlyTransactionObj?.tag ?? TRANS_TAG_TRANSPORT | ||
if let data = monthlyTransactionObj?.imageAttached { | ||
self.imageAttached = UIImage(data: data) | ||
} | ||
|
||
AttachmentHandler.shared.imagePickedBlock = { [weak self] image in | ||
self?.imageUpdated = true | ||
self?.imageAttached = image | ||
} | ||
|
||
} else { | ||
self.amount = "" | ||
self.typeTitle = "Income" | ||
} | ||
self.occuredOn = expenseObj?.occuredOn ?? Date() | ||
self.note = expenseObj?.note ?? "" | ||
self.tagTitle = getTransTagTitle(transTag: expenseObj?.tag ?? TRANS_TAG_TRANSPORT) | ||
self.selectedType = expenseObj?.type ?? TRANS_TYPE_INCOME | ||
self.selectedTag = expenseObj?.tag ?? TRANS_TAG_TRANSPORT | ||
if let data = expenseObj?.imageAttached { | ||
self.imageAttached = UIImage(data: data) | ||
|
||
self.expenseObj = expenseObj | ||
self.title = expenseObj?.title ?? "" | ||
if let expenseObj = expenseObj { | ||
self.amount = String(expenseObj.amount) | ||
self.typeTitle = expenseObj.type == TRANS_TYPE_INCOME ? "Income" : "Expense" | ||
} else { | ||
self.amount = "" | ||
self.typeTitle = "Income" | ||
} | ||
self.occuredOn = expenseObj?.occuredOn ?? Date() | ||
self.note = expenseObj?.note ?? "" | ||
self.tagTitle = getTransTagTitle(transTag: expenseObj?.tag ?? TRANS_TAG_TRANSPORT) | ||
self.selectedType = expenseObj?.type ?? TRANS_TYPE_INCOME | ||
self.selectedTag = expenseObj?.tag ?? TRANS_TAG_TRANSPORT | ||
if let data = expenseObj?.imageAttached { | ||
self.imageAttached = UIImage(data: data) | ||
} | ||
|
||
AttachmentHandler.shared.imagePickedBlock = { [weak self] image in | ||
self?.imageUpdated = true | ||
self?.imageAttached = image | ||
} | ||
|
||
} | ||
|
||
AttachmentHandler.shared.imagePickedBlock = { [weak self] image in | ||
self?.imageUpdated = true | ||
self?.imageAttached = image | ||
} | ||
} | ||
|
||
func getButtText() -> String { | ||
if selectedType == TRANS_TYPE_INCOME { return "\(expenseObj == nil ? "ADD" : "EDIT") INCOME" } | ||
else if selectedType == TRANS_TYPE_EXPENSE { return "\(expenseObj == nil ? "ADD" : "EDIT") EXPENSE" } | ||
else { return "\(expenseObj == nil ? "ADD" : "EDIT") TRANSACTION" } | ||
if selectedType == TRANS_TYPE_INCOME { return "\((expenseObj != nil || monthlyTransactionObj != nil) ? "EDIT" : "ADD") INCOME" } | ||
else if selectedType == TRANS_TYPE_EXPENSE { return "\((expenseObj != nil || monthlyTransactionObj != nil) ? "EDIT" : "ADD") EXPENSE" } | ||
else { return "\((expenseObj != nil || monthlyTransactionObj != nil) ? "EDIT" : "ADD") TRANSACTION" } | ||
} | ||
|
||
func attachImage() { AttachmentHandler.shared.showAttachmentActionSheet() } | ||
|
@@ -139,4 +169,113 @@ class AddExpenseViewModel: ObservableObject { | |
try managedObjectContext.save(); closePresenter = true | ||
} catch { alertMsg = "\(error)"; showAlert = true } | ||
} | ||
|
||
|
||
func saveMonthlyTransaction(managedObjectContext: NSManagedObjectContext) { | ||
let expense: MonthlyTransactionCD | ||
let titleStr = title.trimmingCharacters(in: .whitespacesAndNewlines) | ||
let amountStr = amount.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
||
if titleStr.isEmpty || titleStr == "" { | ||
alertMsg = "Enter Title"; showAlert = true | ||
return | ||
} | ||
if amountStr.isEmpty || amountStr == "" { | ||
alertMsg = "Enter Amount"; showAlert = true | ||
return | ||
} | ||
guard let amount = Double(amountStr) else { | ||
alertMsg = "Enter valid number"; showAlert = true | ||
return | ||
} | ||
guard amount >= 0 else { | ||
alertMsg = "Amount can't be negative"; showAlert = true | ||
return | ||
} | ||
guard amount <= 1000000000 else { | ||
alertMsg = "Enter a smaller amount"; showAlert = true | ||
return | ||
} | ||
|
||
if monthlyTransactionObj != nil { | ||
|
||
expense = monthlyTransactionObj! | ||
|
||
// if let image = imageAttached { | ||
// if imageUpdated { | ||
// if let _ = expense.imageAttached { | ||
// // Delete Previous Image from CoreData | ||
// } | ||
// expense.imageAttached = image.jpegData(compressionQuality: 1.0) | ||
// } | ||
// } else { | ||
// if let _ = expense.imageAttached { | ||
// // Delete Previous Image from CoreData | ||
// } | ||
// expense.imageAttached = nil | ||
// } | ||
|
||
} else { | ||
expense = MonthlyTransactionCD(context: managedObjectContext) | ||
// if let image = imageAttached { | ||
// expense.imageAttached = image.jpegData(compressionQuality: 1.0) | ||
// } | ||
} | ||
expense.usingDate = occuredOn | ||
expense.type = selectedType | ||
expense.title = titleStr | ||
expense.tag = selectedTag | ||
expense.note = note | ||
expense.amount = amount | ||
do { | ||
try managedObjectContext.save() | ||
closePresenter = true | ||
} catch { alertMsg = "\(error)"; showAlert = true } | ||
} | ||
|
||
func deleteMonthlyTransaction(managedObjectContext: NSManagedObjectContext) { | ||
guard let monthlyTransactionObj = monthlyTransactionObj else { return } | ||
managedObjectContext.delete(monthlyTransactionObj) | ||
do { | ||
try managedObjectContext.save(); closePresenter = true | ||
} catch { alertMsg = "\(error)"; showAlert = true } | ||
} | ||
|
||
func checkAllMonthlyExpenses(managedObjectContext: NSManagedObjectContext, request: NSFetchRequest<MonthlyTransactionCD>) { | ||
do { | ||
let allMonthlyExpenses = try? managedObjectContext.fetch(request) | ||
if allMonthlyExpenses == nil { | ||
print("error") | ||
return | ||
} | ||
for expense in allMonthlyExpenses! { | ||
print(expense.usingDate ?? Date()) | ||
if expense.usingDate == nil { | ||
continue | ||
} | ||
if Calendar.current.isDateInToday(expense.usingDate!) || Date() > expense.usingDate! { | ||
if let date = Calendar.current.date(byAdding: .month, value: 1, to: expense.usingDate!) { | ||
expense.usingDate = date | ||
print("date used") | ||
let newExpense = ExpenseCD(context: managedObjectContext) | ||
newExpense.amount = expense.amount | ||
newExpense.createdAt = Date() | ||
//newExpense.imageAttached = expense.imageAttached | ||
newExpense.note = expense.note | ||
newExpense.occuredOn = Date() | ||
newExpense.tag = expense.tag | ||
newExpense.title = expense.title | ||
newExpense.type = expense.type | ||
newExpense.updatedAt = Date() | ||
try managedObjectContext.save() | ||
} | ||
} else { | ||
print("not current date or later date") | ||
} | ||
} | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of new model, we can use the existing model ExpenseCD with
frequency
enum. Which can store values such as{ onetime, monthly }
. Use this frequency val to query.Also we don't need a separate button (Add income every month) to create recurring expense. Let's add a toggle which can update the frequency value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a possibility that this feature will be included in the app in the App Store? I ask myself this question because you mentioned that this is a different approach. However, I will start working on it.