-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added setting page and income distribution loggic but its now working…
… like we planned
- Loading branch information
Showing
9 changed files
with
514 additions
and
22 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/elececo/umoney/data/model/UserPreferences.java
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,16 @@ | ||
package com.elececo.umoney.data.model; | ||
public class UserPreferences { | ||
private int needsPercentage; | ||
private int wantsPercentage; | ||
private int savingsPercentage; | ||
|
||
public UserPreferences(int needsPercentage, int wantsPercentage, int savingsPercentage) { | ||
this.needsPercentage = needsPercentage; | ||
this.wantsPercentage = wantsPercentage; | ||
this.savingsPercentage = savingsPercentage; | ||
} | ||
|
||
public int getNeedsPercentage() { return needsPercentage; } | ||
public int getWantsPercentage() { return wantsPercentage; } | ||
public int getSavingsPercentage() { return savingsPercentage; } | ||
} |
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
176 changes: 168 additions & 8 deletions
176
app/src/main/java/com/elececo/umoney/ui/income/viewmodel/IncomeViewModel.java
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 |
---|---|---|
@@ -1,25 +1,185 @@ | ||
package com.elececo.umoney.ui.income.viewmodel; | ||
|
||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
import com.elececo.umoney.data.model.Transaction; | ||
import com.elececo.umoney.data.repository.TransactionRepository; | ||
import com.elececo.umoney.data.model.UserPreferences; | ||
import com.google.firebase.auth.FirebaseAuth; | ||
import com.google.firebase.firestore.FirebaseFirestore; | ||
import com.google.firebase.firestore.QueryDocumentSnapshot; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class IncomeViewModel extends ViewModel { | ||
private final TransactionRepository repository; | ||
private final LiveData<List<Transaction>> transactions; | ||
private final FirebaseFirestore db; | ||
private final FirebaseAuth auth; | ||
private final MutableLiveData<List<Transaction>> transactions; | ||
private final MutableLiveData<UserPreferences> userPreferences; | ||
|
||
public IncomeViewModel() { | ||
repository = new TransactionRepository(); | ||
transactions = repository.getTransactionsByType("INCOME"); | ||
db = FirebaseFirestore.getInstance(); | ||
auth = FirebaseAuth.getInstance(); | ||
transactions = new MutableLiveData<>(new ArrayList<>()); | ||
userPreferences = new MutableLiveData<>(); | ||
loadTransactions(); | ||
loadUserPreferences(); | ||
} | ||
|
||
public LiveData<List<Transaction>> getTransactions() { | ||
return transactions; | ||
} | ||
|
||
public LiveData<UserPreferences> getUserPreferences() { | ||
return userPreferences; | ||
} | ||
|
||
private void loadTransactions() { | ||
String userId = auth.getCurrentUser().getUid(); | ||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.whereEqualTo("type", "INCOME") | ||
.get() | ||
.addOnSuccessListener(queryDocumentSnapshots -> { | ||
List<Transaction> transactionList = new ArrayList<>(); | ||
for (QueryDocumentSnapshot document : queryDocumentSnapshots) { | ||
Transaction transaction = document.toObject(Transaction.class); | ||
transactionList.add(transaction); | ||
} | ||
transactions.setValue(transactionList); | ||
}); | ||
} | ||
|
||
private void loadUserPreferences() { | ||
String userId = auth.getCurrentUser().getUid(); | ||
db.collection("users") | ||
.document(userId) | ||
.get() | ||
.addOnSuccessListener(document -> { | ||
if (document.exists()) { | ||
Map<String, Object> data = document.getData(); | ||
int needs = data.containsKey("needsPercentage") ? | ||
((Long) data.get("needsPercentage")).intValue() : 50; | ||
int wants = data.containsKey("wantsPercentage") ? | ||
((Long) data.get("wantsPercentage")).intValue() : 30; | ||
int savings = data.containsKey("savingsPercentage") ? | ||
((Long) data.get("savingsPercentage")).intValue() : 20; | ||
|
||
userPreferences.setValue(new UserPreferences(needs, wants, savings)); | ||
} else { | ||
userPreferences.setValue(new UserPreferences(50, 30, 20)); | ||
} | ||
}) | ||
.addOnFailureListener(e -> { | ||
userPreferences.setValue(new UserPreferences(50, 30, 20)); | ||
}); | ||
} | ||
|
||
public void distributeIncome(double totalIncome, UserPreferences preferences, String parentTransactionId) { | ||
String userId = auth.getCurrentUser().getUid(); | ||
|
||
// Calculate amounts for each category | ||
double needsAmount = totalIncome * (preferences.getNeedsPercentage() / 100.0); | ||
double wantsAmount = totalIncome * (preferences.getWantsPercentage() / 100.0); | ||
double savingsAmount = totalIncome * (preferences.getSavingsPercentage() / 100.0); | ||
|
||
Date currentTime = new Date(); | ||
|
||
// Create distributed transactions | ||
Transaction needsTransaction = new Transaction(); | ||
needsTransaction.setAmount(needsAmount); | ||
needsTransaction.setNotes("Auto-distributed from Income"); | ||
needsTransaction.setCategory("NEEDS"); | ||
needsTransaction.setType("INCOME"); | ||
needsTransaction.setTimestamp(currentTime); | ||
needsTransaction.setParentTransactionId(parentTransactionId); | ||
needsTransaction.setUserId(userId); | ||
|
||
Transaction wantsTransaction = new Transaction(); | ||
wantsTransaction.setAmount(wantsAmount); | ||
wantsTransaction.setNotes("Auto-distributed from Income"); | ||
wantsTransaction.setCategory("WANTS"); | ||
wantsTransaction.setType("INCOME"); | ||
wantsTransaction.setTimestamp(currentTime); | ||
wantsTransaction.setParentTransactionId(parentTransactionId); | ||
wantsTransaction.setUserId(userId); | ||
|
||
Transaction savingsTransaction = new Transaction(); | ||
savingsTransaction.setAmount(savingsAmount); | ||
savingsTransaction.setNotes("Auto-distributed from Income"); | ||
savingsTransaction.setCategory("SAVINGS"); | ||
savingsTransaction.setType("INCOME"); | ||
savingsTransaction.setTimestamp(currentTime); | ||
savingsTransaction.setParentTransactionId(parentTransactionId); | ||
savingsTransaction.setUserId(userId); | ||
|
||
// Save all transactions | ||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.add(needsTransaction); | ||
|
||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.add(wantsTransaction); | ||
|
||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.add(savingsTransaction); | ||
} | ||
|
||
public void saveTransaction(Transaction transaction) { | ||
repository.saveTransaction(transaction); | ||
String userId = auth.getCurrentUser().getUid(); | ||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.add(transaction) | ||
.addOnSuccessListener(documentReference -> { | ||
String transactionId = documentReference.getId(); | ||
transaction.setId(transactionId); | ||
loadTransactions(); | ||
// After saving income transaction, distribute it | ||
if (transaction.getType().equals("INCOME")) { | ||
getUserPreferences().observeForever(preferences -> { | ||
distributeIncome(transaction.getAmount(), preferences, transactionId); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
public LiveData<List<Transaction>> getTransactions() { | ||
return transactions; | ||
public void updateDistributedTransactions(String parentTransactionId, double newAmount, UserPreferences preferences) { | ||
String userId = auth.getCurrentUser().getUid(); | ||
|
||
db.collection("users") | ||
.document(userId) | ||
.collection("transactions") | ||
.whereEqualTo("parentTransactionId", parentTransactionId) | ||
.get() | ||
.addOnSuccessListener(querySnapshot -> { | ||
double needsAmount = newAmount * (preferences.getNeedsPercentage() / 100.0); | ||
double wantsAmount = newAmount * (preferences.getWantsPercentage() / 100.0); | ||
double savingsAmount = newAmount * (preferences.getSavingsPercentage() / 100.0); | ||
|
||
for (QueryDocumentSnapshot document : querySnapshot) { | ||
Transaction transaction = document.toObject(Transaction.class); | ||
switch (transaction.getType()) { | ||
case "NEEDS": | ||
transaction.setAmount(needsAmount); | ||
break; | ||
case "WANTS": | ||
transaction.setAmount(wantsAmount); | ||
break; | ||
case "SAVINGS": | ||
transaction.setAmount(savingsAmount); | ||
break; | ||
} | ||
document.getReference().set(transaction); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.