Skip to content

Commit a06c7fb

Browse files
committed
update: countdown timer
1 parent 111f8a9 commit a06c7fb

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

app/src/main/java/com/qomunal/opensource/androidresearch/common/ext/DateExt.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.util.TimeZone
1212

1313

1414
const val ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
15+
const val DEFAULT_FORMAT = "dd LLLL yyyy"
1516
const val DATE_TIME_FORMAT = "dd-MM-yyyy HH:mm:ss"
1617

1718
const val DATE_TIME_FORMAT_AM_PM = "dd MMMM yyyy, H.mm a"
@@ -22,6 +23,10 @@ val TIME_ZONE_ID_EXT = TimeZone.getDefault().id
2223

2324
// -------------------------------------------------------------------------------------------------
2425

26+
fun getDateTodayExt(format: String = DATE_TIME_FORMAT): String {
27+
return SimpleDateFormat(format, getLocaleExt()).format(Date())
28+
}
29+
2530
fun String.dateFormater(
2631
from: String = SIMPLE_DATE_FORMAT,
2732
to: String = DATE_TIME_FORMAT,
@@ -87,7 +92,9 @@ fun String.fromIsoDateUTC(format: String = DATE_TIME_FORMAT): String {
8792
fun String.getMillisExt(format: String = SIMPLE_DATE_FORMAT): Long {
8893
var date = Date()
8994
try {
90-
date = SimpleDateFormat(format, getLocaleExt()).parse(this) ?: Date()
95+
date = SimpleDateFormat(format, getLocaleExt()).apply {
96+
timeZone = TimeZone.getTimeZone(TIME_ZONE_ID_EXT)
97+
}.parse(this) ?: Date()
9198
} catch (e: ParseException) {
9299
e.printStackTrace()
93100
}

app/src/main/java/com/qomunal/opensource/androidresearch/ui/main/MainActivity.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import android.os.Bundle
44
import android.util.Log
55
import androidx.activity.viewModels
66
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
7+
import com.qomunal.opensource.androidresearch.common.ext.DATE_TIME_FORMAT
78
import com.qomunal.opensource.androidresearch.common.ext.ISO_FORMAT
9+
import com.qomunal.opensource.androidresearch.common.ext.SIMPLE_DATE_FORMAT
810
import com.qomunal.opensource.androidresearch.common.ext.countdownTimerExt
911
import com.qomunal.opensource.androidresearch.common.ext.fromIsoDateUTC
12+
import com.qomunal.opensource.androidresearch.common.ext.getDateTodayExt
1013
import com.qomunal.opensource.androidresearch.common.ext.getMillisExt
1114
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
15+
import java.util.Calendar
1216

1317

1418
class MainActivity : BaseActivity<ActivityMainBinding>() {
@@ -30,12 +34,25 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
3034
binding.apply {
3135

3236
val dates = "2025-06-16T08:54:12.000Z"
37+
val today = getDateTodayExt(ISO_FORMAT)
3338

34-
Log.d("Stechu", "dates: $dates")
35-
Log.d("Stechu", "dates: ${dates.fromIsoDateUTC()}")
36-
Log.d("Stechu", "dates: ${dates.getMillisExt(ISO_FORMAT)}")
39+
val currentTime = Calendar.getInstance().time
3740

38-
tvText.countdownTimerExt(dates.getMillisExt(ISO_FORMAT))
41+
val due = dates.getMillisExt(ISO_FORMAT) - currentTime.time
42+
43+
Log.d("CountDown", "today: $today")
44+
Log.d("CountDown", "today: ${today.fromIsoDateUTC()}")
45+
46+
Log.d("CountDown", "dates: $dates")
47+
Log.d("CountDown", "dates: ${dates.fromIsoDateUTC()}")
48+
49+
Log.d("CountDown", "today: ${today.getMillisExt(ISO_FORMAT)}")
50+
Log.d("CountDown", "dates: ${dates.getMillisExt(ISO_FORMAT)}")
51+
Log.d("CountDown", "due: $due")
52+
53+
tvToday.countdownTimerExt(today.getMillisExt(ISO_FORMAT))
54+
tvDate.countdownTimerExt(dates.getMillisExt(ISO_FORMAT))
55+
tvDue.countdownTimerExt(due)
3956
}
4057
}
4158

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
4+
android:layout_height="match_parent"
5+
android:gravity="center"
6+
android:orientation="vertical">
67

78
<TextView
8-
android:id="@+id/tv_text"
9+
android:id="@+id/tv_today"
910
android:layout_width="wrap_content"
1011
android:layout_height="wrap_content"
12+
android:text="Click On Me" />
13+
14+
<TextView
15+
android:id="@+id/tv_date"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_marginTop="16dp"
19+
android:text="Click On Me" />
20+
21+
<TextView
22+
android:id="@+id/tv_due"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_marginTop="16dp"
1126
android:text="Click On Me"
12-
app:layout_constraintBottom_toBottomOf="parent"
13-
app:layout_constraintEnd_toEndOf="parent"
14-
app:layout_constraintStart_toStartOf="parent"
15-
app:layout_constraintTop_toTopOf="parent" />
27+
android:textStyle="bold" />
1628

17-
</androidx.constraintlayout.widget.ConstraintLayout>
29+
</LinearLayout>

0 commit comments

Comments
 (0)