| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- package com.cygnusa.hyperm;
- import android.annotation.SuppressLint;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.RectF;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.recyclerview.widget.RecyclerView;
- import androidx.appcompat.widget.Toolbar;
- import androidx.recyclerview.widget.ItemTouchHelper;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ProgressBar;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import com.google.gson.JsonElement;
- import com.google.gson.reflect.TypeToken;
- import com.cygnusa.hyperm.adapter.NotificationAdapter;
- import com.cygnusa.hyperm.fancyshowcase.DismissListener;
- import com.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
- import com.cygnusa.hyperm.fancyshowcase.FocusShape;
- import com.cygnusa.hyperm.util.ApiService;
- import com.cygnusa.hyperm.util.GlobalData;
- import com.cygnusa.hyperm.util.MainApplication;
- import com.cygnusa.hyperm.util.MyCallback;
- import com.cygnusa.hyperm.util.PositionUpdateListener;
- import com.cygnusa.hyperm.util.TransparentProgressDialog;
- import com.cygnusa.hyperm.util.UtilService;
- import com.cygnusa.hyperm.util.WrapContentLinearLayoutManager;
- import com.cygnusa.hyperm.wrappers.AssessmentList;
- import com.cygnusa.hyperm.wrappers.ErrorResponse;
- import com.cygnusa.hyperm.wrappers.Notification;
- import com.cygnusa.hyperm.wrappers.NotificationResponse;
- import com.tylersuehr.esr.EmptyStateRecyclerView;
- import com.tylersuehr.esr.TextStateDisplay;
- import java.util.ArrayList;
- import java.util.HashMap;
- import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class NotificationActivity extends AppCompatActivity implements PositionUpdateListener, DismissListener {
- ApiService restService;
- EmptyStateRecyclerView notificationList;
- TransparentProgressDialog progressDialog;
- int PAGE = 0;
- int LIMIT = 10;
- WrapContentLinearLayoutManager linearLayoutManager;
- int TOTAL_RECORD = 0;
- ProgressBar progressBar;
- ArrayList<Notification> MY_NOTIFICATIONS = new ArrayList<>();
- NotificationAdapter adapter;
- private Paint p = new Paint();
- public static Handler.Callback notificationCallback;
- ArrayList<AssessmentList> ASSESSMENT_LIST = new ArrayList<>();
- ArrayList<AssessmentList> OFFLINE_ASSESSMENT_LIST = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_notification);
- setupWindowAnimations();
- restService = ((MainApplication) getApplication()).getClient();
- GlobalData.openDatabase(NotificationActivity.this);
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- toolbar.setContentInsetStartWithNavigation(0);
- toolbar.setTitle("");
- getSupportActionBar().setTitle("");
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- @SuppressLint("PrivateResource")
- Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
- backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
- getSupportActionBar().setHomeAsUpIndicator(backArrow);
- notificationList = findViewById(R.id.notificationList);
- notificationList.setHasFixedSize(true);
- notificationList.setItemAnimator(new SlideInLeftAnimator());
- linearLayoutManager = new WrapContentLinearLayoutManager(getBaseContext());
- notificationList.setLayoutManager(linearLayoutManager);
- notificationList.addOnScrollListener(recyclerViewOnScrollListener);
- progressBar = findViewById(R.id.progressBar);
- notificationList.setStateDisplay(EmptyStateRecyclerView.STATE_EMPTY,
- new TextStateDisplay(this, "No data available", ""));
- notificationList.post(new Runnable() {
- @Override
- public void run() {
- getNotifications(true);
- }
- });
- initSwipe();
- findViewById(R.id.helpIcon).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showHelpScreen();
- }
- });
- notificationCallback = new Handler.Callback() {
- @Override
- public boolean handleMessage(Message msg) {
- PAGE = 0;
- getNotifications(false);
- return false;
- }
- };
- }
- private void showHelpScreen() {
- try {
- View view;
- // if (adapter != null && adapter.getItemCount() > 0) {
- // view = notificationList.findViewHolderForAdapterPosition(linearLayoutManager.findFirstVisibleItemPosition()).itemView;
- // } else {
- // view = notificationList;
- // }
- new FancyShowCaseView.Builder(this)
- .focusOn(notificationList)
- .title("Tap here to view notification")
- .titleSize(22, 1)
- .titleGravity(1)
- .closeOnTouch(true)
- .fitSystemWindows(false)
- .dismissListener(this)
- .focusShape(FocusShape.ROUNDED_RECTANGLE)
- .backgroundColor(Color.parseColor("#99000000"))
- .build()
- .show();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onDismiss(String id) {
- try {
- View view;
- if (adapter != null && adapter.getItemCount() > 0) {
- view = notificationList.findViewHolderForAdapterPosition(linearLayoutManager.findFirstVisibleItemPosition()).itemView;
- } else {
- view = notificationList;
- }
- new FancyShowCaseView.Builder(this)
- .focusOn(view)
- .title("Swipe right to left delete notification")
- .titleSize(22, 1)
- .closeOnTouch(true)
- .fitSystemWindows(false)
- .focusShape(FocusShape.ROUNDED_RECTANGLE)
- .backgroundColor(Color.parseColor("#99000000"))
- .build()
- .show();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onSkipped(String id) {
- }
- private void initSwipe() {
- ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
- @Override
- public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
- return false;
- }
- @Override
- public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
- int position = viewHolder.getAdapterPosition();
- if (direction == ItemTouchHelper.LEFT) {
- try {
- notificationList.setEnabled(false);
- deleteNotification(position);
- notificationList.setEnabled(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
- Bitmap icon;
- if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
- View itemView = viewHolder.itemView;
- float height = (float) itemView.getBottom() - (float) itemView.getTop();
- float width = height / 3;
- if (dX < 0) {
- p.setColor(Color.RED);
- RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),
- (float) itemView.getRight(), (float) itemView.getBottom());
- c.drawRect(background, p);
- icon = BitmapFactory.decodeResource(getResources(), R.mipmap.delete_icon);
- RectF icon_dest = new RectF((float) itemView.getRight() - 2 * width,
- (float) itemView.getTop() + width, (float) itemView.getRight() - width,
- (float) itemView.getBottom() - width);
- c.drawBitmap(icon, null, icon_dest, p);
- }
- }
- super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
- }
- };
- ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
- itemTouchHelper.attachToRecyclerView(notificationList);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == android.R.id.home) {
- finish();
- this.overridePendingTransition(R.anim.animation_f_enter,
- R.anim.animation_f_leave);
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- this.overridePendingTransition(R.anim.animation_f_enter,
- R.anim.animation_f_leave);
- }
- private void setupWindowAnimations() {
- this.overridePendingTransition(R.anim.animation_enter,
- R.anim.animation_leave);
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (GlobalData.canLogout) {
- finish();
- }
- }
- private void getNotifications(boolean canLoadProgress) {
- if (new UtilService().isNetworkAvailable(NotificationActivity.this)) {
- if (canLoadProgress) {
- progressDialog = new TransparentProgressDialog(NotificationActivity.this);
- progressDialog.show();
- progressBar.setVisibility(View.GONE);
- } else {
- progressBar.setVisibility(View.VISIBLE);
- }
- restService.getNotifications(PAGE, LIMIT).enqueue(new Callback<NotificationResponse>() {
- @Override
- public void onResponse(Call<NotificationResponse> call, Response<NotificationResponse> response) {
- if (progressDialog != null && progressDialog.isShowing()) {
- progressDialog.dismiss();
- }
- progressBar.setVisibility(View.GONE);
- if (response.body() != null && response.body().getStatus() == 200) {
- TOTAL_RECORD = Integer.parseInt(response.body().getTotal());
- if (adapter == null) {
- MY_NOTIFICATIONS = response.body().getData();
- adapter = new NotificationAdapter(NotificationActivity.this, MY_NOTIFICATIONS,
- NotificationActivity.this, restService);
- notificationList.setAdapter(adapter);
- } else {
- MY_NOTIFICATIONS.addAll(response.body().getData());
- adapter.notifyDataSetChanged();
- }
- if (MY_NOTIFICATIONS.size() == 0) {
- notificationList.invokeState(EmptyStateRecyclerView.STATE_EMPTY);
- } else {
- notificationList.invokeState(EmptyStateRecyclerView.STATE_OK);
- }
- GlobalData.sdb.delete("notifications", null, null);
- Gson gson = new Gson();
- JsonElement jsonElement = gson.toJsonTree(
- MY_NOTIFICATIONS, new TypeToken<ArrayList<Notification>>() {
- }.getType());
- ContentValues values = new ContentValues();
- values.put("notifications", jsonElement.toString());
- GlobalData.sdb.insert("notifications", null, values);
- } else if (response.body() != null && response.body().getStatus() == 401) {
- GlobalData.canLogout = true;
- finish();
- } else if (response.body() != null) {
- GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
- } else if (response.errorBody() != null) {
- Gson gson = new Gson();
- ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
- GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
- if (message.getStatus() == 401) {
- GlobalData.canLogout = true;
- finish();
- }
- }
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- if (getPreferences(MODE_PRIVATE).getBoolean("NotificationActivity", true)) {
- showHelpScreen();
- getPreferences(MODE_PRIVATE).edit().putBoolean("NotificationActivity", false).apply();
- }
- }
- }, 1000);
- }
- @Override
- public void onFailure(Call<NotificationResponse> call, Throwable t) {
- if (progressDialog != null && progressDialog.isShowing()) {
- progressDialog.dismiss();
- }
- progressBar.setVisibility(View.GONE);
- GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
- }
- });
- } else {
- if (adapter == null) {
- Cursor cursor = GlobalData.sdb.rawQuery("select * from notifications", null);
- if (cursor.getCount() > 0) {
- cursor.moveToFirst();
- Gson gson = new Gson();
- MY_NOTIFICATIONS = gson.fromJson(
- cursor.getString(cursor.getColumnIndex("notifications")),
- new TypeToken<ArrayList<Notification>>() {
- }.getType());
- TOTAL_RECORD = MY_NOTIFICATIONS.size();
- adapter = new NotificationAdapter(NotificationActivity.this, MY_NOTIFICATIONS,
- NotificationActivity.this, restService);
- notificationList.setAdapter(adapter);
- } else {
- GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
- }
- } else {
- GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- int visibleItemCount = linearLayoutManager.getChildCount();
- int totalItemCount = linearLayoutManager.getItemCount();
- int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
- if (progressBar.getVisibility() != View.VISIBLE) {
- if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
- && firstVisibleItemPosition >= 0
- && totalItemCount < TOTAL_RECORD && MY_NOTIFICATIONS.size() < TOTAL_RECORD) {
- PAGE++;
- getNotifications(false);
- }
- }
- }
- };
- @Override
- public void onPositionUpdate(int position) {
- }
- private void deleteNotification(int id) {
- Cursor cursor = GlobalData.sdb.rawQuery("select * from notification_deleted", null);
- String ids;
- if (cursor.getCount() > 0) {
- cursor.moveToFirst();
- ids = cursor.getString(cursor.getColumnIndex("notification_deleted"));
- ids = ids + "," + MY_NOTIFICATIONS.get(id).getId().trim();
- } else {
- ids = MY_NOTIFICATIONS.get(id).getId().trim();
- }
- adapter.remove(id);
- if (MY_NOTIFICATIONS.size() == 0) {
- notificationList.invokeState(EmptyStateRecyclerView.STATE_EMPTY);
- }
- GlobalData.sdb.delete("notification_deleted", null, null);
- ContentValues values1 = new ContentValues();
- values1.put("notification_deleted", ids);
- GlobalData.sdb.insert("notification_deleted", null, values1);
- GlobalData.sdb.delete("notifications", null, null);
- Gson gson = new Gson();
- JsonElement jsonElement = gson.toJsonTree(
- MY_NOTIFICATIONS, new TypeToken<ArrayList<Notification>>() {
- }.getType());
- ContentValues values = new ContentValues();
- values.put("notifications", jsonElement.toString());
- GlobalData.sdb.insert("notifications", null, values);
- if (new UtilService().isNetworkAvailable(NotificationActivity.this)) {
- HashMap<String, String> map = new HashMap<>();
- map.put("notificationid", ids);
- restService.updateDeleteNotifications(map)
- .enqueue(new MyCallback<ErrorResponse>(ids) {
- @Override
- public void onResponse(Call<ErrorResponse> call, Response<ErrorResponse> response) {
- if (response.body() != null && response.body().getStatus() == 200) {
- GlobalData.sdb.delete("notification_deleted", null, null);
- }
- try {
- new android.os.Handler(HomeActivity.badgeUpdateCallback).sendEmptyMessage(0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(Call<ErrorResponse> call, Throwable t) {
- GlobalData.showToast(NotificationActivity.this, "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
- }
- });
- } else {
- try {
- new android.os.Handler(HomeActivity.badgeUpdateCallback).sendEmptyMessage(0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|