From 40cee86c0d329b3606be3acdd92cd794f7b3d2a3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 28 Feb 2018 18:51:39 +0100 Subject: [PATCH] add the date under time --- app/build.gradle | 2 +- .../clock/activities/MainActivity.kt | 3 ++- .../clock/adapters/ViewPagerAdapter.kt | 3 ++- .../clock/fragments/ClockFragment.kt | 26 ++++++++++++++++--- .../clock/helpers/Constants.kt | 2 ++ app/src/main/res/layout/fragment_clock.xml | 9 +++++++ build.gradle | 1 + 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index fbc1b5b2..c75d1d39 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.13.20' + implementation 'com.simplemobiletools:commons:3.14.1' } Properties props = new Properties() diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/MainActivity.kt index 4d1cc273..1dc5cb68 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/MainActivity.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.clock.BuildConfig import com.simplemobiletools.clock.R import com.simplemobiletools.clock.adapters.ViewPagerAdapter import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.helpers.TABS_COUNT import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN import com.simplemobiletools.commons.models.FAQItem @@ -102,7 +103,7 @@ class MainActivity : SimpleActivity() { } viewpager.currentItem = config.lastUsedViewPagerPage - viewpager.offscreenPageLimit = 2 + viewpager.offscreenPageLimit = TABS_COUNT - 1 main_tabs_holder.onTabSelectionChanged( tabUnselectedAction = { it.icon?.applyColorFilter(config.textColor) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/ViewPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/ViewPagerAdapter.kt index 633d2f71..a1674ce3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/ViewPagerAdapter.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.ViewGroup import com.simplemobiletools.clock.R import com.simplemobiletools.clock.activities.SimpleActivity +import com.simplemobiletools.clock.helpers.TABS_COUNT class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() { @@ -19,7 +20,7 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() { container.removeView(item as View) } - override fun getCount() = 3 + override fun getCount() = TABS_COUNT override fun isViewFromObject(view: View, item: Any) = view == item private fun getFragment(position: Int) = when (position) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt index 3197b05a..ea059598 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.Handler import android.util.AttributeSet import android.widget.RelativeLayout +import com.simplemobiletools.clock.R import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.commons.extensions.updateTextColors import kotlinx.android.synthetic.main.fragment_clock.view.* @@ -12,24 +13,26 @@ import java.util.* class ClockFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { private val ONE_SECOND = 1000L private var passedSeconds = 0 + private val calendar = Calendar.getInstance() private val updateHandler = Handler() override fun onFinishInflate() { super.onFinishInflate() context.updateTextColors(clock_fragment) - val offset = Calendar.getInstance().timeZone.rawOffset - passedSeconds = ((Calendar.getInstance().timeInMillis + offset) / 1000).toInt() + val offset = calendar.timeZone.rawOffset + passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt() updateCurrentTime() + updateDate() } private fun updateCurrentTime() { val hours = (passedSeconds / 3600) % 24 val minutes = (passedSeconds / 60) % 60 + val seconds = passedSeconds % 60 var format = "%02d:%02d" val formattedText = if (context.config.showSeconds) { - val seconds = passedSeconds % 60 format += ":%02d" String.format(format, hours, minutes, seconds) } else { @@ -38,12 +41,29 @@ class ClockFragment(context: Context, attributeSet: AttributeSet) : RelativeLayo clock_time.text = formattedText + if (hours == 0 && minutes == 0 && seconds == 0) { + updateDate() + } + updateHandler.postDelayed({ passedSeconds++ updateCurrentTime() }, ONE_SECOND) } + private fun updateDate() { + val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 // make sure index 0 means monday + val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) + val month = calendar.get(Calendar.MONTH) + + val dayString = context.resources.getStringArray(R.array.week_days)[dayOfWeek] + val shortDayString = dayString.substring(0, Math.min(3, dayString.length)) + val monthString = context.resources.getStringArray(R.array.months)[month] + + val dateString = "$shortDayString, $dayOfMonth $monthString" + clock_date.text = dateString + } + override fun onDetachedFromWindow() { super.onDetachedFromWindow() updateHandler.removeCallbacksAndMessages(null) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt index 136c4df3..d6655013 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt @@ -2,3 +2,5 @@ package com.simplemobiletools.clock.helpers // shared preferences const val SHOW_SECONDS = "show_seconds" + +const val TABS_COUNT = 3 diff --git a/app/src/main/res/layout/fragment_clock.xml b/app/src/main/res/layout/fragment_clock.xml index 9cfcaabf..f8f8e784 100644 --- a/app/src/main/res/layout/fragment_clock.xml +++ b/app/src/main/res/layout/fragment_clock.xml @@ -15,4 +15,13 @@ android:textSize="@dimen/clock_text_size" tools:text="00:00:00"/> + + diff --git a/build.gradle b/build.gradle index 45490e79..a3b736f3 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,7 @@ buildscript { google() jcenter() } + dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"