Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit f1952f5

Browse files
author
Evgenii Kanivets
committed
[30m]. Refactor AppUtils.
1 parent 7a5011b commit f1952f5

File tree

10 files changed

+88
-129
lines changed

10 files changed

+88
-129
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
99

1010
<application
11-
android:name=".util.MTApp"
11+
android:name=".util.MtApp"
1212
android:allowBackup="true"
1313
android:icon="@drawable/ic_launcher"
1414
android:label="@string/app_name"

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/NavDrawerActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.blogspot.e_kanivets.moneytracker.fragment.ExportFragment;
1414
import com.blogspot.e_kanivets.moneytracker.fragment.NavigationDrawerFragment;
1515
import com.blogspot.e_kanivets.moneytracker.fragment.RecordsFragment;
16-
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
16+
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
1717

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

3737
/* Increment launch count */
38-
AppUtils.addLaunchCount(NavDrawerActivity.this);
38+
PrefUtils.addLaunchCount();
3939

4040
mNavigationDrawerFragment = (NavigationDrawerFragment)
4141
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/RecordsFragment.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.blogspot.e_kanivets.moneytracker.model.Record;
3535
import com.blogspot.e_kanivets.moneytracker.ui.AppRateDialog;
3636
import com.blogspot.e_kanivets.moneytracker.ui.ChangeDateDialog;
37-
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
37+
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
3838

3939
import java.util.Calendar;
4040
import java.util.Date;
@@ -180,7 +180,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
180180
break;
181181
}
182182

183-
AppUtils.writeUsedPeriod(getActivity(), position);
183+
PrefUtils.writeUsedPeriod(position);
184184

185185
update();
186186

@@ -250,9 +250,7 @@ public void onGlobalLayout() {
250250
if (isFirst) {
251251
isFirst = false;
252252
listView.setSelection(listView.getCount() - 1);
253-
if (AppUtils.checkRateDialog(getActivity())) {
254-
showAppRateDialog();
255-
}
253+
if (PrefUtils.checkRateDialog()) showAppRateDialog();
256254
}
257255
}
258256
});
@@ -274,7 +272,7 @@ private void initActionBar() {
274272
Spinner spinner = (Spinner) customNav.findViewById(R.id.spinner_period);
275273
spinner.setAdapter(new ArrayAdapter<>(getActivity(),
276274
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.array_periods)));
277-
spinner.setSelection(AppUtils.readUsedPeriod(getActivity()));
275+
spinner.setSelection(PrefUtils.readUsedPeriod());
278276
spinner.setOnItemSelectedListener(this);
279277

280278
if (actionBar != null) {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Record.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.blogspot.e_kanivets.moneytracker.controller.CategoryController;
44
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
5-
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
5+
import com.blogspot.e_kanivets.moneytracker.util.MtApp;
66

77
import java.io.Serializable;
88

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

33-
category = new CategoryController(new DbHelper(MTApp.get())).getCategoryById(categoryId);
33+
category = new CategoryController(new DbHelper(MtApp.get())).getCategoryById(categoryId);
3434
}
3535

3636
public int getId() {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Report.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import android.util.Pair;
44

55
import com.blogspot.e_kanivets.moneytracker.R;
6-
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
6+
import com.blogspot.e_kanivets.moneytracker.util.MtApp;
77

88
import java.util.ArrayList;
9-
import java.util.Arrays;
10-
import java.util.Collections;
119
import java.util.HashMap;
1210
import java.util.List;
1311

@@ -110,11 +108,11 @@ private void fillSummaryReportList(int totalIncome, int totalExpense) {
110108
//Add summary row to list
111109
summaryReportList = new ArrayList<Pair<String, Integer>>();
112110
summaryReportList.add(new Pair<String, Integer>(
113-
MTApp.get().getResources().getString(R.string.total_incomes) + " :", totalIncome));
111+
MtApp.get().getResources().getString(R.string.total_incomes) + " :", totalIncome));
114112
summaryReportList.add(new Pair<String, Integer>(
115-
MTApp.get().getResources().getString(R.string.total_expenses) + " :", totalExpense));
113+
MtApp.get().getResources().getString(R.string.total_expenses) + " :", totalExpense));
116114
summaryReportList.add(new Pair<String, Integer>(
117-
MTApp.get().getResources().getString(R.string.total) + " :", totalExpense + totalIncome));
115+
MtApp.get().getResources().getString(R.string.total) + " :", totalExpense + totalIncome));
118116
}
119117

120118
private void fillRecordList(HashMap<String, Record> recordMap) {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/AppRateDialog.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
import android.widget.Button;
1010

1111
import com.blogspot.e_kanivets.moneytracker.R;
12-
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
12+
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
1313
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1414

1515
public class AppRateDialog extends AlertDialog {
16-
1716
private Context context;
1817

1918
public AppRateDialog(Context context) {
@@ -36,8 +35,8 @@ protected void onCreate(Bundle savedInstanceState) {
3635
@Override
3736
public void onClick(View view) {
3837
context.startActivity(new Intent(Intent.ACTION_VIEW,
39-
Uri.parse(Constants.GP_MARKET + Constants.APP_NAME)));
40-
AppUtils.appRated(context);
38+
Uri.parse(Constants.GP_MARKET + context.getPackageName())));
39+
PrefUtils.appRated();
4140
dismiss();
4241
}
4342
});
@@ -52,7 +51,7 @@ public void onClick(View view) {
5251
thanksButton.setOnClickListener(new View.OnClickListener() {
5352
@Override
5453
public void onClick(View view) {
55-
AppUtils.appRated(context);
54+
PrefUtils.appRated();
5655
dismiss();
5756
}
5857
});
@@ -66,4 +65,4 @@ public void dismiss() {
6665
e.printStackTrace();
6766
}
6867
}
69-
}
68+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/AppUtils.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/Constants.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
/**
44
* Constants class. It's better to change it to interface.
5-
* Created by eugene on 29/08/14.
5+
* Created on 29/08/14.
6+
*
7+
* @author Evgenii Kanivets
68
*/
7-
public class Constants {
8-
public static final String APP_NAME = "com.blogspot.e_kanivets.moneytracker";
9-
public static final String GP_MARKET = "market://details?id=";
10-
public static final String APP_RATED = "app_rated";
11-
public static final String LAUNCH_COUNT = "launch_count";
12-
public static final int RATE_PERIOD = 5;
13-
public static final String CONTRIBUTION = "contribution";
14-
public static final String KEY_USED_PERIOD = "key_used_period";
9+
public interface Constants {
10+
String GP_MARKET = "market://details?id=";
11+
String APP_RATED = "app_rated";
12+
String LAUNCH_COUNT = "launch_count";
13+
int RATE_PERIOD = 5;
14+
String CONTRIBUTION = "contribution";
15+
String KEY_USED_PERIOD = "key_used_period";
1516

16-
public static final int DEFAULT_USED_PERIOD = 1;
17+
int DEFAULT_USED_PERIOD = 1;
1718

18-
public static final String TITLE_PARAM_NAME = "title";
19-
public static final String PRICE_PARAM_NAME = "price";
19+
String TITLE_PARAM_NAME = "title";
20+
String PRICE_PARAM_NAME = "price";
2021

21-
public static final String DEFAULT_EXPORT_FILE_NAME = "money_tracker.csv";
22-
}
22+
String DEFAULT_EXPORT_FILE_NAME = "money_tracker.csv";
23+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/MTApp.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*
99
* @author Evgenii Kanivets
1010
*/
11-
public class MTApp extends Application {
11+
public class MtApp extends Application {
1212

13-
private static MTApp mtApp;
13+
private static MtApp mtApp;
1414

15-
public static MTApp get() {
15+
public static MtApp get() {
1616
return mtApp;
1717
}
1818

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.blogspot.e_kanivets.moneytracker.util;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
/**
7+
* Util class for application.
8+
* Created on 02/09/14.
9+
*
10+
* @author Evgenii Kanivets
11+
*/
12+
public class PrefUtils {
13+
public static void addLaunchCount() {
14+
SharedPreferences preferences = getDefaultPrefs();
15+
SharedPreferences.Editor editor = preferences.edit();
16+
editor.putInt(Constants.LAUNCH_COUNT, preferences.getInt(Constants.LAUNCH_COUNT, 0) + 1);
17+
editor.apply();
18+
}
19+
20+
public static boolean checkRateDialog() {
21+
SharedPreferences preferences = getDefaultPrefs();
22+
23+
boolean appRated = preferences.getBoolean(Constants.APP_RATED, false);
24+
if (appRated) return false;
25+
26+
int launchCount = preferences.getInt(Constants.LAUNCH_COUNT, 0);
27+
return launchCount % Constants.RATE_PERIOD == 0;
28+
}
29+
30+
public static void appRated() {
31+
SharedPreferences preferences = getDefaultPrefs();
32+
SharedPreferences.Editor editor = preferences.edit();
33+
editor.putBoolean(Constants.APP_RATED, true);
34+
editor.apply();
35+
}
36+
37+
public static int readUsedPeriod() {
38+
SharedPreferences preferences = getDefaultPrefs();
39+
return preferences.getInt(Constants.KEY_USED_PERIOD, Constants.DEFAULT_USED_PERIOD);
40+
}
41+
42+
public static void writeUsedPeriod(int usedPeriod) {
43+
SharedPreferences preferences = getDefaultPrefs();
44+
SharedPreferences.Editor editor = preferences.edit();
45+
editor.putInt(Constants.KEY_USED_PERIOD, usedPeriod);
46+
editor.apply();
47+
}
48+
49+
private static SharedPreferences getDefaultPrefs() {
50+
return MtApp.get().getSharedPreferences(MtApp.get().getPackageName(), Context.MODE_PRIVATE);
51+
}
52+
}

0 commit comments

Comments
 (0)