Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Added cancel button
Browse files Browse the repository at this point in the history
Signed-off-by: micrusa <[email protected]>
  • Loading branch information
micrusa committed Apr 12, 2020
1 parent 177b3f4 commit 218ec6a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 41 deletions.
105 changes: 70 additions & 35 deletions app/src/main/java/me/micrusa/amaztimer/widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import clc.sliteplugin.flowboard.AbstractPlugin;
import clc.sliteplugin.flowboard.ISpringBoardHostStub;
Expand All @@ -29,10 +31,15 @@ public class widget extends AbstractPlugin {
private boolean mHasActive = false;
private ISpringBoardHostStub mHost = null;
private int v;
//Setup objects
private Button plus, plus2, plus3, minus, minus2, minus3, start;
//Define items
private Button plus, plus2, plus3, minus, minus2, minus3, start, cancel;
private TextView sets, rest, work, time, hr, rSets, status;
private ConstraintLayout L1, L2;
//Define timers and timer booleans
private CountDownTimer workTimer;
private CountDownTimer restTimer;
private boolean workStarted = false;
private boolean restStarted = false;
//Classes
private utils utils = new utils();
//Default values
Expand Down Expand Up @@ -144,15 +151,7 @@ public void onClick(View view) {
final CountDownTimer PrepareTimer = new CountDownTimer(5 * 1000, 1000) {
@Override
public void onTick(long l) {
v = (int) l / 1000;
time.setText(utils.sToMinS(v));
if(v<4){
if(v==1){
utils.vibrate(defValues.lVibration, gView.getContext());}
if(v!=1){
utils.vibrate(defValues.sVibration, gView.getContext());}
}

timerUpdate((int) l / 1000);
}

@Override
Expand All @@ -163,6 +162,27 @@ public void onFinish() {

}
});

//Cancel button
//To avoid accidental clicks, just a long click will cancel it
cancel.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//Display start layout
L1.setVisibility(View.VISIBLE);
L2.setVisibility(View.GONE);
//Stop timers
stopTimers();
return true;
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Send toast
Toast.makeText(gView.getContext(), gView.getResources().getString(R.string.canceltoast), Toast.LENGTH_SHORT);
}
});
return this.mView;
}

Expand All @@ -175,6 +195,7 @@ private void init(){
minus2 = this.mView.findViewById(R.id.minus);
minus3 = this.mView.findViewById(R.id.minus3);
start = this.mView.findViewById(R.id.start);
cancel = this.mView.findViewById(R.id.cancel);
//TextViews
sets = this.mView.findViewById(R.id.sets);
rest = this.mView.findViewById(R.id.rest);
Expand All @@ -188,52 +209,66 @@ private void init(){
L2 = this.mView.findViewById(R.id.timerScreen);
}

private void timerUpdate(int v){
this.init();
time.setText(utils.sToMinS(v));
if(v<4){
if(v==1){
utils.vibrate(defValues.lVibration, this.mView.getContext());}
if(v!=1){
utils.vibrate(defValues.sVibration, this.mView.getContext());}
}
}

private void stopTimers(){
if(this.workStarted){
this.workTimer.cancel();
}
if(this.restStarted){
this.restTimer.cancel();
}
}

private void startTimer(final View c, final String sWork, final String sRest, final int work, final int rest, final hrSensor hrSensor){
this.init();
status.setText(sWork);
L2.setBackgroundColor(c.getResources().getColor(R.color.red));
this.workStarted = true;
this.restStarted = false;
if(!this.mHasActive){
this.workStarted = false;
this.restStarted = false;
return;
}
CountDownTimer Timer = new CountDownTimer(work * 1000, 1000) {
status.setText(sWork);
L2.setBackgroundColor(c.getResources().getColor(R.color.red));
this.workTimer = new CountDownTimer(work * 1000, 1000) {
@Override
public void onTick(long l) {
v = (int) l / 1000;
time.setText(utils.sToMinS(v));
if(v<4){
if(v==1){
utils.vibrate(defValues.lVibration, c.getContext());}
if(v!=1){
utils.vibrate(defValues.sVibration, c.getContext());}
}
timerUpdate((int) l / 1000);
}

@Override
public void onFinish() {
restTimer(c, sWork, sRest, work, rest, hrSensor);
}
};
Timer.start();
this.workTimer.start();
}

private void restTimer(final View c, final String sWork, final String sRest, final int work, final int rest, final hrSensor hrSensor){
this.init();
status.setText(sRest);
L2.setBackgroundColor(c.getResources().getColor(R.color.green));
this.workStarted = false;
this.restStarted = true;
if(!this.mHasActive){
this.workStarted = false;
this.restStarted = false;
return;
}
CountDownTimer Timer = new CountDownTimer(rest * 1000, 1000) {
status.setText(sRest);
L2.setBackgroundColor(c.getResources().getColor(R.color.green));
this.restTimer = new CountDownTimer(rest * 1000, 1000) {
@Override
public void onTick(long l) {
v = (int) l / 1000;
time.setText(utils.sToMinS(v));
if (v < 4) {
if (v != 1) {
utils.vibrate(defValues.sVibration, c.getContext());
} else{
utils.vibrate(defValues.lVibration, c.getContext());}
}
timerUpdate((int) l / 1000);
}

@Override
Expand All @@ -249,7 +284,7 @@ public void onFinish() {
}
}
};
Timer.start();
this.restTimer.start();
}

//Return the icon for this page, used when the page is disabled in the app list. In this case, the launcher icon is used
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/res/layout/amaztimer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:id="@+id/startScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -160,7 +160,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:visibility="visible"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -170,7 +170,7 @@
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginBottom="4dp"
android:text="--:--"
android:textColor="@color/black"
android:textSize="36sp"
Expand All @@ -182,11 +182,12 @@
android:id="@+id/heartbeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:drawableStart="@drawable/heartbeat"
android:text="--"
android:textColor="@color/black"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/remSets"
app:layout_constraintBottom_toTopOf="@+id/time"
app:layout_constraintEnd_toEndOf="@+id/time"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="@+id/time" />
Expand All @@ -207,14 +208,25 @@
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:text="--------------"
android:textColor="@color/black"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/cancel"
app:layout_constraintEnd_toEndOf="@+id/time"
app:layout_constraintHorizontal_bias="0.523"
app:layout_constraintStart_toStartOf="@+id/time" />

<Button
android:id="@+id/cancel"
android:layout_width="67dp"
android:layout_height="36dp"
android:layout_marginBottom="4dp"
android:background="@drawable/circle_button"
android:text="@android:string/cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/time"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toStartOf="@+id/time" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
<string name="work">Trabajo</string>
<string name="rest">Descanso</string>
<string name="prepare">Preparate!</string>
<string name="cancel">Cancelar</string>
<string name="canceltoast">¡Mantén pulsado!</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
<string name="work">Trabajo</string>
<string name="rest">Descanso</string>
<string name="prepare">Preparate!</string>
<string name="cancel">Cancelar</string>
<string name="canceltoast">¡Mantén pulsado!</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
<string name="rest">Rest</string>
<string name="sets" translatable="false">Sets</string>
<string name="prepare">Get prepared!</string>
<string name="cancel">Cancel</string>
<string name="canceltoast">Long click!</string>
</resources>

0 comments on commit 218ec6a

Please sign in to comment.