Skip to content

Commit 76113b6

Browse files
committed
added a listener
1 parent c2cb1c2 commit 76113b6

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

.idea/codeStyles/Project.xml

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

androidcountdown/src/main/java/com/tsuryo/androidcountdown/Counter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Counter extends ConstraintLayout {
3333
private String mDate;
3434
private boolean mIsShowingTextDesc;
3535
private Typeface mTypeFace;
36+
private Listener mListener;
3637

3738
public Counter(Context context, AttributeSet attrs) {
3839
super(context, attrs);
@@ -148,6 +149,10 @@ public void setTypeFace(Typeface typeFace) {
148149
refresh();
149150
}
150151

152+
public void setListener(Listener listener) {
153+
mListener = listener;
154+
}
155+
151156
public Integer getTextColor() {
152157
return mTextColor;
153158
}
@@ -232,6 +237,7 @@ public void onTick(long millisUntilFinished) {
232237
MILLISECONDS.toMinutes(millisUntilFinished)) :
233238
MILLISECONDS.toSeconds(millisUntilFinished);
234239
setText(days, hours, minutes, seconds);
240+
notifyListener(millisUntilFinished, days, hours, minutes, seconds);
235241
}
236242

237243
public void onFinish() {
@@ -262,8 +268,22 @@ private void setText(long days, long hours, long minutes, long seconds) {
262268
}
263269
}
264270

271+
private void notifyListener(long millisUntilFinished, long days,
272+
long hours, long minutes, long seconds) {
273+
if (mListener != null) {
274+
mListener.onTick(millisUntilFinished);
275+
mListener.onTick(days, hours, minutes, seconds);
276+
}
277+
}
278+
265279
private void refresh() {
266280
invalidate();
267281
requestLayout();
268282
}
283+
284+
public interface Listener {
285+
void onTick(long millisUntilFinished);
286+
287+
void onTick(long days, long hours, long minutes, long seconds);
288+
}
269289
}

app/src/main/java/com/tsuryo/countdowntimer/MainActivity.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,27 @@ protected void onStart() {
3636
SimpleDateFormat format = new SimpleDateFormat(
3737
"yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
3838
try {
39-
Date date = format.parse("2019-07-22T18:33:00");
39+
Date date = format.parse("2020-06-10T18:33:00");
4040
mCounter.setDate(date);
4141
} catch (ParseException e) {
4242
e.printStackTrace();
4343
}
44-
mCounter1.setDate("2019-07-22T18:33:00");
44+
mCounter1.setDate("2020-06-10T18:33:00");
4545

46+
mCounter.setListener(new Counter.Listener() {
47+
@Override
48+
public void onTick(long millisUntilFinished) {
49+
Log.d(TAG, "onTick: Counter - " + millisUntilFinished);
50+
}
51+
52+
@Override
53+
public void onTick(long days, long hours, long minutes, long seconds) {
54+
Log.d(TAG, "onTick: Counter - " + days + "d " +
55+
hours + "h " +
56+
minutes + "m " +
57+
seconds + "s " );
58+
}
59+
});
4660

4761
// mCounter.setIsShowingTextDesc(true);
4862
// mCounter.setTextColor(R.color.colorPrimary);

0 commit comments

Comments
 (0)