Skip to content

Commit 2904939

Browse files
authored
Merge pull request #2 from djubreel/master
Improvement: Added new methods to the Utils, Removed unneeded files also.
2 parents 24fe81c + fb5c64f commit 2904939

11 files changed

Lines changed: 260 additions & 43 deletions

File tree

app/src/main/java/com/cottacush/android/libraries/base/BackButtonFragment.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@
1212
*/
1313

1414
public abstract class BackButtonFragment<T extends BasePresenter> extends BaseFragment<T> {
15-
1615
BaseActivity baseActivity;
1716

18-
@Override
19-
public void onCreate(@Nullable Bundle savedInstanceState) {
20-
super.onCreate(savedInstanceState);
21-
setHasOptionsMenu(true);
22-
}
23-
2417
@Override
2518
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
2619
super.onViewCreated(view, savedInstanceState);
@@ -31,12 +24,5 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
3124
}
3225
}
3326

34-
@Override
35-
public boolean onOptionsItemSelected(MenuItem item) {
36-
if (item.getItemId() == android.R.id.home) {
37-
baseActivity.onBackPressed();
38-
return true;
39-
}
40-
return false;
41-
}
27+
4228
}

app/src/main/java/com/cottacush/android/libraries/base/BaseActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import android.view.View;
1111
import android.view.inputmethod.InputMethodManager;
1212

13-
import com.cottacush.android.libraries.utils.JsendResponse;
13+
import com.cottacush.android.libraries.utils.HttpResponseUtils;
1414
import com.cottacush.android.libraries.utils.MessageUtils;
1515
import com.cottacush.android.libraries.utils.NetworkUtils;
1616

@@ -56,7 +56,7 @@ protected void handleRemoteCallFailure(Throwable t, ProgressDialog dialog) {
5656
if (dialog != null) {
5757
dialog.dismiss();
5858
}
59-
showErrorDialog(JsendResponse.ERROR_MESSAGE);
59+
showErrorDialog(HttpResponseUtils.ERROR_MESSAGE);
6060
t.printStackTrace();
6161
}
6262

app/src/main/java/com/cottacush/android/libraries/utils/CacheConstants.java

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

app/src/main/java/com/cottacush/android/libraries/utils/Constants.java

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

app/src/main/java/com/cottacush/android/libraries/utils/DateUtils.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.cottacush.android.libraries.utils;
22

3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
import java.util.Locale;
8+
39
/**
410
* @author Adegoke Obasa <goke@cottacush.com>
511
*/
@@ -10,4 +16,65 @@ public class DateUtils {
1016
public static final String MONTH_DATE = "MMM dd";
1117
public static final String FULL_DATE = "dd MMMM yyyy";
1218
public static final String FULL_DATE_AND_TIME = "dd MMMM yyyy 'at' hh:mma";
19+
20+
/**
21+
* @param date
22+
* @return
23+
*/
24+
public static String formatDate(String date) {
25+
SimpleDateFormat sFrom = new SimpleDateFormat(MYSQL_DATE_ONLY, Locale.getDefault());
26+
SimpleDateFormat sTo = new SimpleDateFormat(FULL_DATE, Locale.getDefault());
27+
String result = null;
28+
try {
29+
result = sTo.format(sFrom.parse(date));
30+
} catch (ParseException e) {
31+
e.printStackTrace();
32+
}
33+
return result;
34+
}
35+
36+
/**
37+
* @param Time
38+
* @return
39+
*/
40+
public static String getRelativeTime(String Time) {
41+
String relativeTime = "";
42+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(MYSQL_FORMAT, Locale.getDefault());
43+
try {
44+
Date date = simpleDateFormat.parse(Time);
45+
relativeTime = (String) android.text.format.DateUtils.getRelativeTimeSpanString(date.getTime(), System.currentTimeMillis(), android.text.format.DateUtils.DAY_IN_MILLIS, android.text.format.DateUtils.FORMAT_ABBREV_ALL);
46+
} catch (ParseException e) {
47+
e.printStackTrace();
48+
}
49+
return relativeTime;
50+
}
51+
52+
/**
53+
* @param dateOfBirth
54+
* @param format e.g yyyy-mm-dd
55+
* @return
56+
*/
57+
public static int getAge(String dateOfBirth, String format) {
58+
Date birthdate;
59+
SimpleDateFormat df = new SimpleDateFormat(format);
60+
try {
61+
birthdate = df.parse(dateOfBirth);
62+
} catch (ParseException e) {
63+
e.printStackTrace();
64+
return 0;
65+
}
66+
Calendar dob = Calendar.getInstance();
67+
Calendar today = Calendar.getInstance();
68+
69+
dob.setTime(birthdate);
70+
today.setTime(Calendar.getInstance().getTime());
71+
72+
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
73+
74+
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
75+
age--;
76+
}
77+
78+
return age;
79+
}
1380
}

app/src/main/java/com/cottacush/android/libraries/utils/JsendResponse.java renamed to app/src/main/java/com/cottacush/android/libraries/utils/HttpResponseUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* @author Adegoke Obasa <goke@cottacush.com>
1414
*/
1515

