lorenzo1723
lorenzo1723

Reputation: 69

Count Down Timer for a timer

I am trying to make a Pomodoro Timer (25 minutes work, 5 minutes break then repeat n times). But I can't find a way to let the break timer start automatically and then start again with the work timer (work timer = 00:10 - break timer = 00:03, i put these values so i can test faster). How can I fix this?

public class MainActivity extends AppCompatActivity {

TextView tvTimer, tvSessions;
ImageView ivStart, ivStop;
SeekBar seekBar;

CountDownTimer countDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvTimer = findViewById(R.id.tvTimer);
    tvSessions = findViewById(R.id.tvSessions);
    ivStart = findViewById(R.id.ivStart);
    ivStop = findViewById(R.id.ivStop);
    seekBar = findViewById(R.id.seekBar);

    seekBar.setMax(2500);
    seekBar.setProgress(10);
    seekBar.setVisibility(View.GONE);

    tvTimer.setText("00:10");

}

// ON CLICK METHODS

public void startTimer(View v){
    countDownTimer = new CountDownTimer(seekBar.getProgress() * 1000 + 100, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            breakTimer();
        }
    };

    countDownTimer.start();
}

public void stopTimer(View v){

}

// UPDATE METHODS

private void breakTimer(){
    seekBar.setProgress(3);
    tvTimer.setText("00:03");
}

private void updateTimer(int progress){
    int minutes = progress / 60;
    int seconds = progress - minutes * 60;

    String textMinutes = String.valueOf(minutes);
    String textSeconds = String.valueOf(seconds);

    if(seconds < 10) textSeconds = "0" + textSeconds;
    if(minutes < 10) textMinutes = "0" + textMinutes;

    tvTimer.setText(textMinutes + ":" + textSeconds);
}
}

Expected output:

00:10 -> 00:00 -> 00:03 -> 00:00 -> 00:10 and so on...

Upvotes: 2

Views: 91

Answers (1)

Blundell
Blundell

Reputation: 76564

What about something like this. Using a flag to keep track of if the Timer is a rest timer or a pomodoro timer.

When the timer finishes, you check the flag to decide which timer to start next (and flip the flag so next time it does the opposite).

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private static final int TICK_EVERY_SECOND = 1000;

    private TextView tvTimer, tvSessions;
    private ImageView ivStart, ivStop;
    private SeekBar seekBar;

    private CountDownTimer countDownTimer;

    private boolean isRest = false;
    private int userSelectedDurationSeconds = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvTimer = findViewById(R.id.tvTimer);
        tvSessions = findViewById(R.id.tvSessions);
        ivStart = findViewById(R.id.ivStart);
        ivStop = findViewById(R.id.ivStop);
        seekBar = findViewById(R.id.seekBar);

        seekBar.setMax(2500);
        seekBar.setProgress(10);
        seekBar.setVisibility(View.GONE);

        tvTimer.setText("00:10");

    }

    // ON CLICK METHODS

    public void startTimer(View v) {
        isRest = false;
        int durationSeconds = seekBar.getProgress();
        userSelectedDurationSeconds = durationSeconds;
        restartTimer(durationSeconds);
    }

    private void restartTimer(int durationSeconds) {
        countDownTimer = new CountDownTimer(TimeUnit.SECONDS.toMillis(durationSeconds), TICK_EVERY_SECOND) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                breakTimer();
            }
        };

        countDownTimer.start();
    }

    public void stopTimer(View v) {

    }

    // UPDATE METHODS

    private void breakTimer() {
        isRest = !isRest;
        if(isRest) {
            seekBar.setProgress(3);
            tvTimer.setText("00:03");
            restartTimer(3);
        } else {
            restartTimer(userSelectedDurationSeconds);
        }
    }

    private void updateTimer(int progressInSeconds) {
        int minutes = progressInSeconds / 60;
        int seconds = progressInSeconds - minutes * 60;

        String textMinutes = String.valueOf(minutes);
        String textSeconds = String.valueOf(seconds);

        if (seconds < 10) textSeconds = "0" + textSeconds;
        if (minutes < 10) textMinutes = "0" + textMinutes;

        tvTimer.setText(textMinutes + ":" + textSeconds);
    }
}

Upvotes: 3

Related Questions