Last active
October 16, 2016 18:54
-
-
Save ahmedengu/8dacaee640b8b3ae419965880d346737 to your computer and use it in GitHub Desktop.
Countdown Label for codenameone , clock demo: https://www.codenameone.com/blog/clock-demo.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.g_ara.gara.controller; | |
import com.codename1.ui.Image; | |
import com.codename1.ui.Label; | |
/** | |
* Created by ahmedengu. | |
*/ | |
public class Countdown extends Label { | |
long lastRenderedTime = 0L; | |
int remaining; | |
Callback callback; | |
public Countdown(int duration) { | |
super(((duration > 0) ? duration / 60 : 0) + ":" + duration % 60); | |
remaining = duration; | |
} | |
public Countdown(int duration, String UUID) { | |
super(((duration > 0) ? duration / 60 : 0) + ":" + duration % 60, UUID); | |
remaining = duration; | |
} | |
public Countdown(int duration, Image icon, String UUID) { | |
super(((duration > 0) ? duration / 60 : 0) + ":" + duration % 60, icon, UUID); | |
remaining = duration; | |
} | |
public Countdown(int duration, Callback callback) { | |
this(duration); | |
this.callback = callback; | |
} | |
public Countdown(int duration, String UUID, Callback callback) { | |
this(duration, UUID); | |
this.callback = callback; | |
} | |
public Countdown(int duration, Image icon, String UUID, Callback callback) { | |
this(duration, icon, UUID); | |
this.callback = callback; | |
} | |
@Override | |
public boolean animate() { | |
if (System.currentTimeMillis() / 1000 != lastRenderedTime / 1000 && remaining != -2) { | |
aSecond(); | |
return true; | |
} | |
return false; | |
} | |
private void aSecond() { | |
if (remaining >= 0) { | |
setText(((remaining > 0) ? remaining / 60 : 0) + ":" + remaining % 60); | |
remaining--; | |
lastRenderedTime = System.currentTimeMillis(); | |
} else if (remaining == -1) { | |
remaining = -2; | |
this.getComponentForm().deregisterAnimated(this); | |
if (callback != null) | |
callback.done(); | |
} | |
} | |
@Override | |
protected void initComponent() { | |
super.initComponent(); | |
this.getComponentForm().registerAnimated(this); | |
} | |
@Override | |
protected void deinitialize() { | |
super.deinitialize(); | |
this.getComponentForm().deregisterAnimated(this); | |
} | |
public static abstract class Callback<T> { | |
public abstract void done(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StateMachine extends StateMachineBase { | |
@Override | |
protected void beforeCountdown(Form f) { | |
f.add(new Countdown(90, new Countdown.Callback() { | |
@Override | |
public void done() { | |
// callback | |
} | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment