Skip to content

Commit 7d15e0b

Browse files
committed
- added more functionality to Base Classes.
- added more options to Alerts.java.
1 parent 7705050 commit 7d15e0b

File tree

5 files changed

+254
-12
lines changed

5 files changed

+254
-12
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

base/src/main/java/com/base/activities/BaseActivity.java

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
package com.base.activities;
22

33
import android.app.Dialog;
4+
import android.content.Intent;
45
import android.content.res.Configuration;
6+
import android.graphics.Color;
7+
import android.os.Build;
58
import android.os.Bundle;
69
import android.view.View;
710

811
import androidx.annotation.Nullable;
912
import androidx.appcompat.app.AppCompatActivity;
1013

1114
import com.base.R;
15+
import com.base.fragments.BaseFragment;
16+
import com.base.interfases.OnBackHandler;
1217
import com.base.utils.Alerts;
1318
import com.base.utils.AppSession;
1419

1520
import java.util.Locale;
1621

17-
public abstract class BaseActivity extends AppCompatActivity {
22+
public abstract class BaseActivity extends AppCompatActivity implements
23+
//to identify child tasks and perform on activity itself
24+
View.OnClickListener {
25+
protected String TAG;
26+
private OnBackHandler backHandler;
27+
private BaseFragment fragment = null;
1828

1929
@Override
2030
protected void onCreate(@Nullable Bundle savedInstanceState) {
2131
super.onCreate(savedInstanceState);
22-
32+
TAG = this.getLocalClassName();
2333
Configuration config;
2434
config = new Configuration(getResources().getConfiguration());
2535
config.setLocale(new Locale(AppSession.getAccept_Language().toLowerCase()));
@@ -30,8 +40,99 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3040
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
3141
}
3242

43+
/**
44+
* This method is used to initialize UI of the layout. Called in onCreate()
45+
*/
3346
protected abstract View getView();
3447

48+
protected final Bundle getBundle() {
49+
Bundle bundle = getIntent().getExtras();
50+
if (bundle == null) {
51+
bundle = new Bundle();
52+
}
53+
return bundle;
54+
55+
}
56+
57+
/**
58+
* Gets back handler.
59+
*
60+
* @return OnBackHandler
61+
*/
62+
public OnBackHandler getBackHandler() {
63+
return backHandler;
64+
}
65+
66+
/**
67+
* Sets back handler.
68+
*
69+
* @param backHandler OnBackHandler
70+
*/
71+
public void setBackHandler(OnBackHandler backHandler) {
72+
this.backHandler = backHandler;
73+
}
74+
75+
@Override
76+
public void onBackPressed() {
77+
if (getBackHandler() != null) {
78+
getBackHandler().onBackPressed();
79+
} else {
80+
super.onBackPressed();
81+
}
82+
}
83+
84+
/**
85+
* Sets fragment when called another activity or application eg camera or gallery
86+
* After completed operation set this to null.
87+
*
88+
* @param fragment the fragment
89+
*/
90+
public void setOnActivityResultFragment(BaseFragment fragment) {
91+
this.fragment = fragment;
92+
}
93+
94+
/**
95+
* restarts current Activity
96+
*/
97+
public void restart() {
98+
super.recreate();
99+
}
100+
101+
@Override
102+
public void onClick(View v) {
103+
104+
}
105+
106+
@Override
107+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
108+
if (fragment != null) {
109+
fragment.onActivityResult(requestCode, resultCode, data);
110+
}
111+
super.onActivityResult(requestCode, resultCode, data);
112+
}
113+
114+
/**
115+
* set Status Bar Color
116+
*
117+
* @param color Color
118+
*/
119+
public void setStatusBarColor(int color) {
120+
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
121+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
122+
getWindow().setStatusBarColor(color);
123+
}
124+
125+
/**
126+
* set Status Bar Color by Hash
127+
*
128+
* @param color String
129+
*/
130+
public void setStatusBarColor(String color) {
131+
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
132+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
133+
getWindow().setStatusBarColor(Color.parseColor(color));
134+
}
135+
35136
/**
36137
* shows message in Alert Dialog
37138
*

base/src/main/java/com/base/fragments/BaseFragment.java

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.base.fragments;
22

3-
import android.app.Activity;
43
import android.app.Dialog;
4+
import android.content.Context;
55
import android.os.Bundle;
66
import android.view.LayoutInflater;
77
import android.view.View;
@@ -10,24 +10,83 @@
1010
import androidx.annotation.NonNull;
1111
import androidx.annotation.Nullable;
1212
import androidx.fragment.app.Fragment;
13+
import androidx.fragment.app.FragmentActivity;
14+
import androidx.fragment.app.FragmentManager;
1315

1416
import com.base.R;
17+
import com.base.activities.BaseActivity;
18+
import com.base.interfases.OnBackHandler;
1519
import com.base.utils.Alerts;
1620

17-
public abstract class BaseFragment extends Fragment {
18-
private Activity activity;
21+
public abstract class BaseFragment extends Fragment implements View.OnClickListener, OnBackHandler {
22+
protected View view;
23+
private boolean enableBackHandle = false;
1924

2025
@Nullable
2126
@Override
2227
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
23-
activity = getActivity();
2428
return getView();
2529
}
2630

2731
public abstract View getView();
2832

29-
public Activity getFragment() {
30-
return activity;
33+
public FragmentActivity getFragment() {
34+
return getActivity();
35+
}
36+
37+
protected Bundle getBundle() {
38+
Bundle bundle = getArguments();
39+
if (bundle == null) {
40+
bundle = new Bundle();
41+
}
42+
return bundle;
43+
44+
}
45+
46+
/**
47+
* Sets enable back handle.
48+
*
49+
* @param enableBackHandle the enable back handle
50+
*/
51+
public void setEnableBackHandle(boolean enableBackHandle) {
52+
this.enableBackHandle = enableBackHandle;
53+
}
54+
55+
@Override
56+
public Context getContext() {
57+
return getActivity();
58+
}
59+
60+
/**
61+
* gets fragment manager
62+
*
63+
* @return FragmentManager
64+
*/
65+
public FragmentManager getFragManager() {
66+
return getChildFragmentManager();
67+
}
68+
69+
@Override
70+
public void onResume() {
71+
/*
72+
Handle BackPress on Fragment.
73+
*/
74+
if (getActivity() instanceof BaseActivity) {
75+
((BaseActivity) getActivity()).setBackHandler(null);
76+
if (enableBackHandle)
77+
((BaseActivity) getActivity()).setBackHandler(this);
78+
}
79+
super.onResume();
80+
}
81+
82+
@Override
83+
public void onClick(View v) {
84+
85+
}
86+
87+
@Override
88+
public void onBackPressed() {
89+
3190
}
3291

3392
/**
@@ -36,7 +95,7 @@ public Activity getFragment() {
3695
* @param message String
3796
*/
3897
public void showDialog(String message) {
39-
Alerts.with(activity).show(getString(R.string.app_name), message, true, false, new Alerts.DialogInterface() {
98+
Alerts.with(getActivity()).show(getString(R.string.app_name), message, true, false, new Alerts.DialogInterface() {
4099
@Override
41100
public void onPositiveClick(Dialog dialog) {
42101
dialog.dismiss();
@@ -55,6 +114,6 @@ public void onNegativeClick(Dialog dialog) {
55114
* @param message String
56115
*/
57116
public void showMessage(String message) {
58-
Alerts.with(activity).showToast(message);
117+
Alerts.with(getActivity()).showToast(message);
59118
}
60119
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.base.interfases;
2+
3+
4+
/**
5+
* The interface On back handler.
6+
*/
7+
public interface OnBackHandler {
8+
/**
9+
* On back pressed.
10+
*/
11+
void onBackPressed();
12+
}

base/src/main/java/com/base/utils/Alerts.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Dialog;
44
import android.content.Context;
5+
import android.graphics.Color;
56
import android.text.Layout;
67
import android.text.Spannable;
78
import android.text.SpannableString;
@@ -13,6 +14,7 @@
1314
import android.widget.Toast;
1415

1516
import com.base.R;
17+
import com.google.android.material.snackbar.Snackbar;
1618

1719
/**
1820
* Custom Alerts Helper Class
@@ -22,7 +24,11 @@
2224
public class Alerts {
2325
private Dialog dialog;
2426
private Context mContext;
25-
private String TAG = "DebugTag";
27+
public static final int SNAKE_BAR_SHORT = -1;
28+
public static final int SNAKE_BAR_LONG = 0;
29+
30+
public static final int TOAST_SHORT = 0;
31+
public static final int TOAST_LONG = 1;
2632

2733
public Alerts() {
2834
}
@@ -45,6 +51,19 @@ public void showToast(String text) {
4551
Toast.makeText(mContext, centeredText, Toast.LENGTH_SHORT).show();
4652
}
4753

54+
/**
55+
* shows centered text
56+
*
57+
* @param text text
58+
* @param duration int
59+
*/
60+
public void showToast(String text, int duration) {
61+
Spannable centeredText = new SpannableString(text);
62+
centeredText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
63+
0, text.length() - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
64+
Toast.makeText(mContext, centeredText, duration).show();
65+
}
66+
4867
/**
4968
* Custom dialog with title, message and buttons
5069
*
@@ -172,6 +191,57 @@ public void show(String title, String body, boolean isHasPositive, String positi
172191
dialog.show();
173192
}
174193

194+
/**
195+
* shows Snake Bar with message only
196+
*
197+
* @param mainView View
198+
* @param message String
199+
* @param duration int
200+
*/
201+
public void showSnakeBar(View mainView, String message, int duration) {
202+
Snackbar.make(mainView, message, Snackbar.LENGTH_LONG).setDuration(duration).show();
203+
}
204+
205+
/**
206+
* shows Snake Bar with message only
207+
*
208+
* @param mainView View
209+
* @param message String
210+
* @param actionText String
211+
* @param duration int
212+
*/
213+
public void showSnakeBar(View mainView, String message, String actionText, int duration) {
214+
Snackbar.make(mainView, message, Snackbar.LENGTH_LONG)
215+
.setAction(actionText, null)
216+
.setDuration(duration)
217+
.show();
218+
}
219+
220+
/**
221+
* shows Snake Bar with acton callback
222+
*
223+
* @param mainView View
224+
* @param message String
225+
* @param textColorHash String
226+
* @param actionText String
227+
* @param actionTextColorHash String
228+
* @param duration int
229+
* @param callback Callback
230+
*/
231+
public void showSnakeBar(View mainView, String message, String textColorHash, String actionText, String actionTextColorHash, String backGroundColor, int duration, Callback callback) {
232+
Snackbar.make(mainView, message, Snackbar.LENGTH_LONG)
233+
.setAction(actionText, callback::onActionClicked)
234+
.setBackgroundTint(Color.parseColor(backGroundColor))
235+
.setActionTextColor(Color.parseColor(actionTextColorHash))
236+
.setTextColor(Color.parseColor(textColorHash))
237+
.setDuration(duration)
238+
.show();
239+
}
240+
241+
public interface Callback {
242+
void onActionClicked(View view);
243+
}
244+
175245
public interface DialogInterface {
176246
void onPositiveClick(Dialog dialog);
177247

0 commit comments

Comments
 (0)