1
\$\begingroup\$

I am building TODO app using MVVM pattern. I have a recycler view that holds all the daily tasks.

My goal was to update the task to be "complete task" and change the row layout when it's completed. I managed to do that successfully, however, I am wondering about the way I did that.

My implementation:

Every time the task list is updated through the adapter, I am running a loop to run all over the tasks on the list and check which one passed based on the current time. I am getting the current time every second, as long as the task list is not empty ,with the help of Handler. In addition,I am using activity listener to check when the user leaves the app,when he leave,the handler stop working. Everything is working as expected.

My questions are:

  • Do you think it's a good practice to do that?
  • How would you do it ?
  • What can I improve in your opinion?

Code:

setTasks -

public void setTasks(List<Task> taskEntries) {

    List<Task> taskListBeforeSort = new ArrayList<>(taskEntries);
    taskListBeforeSort.sort(new Comparator<Task>() {
        @Override
        public int compare(Task task, Task t1) {
            return task.getTaskTime().compareTo(t1.getTaskTime());
        }
    });

    tasksList = taskListBeforeSort;
    ArrayList<Task> listHolder = new ArrayList<>();

    for (int i=0; i<tasksList.size();i++){
        if (!tasksList.get(i).isTaskPassed()){
            listHolder.add(tasksList.get(i));
        }
    }

    if (!listHolder.isEmpty()){
        trackTaskPass(tasksList);
    }else{
        stopHandler();
    }

    notifyDataSetChanged();
}

TrackTaskPass -

private void trackTaskPass(List<Task> tasksList) {
    SimpleDateFormat formatter = new SimpleDateFormat(ConstantsHelper.DateTime.TIME_PATTERN,Locale.getDefault());

    stopHandler();

    someHandler = new Handler(Looper.getMainLooper());
    someHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            String currentDateAndTime = formatter.format(new Date());

            if (!isAllTasksPass(tasksList)){
                for (int i=0; i<tasksList.size();i++){
                    if (!tasksList.get(i).isTaskPassed()){
                        checkIfTaskTimePass(tasksList,currentDateAndTime);
                    }
                }
                Log.e("TAG", "run: " + " Current Time: "+ currentDateAndTime);
                someHandler.postDelayed(this,1000);
            }else{
                stopHandler();
            }
        }
    }, 10);
}

CheckIfTaskTimePassed

public void checkIfTaskTimePass(List<Task> taskList, String currentTime){

    SimpleDateFormat sdf = new SimpleDateFormat(ConstantsHelper.DateTime.TIME_PATTERN,Locale.getDefault());

    for (int i=0; i<taskList.size(); i++){
        String taskTime = taskList.get(i).getTaskTime();
        try {
            Date currentTimeDate = sdf.parse(currentTime);
            Date endTimeDate = sdf.parse(taskTime);
            if (endTimeDate.before(currentTimeDate)){
                taskList.get(i).setTaskPassed(true);
                repository.updateTask(taskList.get(i));
                notifyItemChanged(i);
            }
        } catch (ParseException ignored) {
            Log.e("TAG", "checkIfTaskTimePass: " + ignored.getMessage() );
        }

    }

}

Full RecyclerView Adapter Code:

https://pastebin.com/nkW6yWk4

Thank you ! :)

\$\endgroup\$
3
  • \$\begingroup\$ You tagged Kotlin. Are you wanting a Kotlin solution? It could be done more elegantly with coroutines. \$\endgroup\$
    – Tenfour04
    Commented Aug 29, 2022 at 6:27
  • \$\begingroup\$ I removed kotlin, as the posted code is all Java. \$\endgroup\$
    – janos
    Commented Aug 29, 2022 at 17:04
  • \$\begingroup\$ Yes I accidentally tagged kotlin,I want the review for the java code not kotlin :) \$\endgroup\$
    – user264345
    Commented Aug 29, 2022 at 21:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.