package com.cygnusa.hyperm.adapter; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.os.Handler; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import com.cygnusa.hyperm.AMHomeActivity; import com.cygnusa.hyperm.ASMScheduleViewActivity; import com.cygnusa.hyperm.AcceptTrainingActivity; import com.cygnusa.hyperm.AssessmentActivity; import com.cygnusa.hyperm.AudioViewActivity; import com.cygnusa.hyperm.BulletinDetails; import com.cygnusa.hyperm.ClassRoomDSETainingActivity; import com.cygnusa.hyperm.ClassRoomTrainerTainingActivity; import com.cygnusa.hyperm.HomeActivity; import com.cygnusa.hyperm.MyEarnedPoints; import com.cygnusa.hyperm.NotificationViewActivity; import com.cygnusa.hyperm.PdfViewActivity; import com.cygnusa.hyperm.R; import com.cygnusa.hyperm.SurveyActivity; import com.cygnusa.hyperm.VideoViewActivity; import com.cygnusa.hyperm.screenpinview.AppLockActivity; import com.cygnusa.hyperm.training.activities.ScheduleListActivity; import com.cygnusa.hyperm.util.ApiService; import com.cygnusa.hyperm.util.GlobalData; 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.wrappers.AssessmentList; import com.cygnusa.hyperm.wrappers.AssessmentStatusResponse; import com.cygnusa.hyperm.wrappers.Bulletin; import com.cygnusa.hyperm.wrappers.BulletinStatusResponse; import com.cygnusa.hyperm.wrappers.ContentDetailResponse; import com.cygnusa.hyperm.wrappers.ContentFile; import com.cygnusa.hyperm.wrappers.ErrorResponse; import com.cygnusa.hyperm.wrappers.MyContent; import com.cygnusa.hyperm.wrappers.Notification; import com.cygnusa.hyperm.wrappers.Survey; import com.cygnusa.hyperm.wrappers.SurveyStatusResponse; import com.cygnusa.hyperm.wrappers.ViewScheduleListResponse; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Objects; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class NotificationAdapter extends RecyclerView.Adapter { private ArrayList NOTIFICATION_LIST; private Context context; private PositionUpdateListener listener; private ApiService restService; private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy"); private AssessmentList ASSESSMENT_LIST = new AssessmentList(); private Survey SURVEY_LIST = new Survey(); private MyContent myCon = new MyContent(); private Bulletin myBulletin = new Bulletin(); private TransparentProgressDialog progressDialog; public NotificationAdapter(Context context, ArrayList items, PositionUpdateListener l, ApiService restService) { this.NOTIFICATION_LIST = items; this.context = context; this.listener = l; this.restService = restService; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_item, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Notification item = NOTIFICATION_LIST.get(position); holder.view.setTag(position); // int color1 = ColorGenerator.MATERIAL.getColor(item); // String cha = String.valueOf(item.getNotificationtitle().trim().toUpperCase().charAt(0)); // TextDrawable drawable = TextDrawable.builder() // .beginConfig() // .withBorder(4) // .endConfig() // .buildRound(cha, color1); // holder.imageView.setImageDrawable(drawable); holder.description.setText(item.getNotificationtext()); holder.title.setText(item.getNotificationtitle()); holder.title.setSelected(true); if (item.getStatus().trim().equalsIgnoreCase("0")) { holder.unreadStatus.setVisibility(View.VISIBLE); holder.notificationLayout.setAlpha(1f); } else { holder.unreadStatus.setVisibility(View.GONE); holder.notificationLayout.setAlpha(0.6f); } try { holder.publishedDate.setText(String.format("Published On: %s", format1.format(Objects.requireNonNull(format.parse(item.getCreated_on()))))); } catch (Exception e) { e.printStackTrace(); } holder.view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = (int) v.getTag(); if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("2")) { //getBulletinList(NOTIFICATION_LIST.get(position).getNotificationdata().getAssessmentid()); getAssessmentListByCategory(NOTIFICATION_LIST.get(position).getNotificationdata().getId()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("1")) { getContentCategory(NOTIFICATION_LIST.get(position).getNotificationdata().getId()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("4")) { getBulletinList(NOTIFICATION_LIST.get(position).getNotificationdata().getId()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("3")) { getSurveyListByCategory(NOTIFICATION_LIST.get(position).getNotificationdata().getId()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("6")) { context.startActivity(new Intent(context, MyEarnedPoints.class)); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("7")) { context.startActivity(new Intent(context, AppLockActivity.class)); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("9")) { context.startActivity(new Intent(context, ScheduleListActivity.class)); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("10")) { getScheduledList(NOTIFICATION_LIST.get(position).getNotificationdata().getSchedule_id()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("11")) { getFeedbackList(NOTIFICATION_LIST.get(position).getNotificationtype(), NOTIFICATION_LIST.get(position).getNotificationdata().getSchedule_id()); } else if (NOTIFICATION_LIST.get(position).getNotificationtype().equalsIgnoreCase("12")) { getFeedbackList(NOTIFICATION_LIST.get(position).getNotificationtype(), NOTIFICATION_LIST.get(position).getNotificationdata().getSchedule_id()); } else { NotificationViewActivity.NOTIFICATION = NOTIFICATION_LIST.get(position); context.startActivity(new Intent(context, NotificationViewActivity.class)); } if (NOTIFICATION_LIST.get(position).getStatus().trim().equalsIgnoreCase("0")) { updateNotification(position); } if (listener != null) { listener.onPositionUpdate(position); } } }); } private void getFeedbackList(String type, String scheduleId) { restService.getScheduledList(scheduleId).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { if (response.body().getData() != null) { GlobalData.scheduledDataList = new ArrayList<>(); GlobalData.scheduledDataList = response.body().getData(); if (GlobalData.scheduledDataList != null && GlobalData.scheduledDataList.size() > 0) { if (GlobalData.user.getData().getRole().equalsIgnoreCase("5")) { Intent intent = new Intent(context, ClassRoomTrainerTainingActivity.class); intent.putExtra("type", type); context.startActivity(intent); } else if (GlobalData.user.getData().getRole().equalsIgnoreCase("10")) { Intent i = new Intent(context, ClassRoomDSETainingActivity.class); i.putExtra("type", type); context.startActivity(i); // context.startActivity(new Intent(context, ASMScheduleViewActivity.class)); } } else { GlobalData.showToast(context, "No Data Found.", Toast.LENGTH_SHORT).show(); } } else if (response.body() != null) { GlobalData.showToast(context, response.body().getMessage(), Toast.LENGTH_SHORT).show(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; } else if (response.errorBody() != null) { Gson gson = new Gson(); ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class); GlobalData.showToast(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; } } } @Override public void onFailure(Call call, Throwable t) { Log.e("rr", t.getMessage()); } }); } private void getScheduledList(String scheduleId) { restService.getScheduledList(scheduleId).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { if (response.body().getData() != null) { GlobalData.scheduledDataList = new ArrayList<>(); GlobalData.scheduledDataList = response.body().getData(); if (GlobalData.scheduledDataList != null && GlobalData.scheduledDataList.size() > 0) { if (GlobalData.scheduledDataList.get(0).getParticipant() != null && GlobalData.scheduledDataList.get(0).getParticipant().size() > 0) { if (GlobalData.user.getData().getRole().equalsIgnoreCase("10")) { context.startActivity(new Intent(context, AcceptTrainingActivity.class)); } else if (GlobalData.user.getData().getRole().equalsIgnoreCase("7")) { Intent i = new Intent(context, ASMScheduleViewActivity.class); i.putExtra("SCHEDULE_ID", scheduleId); context.startActivity(i); // context.startActivity(new Intent(context, ASMScheduleViewActivity.class)); } } else { GlobalData.showToast(context, "No Data Found.", Toast.LENGTH_SHORT).show(); } } else { GlobalData.showToast(context, "No Data Found.", Toast.LENGTH_SHORT).show(); } } else if (response.body() != null) { GlobalData.showToast(context, response.body().getMessage(), Toast.LENGTH_SHORT).show(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; } else if (response.errorBody() != null) { Gson gson = new Gson(); ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class); GlobalData.showToast(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; } } } @Override public void onFailure(Call call, Throwable t) { Log.e("rr", t.getMessage()); } }); } private void getBulletinList(String id) { progressDialog = new TransparentProgressDialog(context); progressDialog.show(); if (new UtilService().isNetworkAvailable(context)) { try { restService.getBulletinByPosition(id).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (response.body() != null && response.body().getStatus() == 200) { myBulletin = response.body().getData(); goToBulletinActivity(); if (myCon == null) { myCon = new MyContent(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } else if (response.body() != null) { GlobalData.showToast(context, 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(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } } } @Override public void onFailure(Call call, Throwable t) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } } else { } } private void getContentCategory(String id) { progressDialog = new TransparentProgressDialog(context); progressDialog.show(); if (new UtilService().isNetworkAvailable(context)) { try { restService.getTopicContentsDetails(id).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (response.body() != null && response.body().getStatus() == 200) { myCon = response.body().getData(); goToTopicActivity(); if (myCon == null) { myCon = new MyContent(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } else if (response.body() != null) { GlobalData.showToast(context, 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(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } } } @Override public void onFailure(Call call, Throwable t) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } } else { } } private void getAssessmentListByCategory(String id) { progressDialog = new TransparentProgressDialog(context); progressDialog.show(); if (new UtilService().isNetworkAvailable(context)) { try { restService.getAssessmentByPosition(id).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (response.body() != null && response.body().getStatus() == 200) { ASSESSMENT_LIST = response.body().getData(); goToAssessmentActivity(); if (ASSESSMENT_LIST == null) { ASSESSMENT_LIST = new AssessmentList(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } else if (response.body() != null) { GlobalData.showToast(context, 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(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } } } @Override public void onFailure(Call call, Throwable t) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } } else { } } private void getSurveyListByCategory(String id) { progressDialog = new TransparentProgressDialog(context); progressDialog.show(); if (new UtilService().isNetworkAvailable(context)) { try { restService.getSurveyByPosition(id).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (response.body() != null && response.body().getStatus() == 200) { SURVEY_LIST = response.body().getData(); goToSurveyActivity(); if (SURVEY_LIST == null) { SURVEY_LIST = new Survey(); } } else if (response.body() != null && response.body().getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } else if (response.body() != null) { GlobalData.showToast(context, 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(context, message.getMessage(), Toast.LENGTH_SHORT).show(); if (message.getStatus() == 401) { GlobalData.canLogout = true; new Handler(HomeActivity.callback).sendEmptyMessage(0); } } } @Override public void onFailure(Call call, Throwable t) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } } else { } } private void goToSurveyActivity() { SurveyActivity.ASSESSMENT = SURVEY_LIST; if (listener != null) { // listener.onPositionUpdate(SURVEY_LIST.indexOf(SURVEY_LIST)); } context.startActivity(new Intent(context, SurveyActivity.class)); } private void goToBulletinActivity() { BulletinDetails.CONTENT = myBulletin; if (listener != null) { // listener.onPositionUpdate(SURVEY_LIST.indexOf(SURVEY_LIST)); } context.startActivity(new Intent(context, BulletinDetails.class)); } private void goToTopicActivity() { ContentFile file = getContentFile(myCon); assert file != null; if (file.getFiletypeid().trim().equalsIgnoreCase("1")) { PdfViewActivity.CONTENT = myCon; //PdfViewActivity.TOPIC = myCon.getTitle(); Intent i = new Intent(context, PdfViewActivity.class); i.putExtra("type", "notification"); context.startActivity(i); } else if (file.getFiletypeid().trim().equalsIgnoreCase("2")) { VideoViewActivity.CONTENT = myCon; Intent i = new Intent(context, VideoViewActivity.class); i.putExtra("type", "notification"); context.startActivity(i); } else if (file.getFiletypeid().trim().equalsIgnoreCase("3")) { AudioViewActivity.CONTENT = myCon; // AudioViewActivity.TOPIC = TOPIC; context.startActivity(new Intent(context, AudioViewActivity.class)); } } private ContentFile getContentFile(MyContent item) { if (item.getContent_files() == null) { return null; } String language = context.getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE).getString("DEFAULT", "en_us"); for (ContentFile file : item.getContent_files()) { if (file.getLanguage_code().trim().equalsIgnoreCase(language.trim())) { return file; } } return item.getContent_files().get(0); } private void goToAssessmentActivity() { if (Integer.parseInt(ASSESSMENT_LIST.getAttemptslimit().trim()) > ASSESSMENT_LIST.getAttempt_time()) { if (ASSESSMENT_LIST.getIs_expired().trim().equalsIgnoreCase("0")) { AssessmentActivity.ASSESSMENT = ASSESSMENT_LIST; context.startActivity(new Intent(context, AssessmentActivity.class)); } else { GlobalData.showToast(context, "Assessment expired", Toast.LENGTH_SHORT).show(); } } else { GlobalData.showToast(context, "You have attempted this test " + ASSESSMENT_LIST.getAttempt_time() + " times. Please contact the admin for more details.", Toast.LENGTH_SHORT).show(); } } private void updateNotification(int id) { Cursor cursor = GlobalData.sdb.rawQuery("select * from notification_readed", null); String ids; if (cursor.getCount() > 0) { cursor.moveToFirst(); ids = cursor.getString(cursor.getColumnIndex("notification_readed")); ids = ids + "," + NOTIFICATION_LIST.get(id).getId().trim(); } else { ids = NOTIFICATION_LIST.get(id).getId().trim(); } NOTIFICATION_LIST.get(id).setStatus("1"); notifyDataSetChanged(); GlobalData.sdb.delete("notification_readed", null, null); ContentValues values1 = new ContentValues(); values1.put("notification_readed", ids); GlobalData.sdb.insert("notification_readed", null, values1); GlobalData.sdb.delete("notifications", null, null); Gson gson = new Gson(); JsonElement jsonElement = gson.toJsonTree( NOTIFICATION_LIST, new TypeToken>() { }.getType()); ContentValues values = new ContentValues(); values.put("notifications", jsonElement.toString()); GlobalData.sdb.insert("notifications", null, values); if (new UtilService().isNetworkAvailable(context)) { HashMap map = new HashMap<>(); map.put("notificationid", ids); restService.updateReadNotifications(map) .enqueue(new MyCallback(ids) { @Override public void onResponse(Call call, Response response) { if (response.body() != null && response.body().getStatus() == 200) { GlobalData.sdb.delete("notification_readed", null, null); } try { new Handler(HomeActivity.badgeUpdateCallback).sendEmptyMessage(0); new Handler(AMHomeActivity.badgeUpdateCallback).sendEmptyMessage(0); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(Call call, Throwable t) { GlobalData.showToast(context, "Connection Failure, try again!", Toast.LENGTH_SHORT).show(); } }); } else { try { new Handler(HomeActivity.badgeUpdateCallback).sendEmptyMessage(0); new Handler(AMHomeActivity.badgeUpdateCallback).sendEmptyMessage(0); } catch (Exception e) { e.printStackTrace(); } } } @Override public int getItemCount() { return NOTIFICATION_LIST.size(); } public void add(Notification item, int position) { NOTIFICATION_LIST.add(position, item); notifyItemInserted(position); } public void add(Notification item) { NOTIFICATION_LIST.add(item); notifyItemInserted(NOTIFICATION_LIST.size()); } public void remove(int position) { NOTIFICATION_LIST.remove(position); notifyItemRemoved(position); } class ViewHolder extends RecyclerView.ViewHolder { TextView unreadStatus, publishedDate, title, description; View view; ImageView imageView; LinearLayout notificationLayout; private ViewHolder(View itemView) { super(itemView); view = itemView; unreadStatus = itemView.findViewById(R.id.unreadStatus); publishedDate = itemView.findViewById(R.id.publishedDate); title = itemView.findViewById(R.id.title); description = itemView.findViewById(R.id.description); imageView = itemView.findViewById(R.id.imageView); notificationLayout = itemView.findViewById(R.id.mainView); } } }