16-
public class JsendResponse {
16+
public class HttpResponseUtils {
1717

1818
private JsonElement successBody;
1919
private JSONObject errorBody;
2020

2121
public static final String ERROR_MESSAGE = "An unexpected network error occurred.";
2222

23-
public JsendResponse(JsonElement successBody, ResponseBody errorBody) {
23+
public HttpResponseUtils(JsonElement successBody, ResponseBody errorBody) {
2424
this.successBody = successBody;
2525
if (errorBody != null) {
2626
try {

app/src/main/java/com/cottacush/android/libraries/utils/MessageUtils.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@
1313

1414
public class MessageUtils {
1515

16+
/**
17+
* @param view
18+
* @param message
19+
*/
1620
public static void showMessage(View view, String message) {
1721
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
1822
}
1923

24+
/**
25+
* @param view
26+
* @param message
27+
*/
2028
public static void showMessageIndef(View view, String message) {
2129
Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).show();
2230
}
2331

24-
public static void showMessage(View view, String message, String action, View.OnClickListener listener, int colorResId) {
32+
/**
33+
* @param view
34+
* @param message
35+
* @param action
36+
* @param listener
37+
*/
38+
public static void showMessage(View view, String message, String action, View.OnClickListener listener, int colorId) {
2539

2640
Context context = view.getContext();
2741
Snackbar snackbar = Snackbar.make(view, message
@@ -31,7 +45,8 @@ public static void showMessage(View view, String message, String action, View.On
3145

3246
View snackbarView = snackbar.getView();
3347
snackbarView.setBackgroundColor(
34-
context.getResources().getColor(colorResId));
48+
context.getResources().getColor(colorId));
3549
snackbar.show();
3650
}
51+
3752
}

app/src/main/java/com/cottacush/android/libraries/utils/PrefsUtils.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class PrefsUtils {
1515

1616
private Context context;
1717

18+
1819
public PrefsUtils(Context context) {
1920
this.context = context;
2021
}
@@ -24,67 +25,133 @@ private SharedPreferences getSharedPreferences() {
2425
Context.MODE_PRIVATE);
2526
}
2627

28+
/**
29+
* @param key
30+
* @param value
31+
*/
2732
public void putString(String key, String value) {
2833
getSharedPreferences().edit()
2934
.putString(key, value)
3035
.apply();
3136
}
3237

38+
/**
39+
* @param key
40+
* @param value
41+
*/
3342
public void putInt(String key, int value) {
3443
getSharedPreferences().edit()
3544
.putInt(key, value)
3645
.apply();
3746
}
3847

48+
/**
49+
* @param key
50+
* @param value
51+
*/
3952
public void putInt(String key, boolean value) {
4053
getSharedPreferences().edit()
4154
.putBoolean(key, value)
4255
.apply();
4356
}
4457

58+
/**
59+
* @param key
60+
* @param value
61+
*/
62+
4563
public void putFloat(String key, float value) {
4664
getSharedPreferences().edit()
4765
.putFloat(key, value)
4866
.apply();
4967
}
5068

69+
/**
70+
* @param key
71+
* @param values
72+
*/
5173
public void putStringSet(String key, Set<String> values) {
5274
getSharedPreferences().edit()
5375
.putStringSet(key, values)
5476
.apply();
5577
}
5678

79+
/**
80+
* @param key
81+
* @param value
82+
*/
5783
public void putLong(String key, Long value) {
5884
getSharedPreferences().edit()
5985
.putLong(key, value)
6086
.apply();
6187
}
6288

89+
/**
90+
* @param key
91+
* @param defaultValue
92+
* @return
93+
*/
6394
public String getString(String key, String defaultValue) {
6495
return getSharedPreferences().getString(key, defaultValue);
6596
}
6697

98+
/**
99+
* @param key
100+
* @param defaultValue
101+
* @return
102+
*/
67103
public int getInt(String key, int defaultValue) {
68104
return getSharedPreferences().getInt(key, defaultValue);
69105
}
70106

107+
/**
108+
* @param key
109+
* @param defaultValue
110+
* @return
111+
*/
71112
public boolean getBoolean(String key, boolean defaultValue) {
72113
return getSharedPreferences().getBoolean(key, defaultValue);
73114
}
74115

116+
/**
117+
* @param key
118+
* @param defaultValue
119+
* @return
120+
*/
75121
public float getFloat(String key, float defaultValue) {
76122
return getSharedPreferences().getFloat(key, defaultValue);
77123
}
78124

125+
/**
126+
* @param key
127+
* @param defaultValue
128+
* @return
129+
*/
79130
public long getLong(String key, long defaultValue) {
80131
return getSharedPreferences().getLong(key, defaultValue);
81132
}
82133

134+
/**
135+
* @param key
136+
* @param defaultValue
137+
* @return
138+
*/
83139
public Set<String> getStringSet(String key, Set<String> defaultValue) {
84140
return getSharedPreferences().getStringSet(key, defaultValue);
85141
}
86142

143+
/**
144+
* @param key
145+
*/
87146
public void remove(String key) {
88147
getSharedPreferences().edit().remove(key);
89148
}
149+
150+
/**
151+
* @param Key
152+
* @return
153+
*/
154+
public Boolean doesContain(String Key) {
155+
return getSharedPreferences().contains(Key);
156+
}
90157
}

app/src/main/java/com/cottacush/android/libraries/utils/RetrofitClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
public class RetrofitClient {
19-
public static final String OAUTH_SERVICE = "oauth_service";
2019

2120
public Retrofit build() {
2221
//TODO Parametrize the base URL

0 commit comments

Comments
 (0)