Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[30m]. Refactor AppUtils.
  • Loading branch information
Evgenii Kanivets committed Jan 26, 2016
commit f1952f50bbd409271905a00b1063a3f8395ff52c
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".util.MTApp"
android:name=".util.MtApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.blogspot.e_kanivets.moneytracker.fragment.ExportFragment;
import com.blogspot.e_kanivets.moneytracker.fragment.NavigationDrawerFragment;
import com.blogspot.e_kanivets.moneytracker.fragment.RecordsFragment;
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;

public class NavDrawerActivity extends AppCompatActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
Expand All @@ -35,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_nav_drawer);

/* Increment launch count */
AppUtils.addLaunchCount(NavDrawerActivity.this);
PrefUtils.addLaunchCount();

mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.blogspot.e_kanivets.moneytracker.model.Record;
import com.blogspot.e_kanivets.moneytracker.ui.AppRateDialog;
import com.blogspot.e_kanivets.moneytracker.ui.ChangeDateDialog;
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
break;
}

AppUtils.writeUsedPeriod(getActivity(), position);
PrefUtils.writeUsedPeriod(position);

update();

Expand Down Expand Up @@ -250,9 +250,7 @@ public void onGlobalLayout() {
if (isFirst) {
isFirst = false;
listView.setSelection(listView.getCount() - 1);
if (AppUtils.checkRateDialog(getActivity())) {
showAppRateDialog();
}
if (PrefUtils.checkRateDialog()) showAppRateDialog();
}
}
});
Expand All @@ -274,7 +272,7 @@ private void initActionBar() {
Spinner spinner = (Spinner) customNav.findViewById(R.id.spinner_period);
spinner.setAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.array_periods)));
spinner.setSelection(AppUtils.readUsedPeriod(getActivity()));
spinner.setSelection(PrefUtils.readUsedPeriod());
spinner.setOnItemSelectedListener(this);

if (actionBar != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.blogspot.e_kanivets.moneytracker.controller.CategoryController;
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
import com.blogspot.e_kanivets.moneytracker.util.MtApp;

import java.io.Serializable;

Expand Down Expand Up @@ -30,7 +30,7 @@ public Record(int id, long time, int type, String title, int categoryId, int pri
this.price = price;
this.accountId = accountId;

category = new CategoryController(new DbHelper(MTApp.get())).getCategoryById(categoryId);
category = new CategoryController(new DbHelper(MtApp.get())).getCategoryById(categoryId);
}

public int getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import android.util.Pair;

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
import com.blogspot.e_kanivets.moneytracker.util.MtApp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -110,11 +108,11 @@ private void fillSummaryReportList(int totalIncome, int totalExpense) {
//Add summary row to list
summaryReportList = new ArrayList<Pair<String, Integer>>();
summaryReportList.add(new Pair<String, Integer>(
MTApp.get().getResources().getString(R.string.total_incomes) + " :", totalIncome));
MtApp.get().getResources().getString(R.string.total_incomes) + " :", totalIncome));
summaryReportList.add(new Pair<String, Integer>(
MTApp.get().getResources().getString(R.string.total_expenses) + " :", totalExpense));
MtApp.get().getResources().getString(R.string.total_expenses) + " :", totalExpense));
summaryReportList.add(new Pair<String, Integer>(
MTApp.get().getResources().getString(R.string.total) + " :", totalExpense + totalIncome));
MtApp.get().getResources().getString(R.string.total) + " :", totalExpense + totalIncome));
}

private void fillRecordList(HashMap<String, Record> recordMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import android.widget.Button;

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
import com.blogspot.e_kanivets.moneytracker.util.Constants;

public class AppRateDialog extends AlertDialog {

private Context context;

public AppRateDialog(Context context) {
Expand All @@ -36,8 +35,8 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View view) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(Constants.GP_MARKET + Constants.APP_NAME)));
AppUtils.appRated(context);
Uri.parse(Constants.GP_MARKET + context.getPackageName())));
PrefUtils.appRated();
dismiss();
}
});
Expand All @@ -52,7 +51,7 @@ public void onClick(View view) {
thanksButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppUtils.appRated(context);
PrefUtils.appRated();
dismiss();
}
});
Expand All @@ -66,4 +65,4 @@ public void dismiss() {
e.printStackTrace();
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

/**
* Constants class. It's better to change it to interface.
* Created by eugene on 29/08/14.
* Created on 29/08/14.
*
* @author Evgenii Kanivets
*/
public class Constants {
public static final String APP_NAME = "com.blogspot.e_kanivets.moneytracker";
public static final String GP_MARKET = "market://details?id=";
public static final String APP_RATED = "app_rated";
public static final String LAUNCH_COUNT = "launch_count";
public static final int RATE_PERIOD = 5;
public static final String CONTRIBUTION = "contribution";
public static final String KEY_USED_PERIOD = "key_used_period";
public interface Constants {
String GP_MARKET = "market://details?id=";
String APP_RATED = "app_rated";
String LAUNCH_COUNT = "launch_count";
int RATE_PERIOD = 5;
String CONTRIBUTION = "contribution";
String KEY_USED_PERIOD = "key_used_period";

public static final int DEFAULT_USED_PERIOD = 1;
int DEFAULT_USED_PERIOD = 1;

public static final String TITLE_PARAM_NAME = "title";
public static final String PRICE_PARAM_NAME = "price";
String TITLE_PARAM_NAME = "title";
String PRICE_PARAM_NAME = "price";

public static final String DEFAULT_EXPORT_FILE_NAME = "money_tracker.csv";
}
String DEFAULT_EXPORT_FILE_NAME = "money_tracker.csv";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*
* @author Evgenii Kanivets
*/
public class MTApp extends Application {
public class MtApp extends Application {

private static MTApp mtApp;
private static MtApp mtApp;

public static MTApp get() {
public static MtApp get() {
return mtApp;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.blogspot.e_kanivets.moneytracker.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
* Util class for application.
* Created on 02/09/14.
*
* @author Evgenii Kanivets
*/
public class PrefUtils {
public static void addLaunchCount() {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(Constants.LAUNCH_COUNT, preferences.getInt(Constants.LAUNCH_COUNT, 0) + 1);
editor.apply();
}

public static boolean checkRateDialog() {
SharedPreferences preferences = getDefaultPrefs();

boolean appRated = preferences.getBoolean(Constants.APP_RATED, false);
if (appRated) return false;

int launchCount = preferences.getInt(Constants.LAUNCH_COUNT, 0);
return launchCount % Constants.RATE_PERIOD == 0;
}

public static void appRated() {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(Constants.APP_RATED, true);
editor.apply();
}

public static int readUsedPeriod() {
SharedPreferences preferences = getDefaultPrefs();
return preferences.getInt(Constants.KEY_USED_PERIOD, Constants.DEFAULT_USED_PERIOD);
}

public static void writeUsedPeriod(int usedPeriod) {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(Constants.KEY_USED_PERIOD, usedPeriod);
editor.apply();
}

private static SharedPreferences getDefaultPrefs() {
return MtApp.get().getSharedPreferences(MtApp.get().getPackageName(), Context.MODE_PRIVATE);
}
}