Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. public class BaseActivity extends AppCompatActivity implements MvpView {
  2.  
  3. private static final String TAG = BaseActivity.class.getSimpleName();
  4. private static final String KEY_ACTIVITY_ID = "KEY_ACTIVITY_ID";
  5. private static final AtomicLong NEXT_ID = new AtomicLong(0);
  6. private static final Map<Long, ConfigPersistentComponent> sComponentsMap = new HashMap<>();
  7.  
  8. private ActivityComponent mActivityComponent;
  9. private long mActivityId;
  10. int statusCode;
  11. protected Dialog progressDialog;
  12.  
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. // set all activity to portrait
  18. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  19.  
  20. // Create the ActivityComponent and reuses cached ConfigPersistentComponent if this is
  21. // being called after a configuration change.
  22. mActivityId = savedInstanceState != null ?
  23. savedInstanceState.getLong(KEY_ACTIVITY_ID) : NEXT_ID.getAndIncrement();
  24. ConfigPersistentComponent configPersistentComponent;
  25. if (!sComponentsMap.containsKey(mActivityId)) {
  26. Log.d(TAG, "Creating new ConfigPersistentComponent id= " + mActivityId);
  27. configPersistentComponent = DaggerConfigPersistentComponent.builder()
  28. .applicationComponent(Apps.get(this).getComponent())
  29. .build();
  30. sComponentsMap.put(mActivityId, configPersistentComponent);
  31. } else {
  32. Log.i(TAG, "Reusing ConfigPersistentComponent id= " + mActivityId);
  33. configPersistentComponent = sComponentsMap.get(mActivityId);
  34. }
  35. mActivityComponent = configPersistentComponent.activityComponent(new ActivityModule(this));
  36.  
  37. }
  38.  
  39. public ActivityComponent activityComponent() {
  40. return mActivityComponent;
  41. }
  42.  
  43. @Override
  44. protected void onSaveInstanceState(Bundle outState) {
  45. Log.v(TAG, "onSaveInstanceState " + this.getClass().getSimpleName());
  46. super.onSaveInstanceState(outState);
  47. outState.putLong(KEY_ACTIVITY_ID, mActivityId);
  48. }
  49.  
  50. @Override
  51. protected void onDestroy() {
  52. Log.v(TAG, "onDestroy " + this.getClass().getSimpleName());
  53. if (!isChangingConfigurations()) {
  54. Log.d(TAG, "Clearing ConfigPersistentComponent id= " + mActivityId);
  55. sComponentsMap.remove(mActivityId);
  56. }
  57.  
  58. super.onDestroy();
  59. }
  60.  
  61. @Override
  62. public void showLoading(boolean isShow) {
  63. Log.d(TAG, "BaseActivity showLoading");
  64. }
  65.  
  66. @Override
  67. public void showNoDataFound(boolean isNoData) {
  68.  
  69. }
  70.  
  71. @Override
  72. public void showError(Throwable error) {
  73.  
  74. }
  75.  
  76. // @Override
  77. // public void showNoDataFound(boolean isNoData) {
  78. // Log.d(TAG, "BaseActivity showNoDataFound");
  79. //
  80. // String nMessage = getResources().getString(R.string.info_notfound);
  81. // showDialog(nMessage);
  82. // }
  83.  
  84. // @Override
  85. // @CallSuper
  86. // public void showError(Throwable error) {
  87. //
  88. // Log.d(TAG, "BaseActivity showError");
  89. //
  90. // if (error instanceof HttpException) {
  91. // showDialog(((HttpException) error).message());
  92. // } else if (error instanceof SocketTimeoutException) {
  93. // showDialog(error.getMessage());
  94. // } else if (error instanceof IOException) {
  95. // showDialog(error.getMessage());
  96. // } else {
  97. // showDialog(error.getMessage());
  98. // }
  99. //
  100. // }
  101.  
  102. protected String statusCode( Throwable error){
  103.  
  104. HttpException exception = (HttpException) error;
  105. statusCode = ((HttpException) error).code();
  106. String nMessageCode = String.valueOf(statusCode);
  107. return nMessageCode;
  108. }
  109.  
  110.  
  111. public static boolean isOnline(Context c) {
  112. ConnectivityManager cm = (ConnectivityManager) c
  113. .getSystemService(Context.CONNECTIVITY_SERVICE);
  114. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  115. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  116. return true;
  117. }
  118. return false;
  119. }
  120.  
  121.  
  122. // @Override
  123. // public void onResponse(Call call, ResponseProduct response) throws IOException {
  124. // int nA = response.code();
  125. // String nMessage = String.valueOf(nA);
  126. // showDialog(nMessage);
  127. //
  128. // }
  129.  
  130.  
  131. // protected void showDialog(final String message) {
  132. // final AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
  133. //
  134. // String nTitle = getResources().getString(R.string.warning_memotus_service);
  135. // builder.setTitle(R.string.warning);
  136. // if (message != null) {
  137. // builder.setMessage(Html.fromHtml(nTitle + " - ( " + message + " )"));
  138. // } else {
  139. // builder.setMessage("");
  140. // }
  141. // builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
  142. // @Override
  143. // public void onClick(DialogInterface dialog, int which) {
  144. // dialog.dismiss();
  145. // Intent i = new Intent(getApplicationContext(), MenuMainBeforeLogin.class);
  146. // startActivity(i);
  147. // finish();
  148. //
  149. // }
  150. // });
  151. // builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  152. // @Override
  153. // public void onClick(DialogInterface dialog, int which) {
  154. // dialog.dismiss();
  155. // Intent i = new Intent(getApplicationContext(), MenuMainBeforeLogin.class);
  156. // startActivity(i);
  157. // finish();
  158. //
  159. // }
  160. // });
  161. // if (!isFinishing()) {
  162. // builder.show();
  163. // }
  164. // }
  165.  
  166. protected void showProgressDialog() {
  167. if (progressDialog == null || !progressDialog.isShowing()) {
  168. progressDialog = new ProgressDialog(this);
  169. progressDialog.setCanceledOnTouchOutside(false);
  170. if (!isFinishing()) {
  171. progressDialog.show();
  172. }
  173. }
  174. }
  175.  
  176. protected void hideProgressDialog() {
  177. if (progressDialog != null && progressDialog.isShowing()) {
  178. if (!isFinishing()) {
  179. progressDialog.dismiss();
  180. }
  181. }
  182. }
  183.  
  184. @Override
  185. public void showProgress() {
  186. showProgressDialog();
  187. }
  188.  
  189. @Override
  190. public void hideProgress() {
  191. hideProgressDialog();
  192. }
  193.  
  194. @Override
  195. public void onResponse(Call call, Response response) throws IOException {
  196.  
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement