BulletinActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.cygnusa.hyperm;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Color;
  6. import android.graphics.PorterDuff;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.view.MenuItem;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.view.inputmethod.InputMethodManager;
  15. import android.widget.EditText;
  16. import android.widget.ProgressBar;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. import androidx.appcompat.app.AppCompatActivity;
  20. import androidx.appcompat.widget.Toolbar;
  21. import androidx.recyclerview.widget.LinearLayoutManager;
  22. import androidx.recyclerview.widget.RecyclerView;
  23. import com.google.firebase.analytics.FirebaseAnalytics;
  24. import com.google.gson.Gson;
  25. import com.cygnusa.hyperm.adapter.BulletinAdapter;
  26. import com.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
  27. import com.cygnusa.hyperm.fancyshowcase.FocusShape;
  28. import com.cygnusa.hyperm.util.ApiService;
  29. import com.cygnusa.hyperm.util.GlobalData;
  30. import com.cygnusa.hyperm.util.MainApplication;
  31. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  32. import com.cygnusa.hyperm.util.UtilService;
  33. import com.cygnusa.hyperm.wrappers.Bulletin;
  34. import com.cygnusa.hyperm.wrappers.BulletinResponse;
  35. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  36. import com.tylersuehr.esr.EmptyStateRecyclerView;
  37. import com.tylersuehr.esr.TextStateDisplay;
  38. import java.util.ArrayList;
  39. import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
  40. import retrofit2.Call;
  41. import retrofit2.Callback;
  42. import retrofit2.Response;
  43. public class BulletinActivity extends AppCompatActivity {
  44. ApiService restService;
  45. TransparentProgressDialog progressDialog;
  46. EmptyStateRecyclerView bulletinList;
  47. TextView title;
  48. ProgressBar progressBar;
  49. LinearLayoutManager linearLayoutManager;
  50. BulletinAdapter adapter = null;
  51. int TOTAL_RECORD = 0;
  52. int PAGE = 0;
  53. public static boolean isFromAMHome = false;
  54. int LIMIT = 10;
  55. ArrayList<Bulletin> MY_CONTENTS = new ArrayList<>();
  56. public Bulletin Bulletin = new Bulletin();
  57. private FirebaseAnalytics firebaseAnalytics;
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.activity_bulletin);
  62. setupWindowAnimations();
  63. setupUI(findViewById(R.id.bgLayout));
  64. firebaseAnalytics = FirebaseAnalytics.getInstance(this);
  65. restService = ((MainApplication) getApplication()).getClient();
  66. title = findViewById(R.id.title);
  67. bulletinList = findViewById(R.id.topicList);
  68. bulletinList.setHasFixedSize(true);
  69. bulletinList.setItemAnimator(new SlideInLeftAnimator());
  70. linearLayoutManager = new LinearLayoutManager(getBaseContext());
  71. bulletinList.setLayoutManager(linearLayoutManager);
  72. bulletinList.setStateDisplay(EmptyStateRecyclerView.STATE_EMPTY,
  73. new TextStateDisplay(this, "No data available", ""));
  74. title.setText("Bulletin");
  75. bulletinList.addOnScrollListener(recyclerViewOnScrollListener);
  76. progressBar = findViewById(R.id.progressBar);
  77. Toolbar toolbar = findViewById(R.id.toolbar);
  78. setSupportActionBar(toolbar);
  79. toolbar.setNavigationIcon(R.mipmap.back);
  80. toolbar.setContentInsetStartWithNavigation(0);
  81. toolbar.setTitle("");
  82. getSupportActionBar().setTitle("");
  83. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  84. getSupportActionBar().setDisplayShowHomeEnabled(true);
  85. @SuppressLint("PrivateResource")
  86. Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
  87. backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
  88. getSupportActionBar().setHomeAsUpIndicator(backArrow);
  89. bulletinList.post(new Runnable() {
  90. @Override
  91. public void run() {
  92. getBulletinContents(true);
  93. }
  94. });
  95. findViewById(R.id.helpIcon).setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View v) {
  98. // showHelpScreen();
  99. }
  100. });
  101. Bundle bundle = new Bundle();
  102. /*bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "1");
  103. bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "BulletinActivity");
  104. //bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
  105. firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);*/
  106. }
  107. private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
  108. @Override
  109. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  110. super.onScrollStateChanged(recyclerView, newState);
  111. }
  112. @Override
  113. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  114. super.onScrolled(recyclerView, dx, dy);
  115. int visibleItemCount = linearLayoutManager.getChildCount();
  116. int totalItemCount = linearLayoutManager.getItemCount();
  117. int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
  118. if (progressBar.getVisibility() != View.VISIBLE) {
  119. if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
  120. && firstVisibleItemPosition >= 0
  121. && totalItemCount < TOTAL_RECORD && MY_CONTENTS.size() < TOTAL_RECORD) {
  122. PAGE++;
  123. getBulletinContents(false);
  124. }
  125. }
  126. }
  127. };
  128. private void setupWindowAnimations() {
  129. this.overridePendingTransition(R.anim.animation_enter,
  130. R.anim.animation_leave);
  131. }
  132. public void setupUI(View view) {
  133. try {
  134. if (!(view instanceof EditText)) {
  135. view.setOnTouchListener(new View.OnTouchListener() {
  136. @SuppressLint("ClickableViewAccessibility")
  137. public boolean onTouch(View v, MotionEvent event) {
  138. hideSoftKeyboard(BulletinActivity.this);
  139. return false;
  140. }
  141. });
  142. }
  143. if (view instanceof ViewGroup) {
  144. for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
  145. View innerView = ((ViewGroup) view).getChildAt(i);
  146. setupUI(innerView);
  147. }
  148. }
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. public static void hideSoftKeyboard(Activity activity) {
  154. try {
  155. InputMethodManager inputMethodManager =
  156. (InputMethodManager) activity.getSystemService(
  157. Activity.INPUT_METHOD_SERVICE);
  158. inputMethodManager.hideSoftInputFromWindow(
  159. activity.getCurrentFocus().getWindowToken(), 0);
  160. } catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. @Override
  165. public void onBackPressed() {
  166. super.onBackPressed();
  167. if (isFromAMHome) {
  168. startActivity(new Intent(BulletinActivity.this, AMHomeActivity.class));
  169. finish();
  170. }
  171. }
  172. @Override
  173. protected void onResume() {
  174. super.onResume();
  175. if (adapter != null) {
  176. adapter.notifyDataSetChanged();
  177. }
  178. if (GlobalData.canLogout) {
  179. finish();
  180. }
  181. }
  182. private void showHelpScreen() {
  183. try {
  184. View view;
  185. if (adapter != null && adapter.getItemCount() > 0) {
  186. view = bulletinList.findViewHolderForAdapterPosition(linearLayoutManager.findFirstVisibleItemPosition()).itemView;
  187. } else {
  188. view = bulletinList;
  189. }
  190. new FancyShowCaseView.Builder(this)
  191. .focusOn(view)
  192. .title("Tap here to view the PDF or Video")
  193. .titleSize(22, 1)
  194. .closeOnTouch(true)
  195. .fitSystemWindows(false)
  196. .focusShape(FocusShape.ROUNDED_RECTANGLE)
  197. .backgroundColor(Color.parseColor("#99000000"))
  198. .build()
  199. .show();
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. }
  204. @Override
  205. public boolean onOptionsItemSelected(MenuItem item) {
  206. if (item.getItemId() == android.R.id.home) {
  207. confirmClose();
  208. }
  209. return super.onOptionsItemSelected(item);
  210. }
  211. private void confirmClose() {
  212. if (isFromAMHome) {
  213. startActivity(new Intent(BulletinActivity.this, AMHomeActivity.class));
  214. }
  215. finish();
  216. BulletinActivity.this.overridePendingTransition(R.anim.animation_f_enter,
  217. R.anim.animation_f_leave);
  218. }
  219. private void getBulletinContents(boolean canLoadProgress) {
  220. if (new UtilService().isNetworkAvailable(BulletinActivity.this)) {
  221. if (canLoadProgress) {
  222. progressDialog = new TransparentProgressDialog(BulletinActivity.this);
  223. progressDialog.show();
  224. progressBar.setVisibility(View.GONE);
  225. } else {
  226. progressBar.setVisibility(View.VISIBLE);
  227. }
  228. restService.getBulletinContents(PAGE, LIMIT).enqueue(new Callback<BulletinResponse>() {
  229. @Override
  230. public void onResponse(Call<BulletinResponse> call, Response<BulletinResponse> response) {
  231. if (progressDialog != null && progressDialog.isShowing()) {
  232. progressDialog.dismiss();
  233. }
  234. progressBar.setVisibility(View.GONE);
  235. if (response.body() != null && response.body().getStatus() == 200) {
  236. TOTAL_RECORD = Integer.parseInt(response.body().getTotal());
  237. if (adapter == null) {
  238. MY_CONTENTS = response.body().getData();
  239. adapter = new BulletinAdapter(BulletinActivity.this, MY_CONTENTS);
  240. bulletinList.setAdapter(adapter);
  241. } else {
  242. MY_CONTENTS.addAll(response.body().getData());
  243. }
  244. if (MY_CONTENTS.size() == 0) {
  245. bulletinList.invokeState(EmptyStateRecyclerView.STATE_EMPTY);
  246. } else {
  247. bulletinList.invokeState(EmptyStateRecyclerView.STATE_OK);
  248. }
  249. } else if (response.body() != null && response.body().getStatus() == 401) {
  250. GlobalData.canLogout = true;
  251. finish();
  252. } else if (response.body() != null) {
  253. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  254. } else if (response.errorBody() != null) {
  255. Gson gson = new Gson();
  256. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  257. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  258. if (message.getStatus() == 401) {
  259. GlobalData.canLogout = true;
  260. finish();
  261. }
  262. }
  263. new Handler().postDelayed(new Runnable() {
  264. @Override
  265. public void run() {
  266. if (getPreferences(MODE_PRIVATE).getBoolean("ContentListActivity", true)) {
  267. // showHelpScreen();
  268. getPreferences(MODE_PRIVATE).edit().putBoolean("ContentListActivity", false).apply();
  269. }
  270. }
  271. }, 1000);
  272. }
  273. @Override
  274. public void onFailure(Call<BulletinResponse> call, Throwable t) {
  275. if (progressDialog != null && progressDialog.isShowing()) {
  276. progressDialog.dismiss();
  277. }
  278. progressBar.setVisibility(View.GONE);
  279. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  280. }
  281. });
  282. } else {
  283. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  284. }
  285. }
  286. }