Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CountDownTimer : In Activity, ViewModel or separate class?

I would like to create a CountdownTimer which will trigger events that will update the UI (trigger popup, start an animation, etc.).

I wonder how to do this clean, here are my hypothesis and why :

  1. A separate component EventCountdownTimer. I could then benefit the use of LifecycleObserver, but I wonder how to communicate the information back to the activity (I tried extending CountdownTimer and using it in the activity but I have an error and can't get it to compile)
  2. In the Activity itself, it's the simplest but I'm not sure it belongs there as it isn't a UI component and I can't benefit the LifecycleObserver
  3. In the ViewModel. I thought as it's activity related and the CountdownTimer is kinda logic data, it should go in here, but that means also watching the lifecycle of the activity, and holding any Activity related field within ViewModel is bad practice.

What's the best option according to you? And why?

like image 204
Sebastien FERRAND Avatar asked Jun 24 '19 09:06

Sebastien FERRAND


People also ask

Do we have references of activity in ViewModel?

Caution: A ViewModel must never reference a view, Lifecycle , or any class that may hold a reference to the activity context. ViewModel objects are designed to outlive specific instantiations of views or LifecycleOwners .

How to use CountDownTimer in Android?

onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish(). start() - It is used to call countdown timer. cancel() - It is used to cancel countdown timer. This example demonstrates how to integrate countdown timer with textview.

What is CountDownTimer?

android.os.CountDownTimer. Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field: Kotlin Java.

What is a countdowntimer?

A CountDownTimer is an accurate timer that can be used for a website or blog to display the count down to any special event, such as a birthday or anniversary. Likewise, here let’s create an Android App to learn how to create a simple Countdown App.

How to use countdowntimer onfinish () and ontick () methods in Android?

The CountDownTimer class provides onFinish () and onTick () methods. Step 1 Create a project like this: Select "File" -> "New Project" -> "Android Application". Step 2 Create an XML file and write this:

How to use pulsecountdown instead of countdowntimer in Android?

Use Up/Down Arrow keys to increase or decrease volume. PulseCountDown in Android is an alternative to CountDownTimer. It is very easy to implement PulseCountDown instead of CountDownTimer because PulseCountDown provides a default layout with some beautiful animations.

How do I show a 50 second countdown in a textview?

Example of showing a 50-second countdown in a text field: textView.setText (f.format (hour) + “:” + f.format (min) + “:” + f.format (sec));


2 Answers

In a MVVM pattern you could have a LiveData observable in your ViewModel which will be observed by the UI and upon value change you update the UI accordingly. How that observable changes value, that is your business logic and all of it should be in your ViewModel or in separate components that will be used by the ViewModel to update the observable state.

This will allow you to separate the UI from the business logic being your observable the bridge of communication between both, without the ViewModel having any knowledge of whats going on in the UI. In simple words it only executes what it is told to execute and updates a variable that is being observed, what then happens in the UI is the UI responsibility and with this you have reached a clear separation of concerns.

like image 72
Ricardo Avatar answered Oct 16 '22 11:10

Ricardo


A separate component "EventCountdownTimer"

In my opinion, this is the best implementation that you might have in your case. For communicating information back to your activity, you might consider having an interface like the following.

public interface TimerListener {
    void onTimerResponse(String response);
}

Modify your EventCountdownTimer to have a constructor which takes TimerListener as a parameter and override the onTimerResponse method in your activity. Now from your EventCountdownTimer, when you are trying to communicate with your activity along with a message, for example, you might just call the function onTimerResponse(msgToDeliver).

Hence your EventCountdownTimer should look something like this.

public class EventCountdownTimer {
    public static Context context;
    public static TimerListener listener;

    public EventCountdownTimer(Context context, TimerListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public startCountdown() {
        // Start the count down here
        // ... Other code

        // When its time to post some update to your activity
        listener.onTimerResponse(msgToDeliver);
    }
}

And from your activity, initialize the EventCountdownTimer like the following.

EventCountdownTimer timer = new EventCountdownTimer(this, new TimerListener() {
    @Override
    public void onTimerResponse(String message) {
        // Do something with the message data 
        // Update your UI maybe
    }
});

I think you have provided good reasons already for not going for other options that you have mentioned.

like image 38
Reaz Murshed Avatar answered Oct 16 '22 10:10

Reaz Murshed