Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
.idea/
backend/vendor/*/
29 changes: 0 additions & 29 deletions Budget/.idea/codeStyles/Project.xml

This file was deleted.

18 changes: 0 additions & 18 deletions Budget/.idea/gradle.xml

This file was deleted.

34 changes: 0 additions & 34 deletions Budget/.idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions Budget/.idea/runConfigurations.xml

This file was deleted.

8 changes: 7 additions & 1 deletion Budget/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'

implementation 'com.squareup.moshi:moshi:1.7.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.7.0'

// Add dependencies to include them in your project.
implementation 'com.android.support:recyclerview-v7:28.0.0'

Expand Down
9 changes: 8 additions & 1 deletion Budget/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="io.jeffchang.budget">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".budgetlist.additem.AddItemActivity" />

</application>

</manifest>
201 changes: 181 additions & 20 deletions Budget/app/src/main/java/io/jeffchang/budget/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,213 @@
package io.jeffchang.budget;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import io.jeffchang.budget.budgetlist.BudgetItem;
import io.jeffchang.budget.model.BudgetItem;
import io.jeffchang.budget.budgetlist.BudgetListRecyclerViewAdapter;
import io.jeffchang.budget.budgetlist.additem.AddItemActivity;
import io.jeffchang.budget.network.BudgetService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.moshi.MoshiConverterFactory;

public class MainActivity extends AppCompatActivity {

public String TAG = MainActivity.class.getSimpleName();

private BudgetListRecyclerViewAdapter adapter;
private TextView budgetTrackerTextView;

float budgetLimit = 0;
private BudgetService budgetService;
private ArrayList<BudgetItem> budgetItemArrayList;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView budgetRecyclerView = findViewById(R.id.activity_main_budget_recycler_view);

budgetTrackerTextView = findViewById(R.id.activity_main_budget_tracker_view);

// Here we add mock data to test our UI
ArrayList<BudgetItem> budgetItemArrayList = new ArrayList<>();
setUpRetrofit();

BudgetItem gasItem = new BudgetItem(
"gas", "BP", "Utility", -43
);
budgetItemArrayList.add(gasItem);
setupRecyclerView();

BudgetItem tacoBellItem = new BudgetItem(
"taco_bell", "Taco Bell", "Food", -8
);
budgetItemArrayList.add(tacoBellItem);
// This has to be called after Retrofit and RecyclerView is set up.
getBudgetFromNetwork();

BudgetItem payCheck = new BudgetItem(
"pay_check", "Pay Check", "Income", 3000
);
budgetItemArrayList.add(payCheck);
Button addButton = findViewById(R.id.activity_main_add_button);

final EditText budgetLimitEditText = findViewById(R.id.activity_main_budget_edit_text);

budgetLimitEditText.addTextChangedListener(new TextWatcher() {

@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
if (!input.startsWith("$")) {
input = "$" + input;

// Prevents the user from deleting past the dollar sign symbol.
budgetLimitEditText.setText(input);
budgetLimitEditText.setSelection(1);
}
try {
// Parses float of all characters after the dollar sign.
input = input.substring(1);
budgetLimit = Float.parseFloat(input);
} catch (NumberFormatException e) {
// If the string is empty or invalid, treat the limit as zero.
budgetLimit = 0;
}
setBudgetTrackerTextView(adapter.getSum());
}
});

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MainActivity.this,
AddItemActivity.class
);
startActivityForResult(intent, AddItemActivity.ADD_ITEM_REQUEST_CODE);
}
});
}

private void setupRecyclerView() {
// Here we add mock data to test our UI.
budgetItemArrayList = new ArrayList<>();

RecyclerView budgetRecyclerView = findViewById(R.id.activity_main_budget_recycler_view);

adapter = new BudgetListRecyclerViewAdapter();

BudgetListRecyclerViewAdapter adapter = new BudgetListRecyclerViewAdapter(
budgetItemArrayList
);
budgetRecyclerView.setLayoutManager(
new LinearLayoutManager(this)
);
budgetRecyclerView.setAdapter(adapter);
}


public void setUpRetrofit() {
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl("https://rocky-fortress-15911.herokuapp.com")
.addConverterFactory(MoshiConverterFactory.create())
.build();

budgetService = retrofit.create(BudgetService.class);
}

// Only call when budgetService is not null.
public void getBudgetFromNetwork() {
budgetService.getBudget().enqueue(new Callback<List<BudgetItem>>() {
@Override
public void onResponse(
Call<List<BudgetItem>> call,
Response<List<BudgetItem>> response) {

float sum = adapter.addBudgetItemList(response.body());
setBudgetTrackerTextView(sum);

}

@Override
public void onFailure(Call<List<BudgetItem>> call, Throwable t) {
Log.e("MainActivity", "Couldn't get budgets");
}
});
}


// This method will update the database on our back-end and give back our budget that we update
// our RecyclerViewAdapter.
public void postBudget(final BudgetItem budgetItem) {
budgetService.postBudget(budgetItem).enqueue(new Callback<BudgetItem>() {
@Override
public void onResponse(Call<BudgetItem> call, Response<BudgetItem> response) {
budgetItemArrayList.add(budgetItem);
adapter.notifyDataSetChanged();
}

@Override
public void onFailure(Call<BudgetItem> call, Throwable t) {
Log.e("MainActivity", "Couldn't save budgets");
}
});

}

public void setBudgetTrackerTextView(float budget) {
int positiveGreenColor = ContextCompat.getColor(this, R.color.green);
int negativeRedColor = ContextCompat.getColor(this, R.color.red);

/*
Equivalent to

int backgroundColor;
if (budget >= budgetLimit) {
backgroundColor = positiveGreenColor;
} else {
backgroundColor = negativeRedColor;
}
*/
int backgroundColor = (budget >= budgetLimit) ? positiveGreenColor: negativeRedColor;
budgetTrackerTextView.setBackgroundColor(backgroundColor);

String budgetText = Float.toString(budget - budgetLimit);
budgetTrackerTextView.setText("$" + budgetText);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == AddItemActivity.ADD_ITEM_REQUEST_CODE) {
Log.d(MainActivity.class.getSimpleName(), "Result from AddItemActivity");

if (data != null) {
Bundle args = data.getExtras();
BudgetItem budgetItem = args.getParcelable(
AddItemActivity.BUDGET_ITEM_EXTRA
);
Log.d(TAG, budgetItem.toString());

postBudget(budgetItem);

float sum = adapter.addBudgetItem(budgetItem);
setBudgetTrackerTextView(sum);
}

}
}
}
Loading