Untitled
unknown
java
3 years ago
32 kB
12
Indexable
package com.example.trackingbudget;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.MutableDateTime;
import org.joda.time.Weeks;
import org.w3c.dom.Text;
import java.util.Calendar;
import java.util.Map;
public class BudgetActivity extends AppCompatActivity {
private FloatingActionButton fab;
private TextView totalBudgetTV;
private RecyclerView recycleView;
private DatabaseReference budgetRef,personalRef;
private FirebaseAuth mAuth;
private ProgressDialog loader;
private String post_key = "";
private String item = "";
private int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget);
mAuth = FirebaseAuth.getInstance();
budgetRef = FirebaseDatabase.getInstance().getReference().child("budget").child(mAuth.getCurrentUser().getUid());
personalRef = FirebaseDatabase.getInstance().getReference().child("personal").child(mAuth.getCurrentUser().getUid());
loader = new ProgressDialog(this);
totalBudgetTV = findViewById(R.id.totalBudgetTV);
recycleView = findViewById(R.id.recycleView);
//Display the budget Items
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recycleView.setHasFixedSize(true);
recycleView.setLayoutManager(linearLayoutManager);
//Display Total budget
budgetRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
int totalAmount =0;
for (DataSnapshot snap:snapshot.getChildren()){
Data data = snap.getValue(Data.class);
totalAmount += data.getAmount();
String sTotal = String.valueOf("Month budger: $"+totalAmount);
totalBudgetTV.setText(sTotal);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
//Add item button
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
additem();
}
});
personalRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists() && snapshot.getChildrenCount()<0){
int totalAmount = 0;
for (DataSnapshot ds : snapshot.getChildren()){
Data data = ds.getValue(Data.class);
String sttotal = String.valueOf("Month Budget: "+totalAmount);
totalBudgetTV.setText(sttotal);
}
int weeklyBudget = totalAmount/4;
int dailyBudget = totalAmount/30;
personalRef.child("budget").setValue(totalAmount);
personalRef.child("weeklyBudget").setValue(weeklyBudget);
personalRef.child("dailyBudget").setValue(dailyBudget);
}else{
personalRef.child("budget").setValue(0);
personalRef.child("weeklyBudget").setValue(0);
personalRef.child("dailyBudget").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
getMonthTransportBudgetRatios();
getMonthFoodBudgetRatios();
getMonthHouseBudgetRatios();
getMonthEducationBudgetRatios();
getMonthEntertainmentBudgetRatios();
getMonthCharityBudgetRatios();
getMonthPersonalBudgetRatios();
getMonthApparelBudgetRatios();
getMonthHealthBudgetRatios();
getMonthOthersBudgetRatios();
}
private void getMonthOthersBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Others");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayOthersRatio").setValue(dayTransRatio);
personalRef.child("weekOthersRatio").setValue(weekTransRation);
personalRef.child("monthOthersRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayOthersRatio").setValue(0);
personalRef.child("weekOthersRatio").setValue(0);
personalRef.child("monthOthersRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthHealthBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Health");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayHealthRatio").setValue(dayTransRatio);
personalRef.child("weekHealthRatio").setValue(weekTransRation);
personalRef.child("monthHealthRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayHealthRatio").setValue(0);
personalRef.child("weekHealthRatio").setValue(0);
personalRef.child("monthHealthRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthApparelBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Apparel and Services");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayApparelRatio").setValue(dayTransRatio);
personalRef.child("weekApparelRatio").setValue(weekTransRation);
personalRef.child("monthApparelRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayApparelRatio").setValue(0);
personalRef.child("weekApparelRatio").setValue(0);
personalRef.child("monthApparelRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthPersonalBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Personal");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayPersonalRatio").setValue(dayTransRatio);
personalRef.child("weekPersonalRatio").setValue(weekTransRation);
personalRef.child("monthPersonalRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayPersonalRatio").setValue(0);
personalRef.child("weekPersonalRatio").setValue(0);
personalRef.child("monthPersonalRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthCharityBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Charity");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayCharityRatio").setValue(dayTransRatio);
personalRef.child("weekCharityRatio").setValue(weekTransRation);
personalRef.child("monthCharityRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayCharityRatio").setValue(0);
personalRef.child("weekCharityRatio").setValue(0);
personalRef.child("monthCharityRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthEntertainmentBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Entertainment");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayEntertainmentRatio").setValue(dayTransRatio);
personalRef.child("weekEntertainmentRatio").setValue(weekTransRation);
personalRef.child("monthEntertainmentRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayEntertainmentRatio").setValue(0);
personalRef.child("weekEntertainmentRatio").setValue(0);
personalRef.child("monthEntertainmentRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthEducationBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Education");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayEducationRatio").setValue(dayTransRatio);
personalRef.child("weekEducationRatio").setValue(weekTransRation);
personalRef.child("monthEducationRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayEducationRatio").setValue(0);
personalRef.child("weekEducationRatio").setValue(0);
personalRef.child("monthEducationRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthHouseBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("House Expenses");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayHouseRatio").setValue(dayTransRatio);
personalRef.child("weekHouseRatio").setValue(weekTransRation);
personalRef.child("monthHouseRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayHouseRatio").setValue(0);
personalRef.child("weekHouseRatio").setValue(0);
personalRef.child("monthHouseRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthFoodBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Food");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayFoodRatio").setValue(dayTransRatio);
personalRef.child("weekFoodRatio").setValue(weekTransRation);
personalRef.child("monthFoodRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayFoodRatio").setValue(0);
personalRef.child("weekFoodRatio").setValue(0);
personalRef.child("monthFoodRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getMonthTransportBudgetRatios() {
Query query = budgetRef.orderByChild("item").equalTo("Transport");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
int pTotal = 0;
for(DataSnapshot ds : snapshot.getChildren()){
Map<String,Object> map = (Map<String,Object>) ds.getValue();
Object total = map.get("amount");
pTotal = Integer.parseInt(String.valueOf(total));
}
int dayTransRatio = pTotal/30;
int weekTransRation = pTotal/4;
int monthTransRatio = pTotal;
personalRef.child("dayTransRatio").setValue(dayTransRatio);
personalRef.child("weekTransRatio").setValue(weekTransRation);
personalRef.child("monthTransRatio").setValue(monthTransRatio);
}else{
personalRef.child("dayTransRatio").setValue(0);
personalRef.child("weekTransRatio").setValue(0);
personalRef.child("monthTransRatio").setValue(0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void additem() {
AlertDialog.Builder myDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = LayoutInflater.from(this);
View myView = inflater.inflate(R.layout.input_layout,null);
myDialog.setView(myView);
final AlertDialog dialog = myDialog.create();
dialog.setCancelable(false);
final Spinner itemSpinner = myView.findViewById(R.id.itemSpinner);
final EditText amount = myView.findViewById(R.id.amount);
final Button cancel = myView.findViewById(R.id.cancel);
final Button save = myView.findViewById(R.id.save);
//Adding an item to firebase database
save.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View view) {
String budgetAmount = amount.getText().toString();
String budgetItem = itemSpinner.getSelectedItem().toString();
//Input Validation
if(TextUtils.isEmpty(budgetAmount)){
amount.setError("Amount is required");
return;
}
if(budgetItem.equals("Select Item")){
Toast.makeText(BudgetActivity.this, "Select valid item", Toast.LENGTH_SHORT).show();
}
loader.setMessage("Adding a budget item");
loader.setCanceledOnTouchOutside(false);
loader.show();
String id = budgetRef.push().getKey();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
String date = dateFormat.format(cal.getTime());
MutableDateTime epoch = new MutableDateTime();
epoch.setDate(0);
DateTime now = new DateTime();
Weeks weeks = Weeks.weeksBetween(epoch,now);
Months month = Months.monthsBetween(epoch,now);
String itemNday = budgetItem+date;
String itemNweek = budgetItem+weeks.getWeeks();
String itemNmonth = budgetItem+month.getMonths();
Data data = new Data(budgetItem,date,id,null,itemNday,itemNweek,itemNmonth,Integer.parseInt(budgetAmount), month.getMonths(),weeks.getWeeks());
budgetRef.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(BudgetActivity.this,"Budget item added successfully",Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(BudgetActivity.this,task.getException().toString(),Toast.LENGTH_SHORT).show();
}
loader.dismiss();
}
});
dialog.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Data> options = new FirebaseRecyclerOptions.Builder<Data>().setQuery(budgetRef,Data.class).build();
FirebaseRecyclerAdapter<Data,MyViewHolder> adapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position, @NonNull Data model) {
holder.setItemAmount("Allocated amount: $"+model.getAmount());
holder.setItemDate("On: "+model.getDate());
holder.setItemName("Budget item: "+model.getItem());
//set note to invisible
holder.notes.setVisibility(View.GONE);
//Change the imageview depending on the item
switch (model.getItem()){
case "Transport":
holder.imageView.setImageResource(R.drawable.ic_transport);
break;
case "Education":
holder.imageView.setImageResource(R.drawable.ic_education);
case "Food":
holder.imageView.setImageResource(R.drawable.ic_food);
case "House":
holder.imageView.setImageResource(R.drawable.ic_house);
case "Entertainment":
holder.imageView.setImageResource(R.drawable.ic_entertainment);
case "Charity": // change imageview
holder.imageView.setImageResource(R.drawable.ic_education);
case "Personal":
holder.imageView.setImageResource(R.drawable.ic_personalcare);
case "Apparel":
holder.imageView.setImageResource(R.drawable.ic_shirt);
case "Other":
holder.imageView.setImageResource(R.drawable.ic_other);
case "Health":
holder.imageView.setImageResource(R.drawable.ic_health);
}
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
post_key = getRef(position).getKey();
item= model.getItem();
amount = model.getAmount();
updateData();
}
});
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.retreive_layout,parent,false);
return new MyViewHolder(view);
}
};
recycleView.setAdapter(adapter);
adapter.startListening();
adapter.notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
View mView;
public ImageView imageView;
public TextView notes;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
imageView = itemView.findViewById(R.id.imageView);
notes = itemView.findViewById(R.id.note);
}
public void setItemName(String itemName){
TextView item = mView.findViewById(R.id.item);
item.setText(itemName);
}
public void setItemAmount(String itemAmount){
TextView amount = mView.findViewById(R.id.amount);
amount.setText(itemAmount);
}
public void setItemDate(String itemDate){
TextView date = mView.findViewById(R.id.date);
date.setText(itemDate);
}
}
private void updateData(){
AlertDialog.Builder myDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = LayoutInflater.from(this);
View mView = inflater.inflate(R.layout.update_layout,null);
myDialog.setView(mView);
final AlertDialog dialog = myDialog.create();
final TextView mItem = mView.findViewById(R.id.itemName);
final EditText mAmount = mView.findViewById(R.id.amount);
final EditText notes = mView.findViewById(R.id.note);
//set notes to invisible
notes.setVisibility(View.GONE);
mItem.setText(item);
mAmount.setText(String.valueOf(amount));
mAmount.setSelection(String.valueOf(amount).length());
Button delBut = mView.findViewById(R.id.btnDelete);
Button btnUpdate = mView.findViewById(R.id.btnUpdate);
//Update Button
btnUpdate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View view) {
amount = Integer.parseInt(mAmount.getText().toString());
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
String date = dateFormat.format(cal.getTime());
MutableDateTime epoch = new MutableDateTime();
epoch.setDate(0);
DateTime now = new DateTime();
Weeks weeks = Weeks.weeksBetween(epoch,now);
Months month = Months.monthsBetween(epoch,now);
String itemNday = item+date;
String itemNweek = item+weeks.getWeeks();
String itemNmonth = item+month.getMonths();
Data data = new Data(item,date,post_key,null, itemNday,itemNweek,itemNmonth,amount,month.getMonths(),weeks.getWeeks());
budgetRef.child(post_key).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(BudgetActivity.this,"Item updated successfully",Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(BudgetActivity.this,task.getException().toString(),Toast.LENGTH_SHORT).show();
}
}
});
dialog.dismiss();
}
});
//Delete Button
delBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
budgetRef.child(post_key).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(BudgetActivity.this,"Item deleted successfully",Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(BudgetActivity.this,task.getException().toString(),Toast.LENGTH_SHORT).show();
}
}
});
dialog.dismiss();
}
});
dialog.show();
}
}Editor is loading...