show the selected time zone times on the main screen

This commit is contained in:
tibbi 2018-03-01 20:37:30 +01:00
parent e76847476e
commit d4edaf646b
7 changed files with 181 additions and 17 deletions

View File

@ -39,7 +39,7 @@ class SelectTimeZonesAdapter(val activity: SimpleActivity, val timeZones: ArrayL
selectedPositions.remove(pos)
}
itemViews[pos]?.time_zone_checkbox?.isChecked = select
itemViews[pos]?.add_time_zone_checkbox?.isChecked = select
}
private val adapterListener = object : MyAdapterListener {
@ -74,12 +74,12 @@ class SelectTimeZonesAdapter(val activity: SimpleActivity, val timeZones: ArrayL
class ViewHolder(view: View, val adapterListener: MyAdapterListener) : RecyclerView.ViewHolder(view) {
fun bindView(timeZone: MyTimeZone, textColor: Int, primaryColor: Int, backgroundColor: Int): View {
itemView.apply {
time_zone_title.text = timeZone.title
time_zone_title.setTextColor(textColor)
add_time_zone_title.text = timeZone.title
add_time_zone_title.setTextColor(textColor)
time_zone_checkbox.setColors(textColor, primaryColor, backgroundColor)
time_zone_holder.setOnClickListener {
adapterListener.toggleItemSelectionAdapter(!time_zone_checkbox.isChecked, adapterPosition)
add_time_zone_checkbox.setColors(textColor, primaryColor, backgroundColor)
add_time_zone_holder.setOnClickListener {
adapterListener.toggleItemSelectionAdapter(!add_time_zone_checkbox.isChecked, adapterPosition)
}
}

View File

@ -0,0 +1,70 @@
package com.simplemobiletools.clock.adapters
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.views.MyRecyclerView
import kotlinx.android.synthetic.main.item_time_zone.view.*
import java.util.*
class TimeZonesAdapter(activity: SimpleActivity, var timeZones: ArrayList<MyTimeZone>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
override fun getActionMenuId() = R.menu.cab_timezones
override fun prepareActionMode(menu: Menu) {}
override fun prepareItemSelection(view: View) {}
override fun markItemSelection(select: Boolean, view: View?) {
view?.time_zone_frame?.isSelected = select
}
override fun actionItemPressed(id: Int) {}
override fun getSelectableItemCount() = timeZones.size
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_time_zone, parent)
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val contact = timeZones[position]
val view = holder.bindView(contact, true) { itemView, layoutPosition ->
setupView(itemView, contact)
}
bindViewHolder(holder, position, view)
}
override fun getItemCount() = timeZones.size
fun updateItems(newItems: ArrayList<MyTimeZone>) {
timeZones = newItems
notifyDataSetChanged()
finishActMode()
}
fun updateTimes() {
notifyDataSetChanged()
}
private fun setupView(view: View, timeZone: MyTimeZone) {
val calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone.zoneName))
val offset = calendar.timeZone.rawOffset
val passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val format = "%02d:%02d"
val formattedTime = String.format(format, hours, minutes)
view.apply {
time_zone_title.text = timeZone.title.substring(timeZone.title.indexOf(' '))
time_zone_title.setTextColor(textColor)
time_zone_time.text = formattedTime
time_zone_time.setTextColor(textColor)
}
}
}

View File

@ -8,8 +8,11 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.adapters.TimeZonesAdapter
import com.simplemobiletools.clock.dialogs.AddTimeZonesDialog
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.helpers.getAllTimeZones
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.fragment_clock.view.*
@ -20,6 +23,7 @@ class ClockFragment : Fragment() {
private var passedSeconds = 0
private var isFirstResume = true
private var displayOtherTimeZones = false
private var calendar = Calendar.getInstance()
private val updateHandler = Handler()
@ -29,6 +33,7 @@ class ClockFragment : Fragment() {
view = inflater.inflate(R.layout.fragment_clock, container, false) as ViewGroup
val offset = calendar.timeZone.rawOffset
passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
displayOtherTimeZones = context!!.config.displayOtherTimeZones
updateCurrentTime()
updateDate()
setupViews()
@ -38,9 +43,11 @@ class ClockFragment : Fragment() {
override fun onResume() {
super.onResume()
displayOtherTimeZones = context!!.config.displayOtherTimeZones
if (!isFirstResume) {
setupViews()
}
isFirstResume = false
}
@ -50,7 +57,6 @@ class ClockFragment : Fragment() {
}
private fun setupViews() {
val displayOtherTimeZones = context!!.config.displayOtherTimeZones
view.apply {
context!!.updateTextColors(clock_fragment)
time_zones_list.beVisibleIf(displayOtherTimeZones)
@ -58,6 +64,10 @@ class ClockFragment : Fragment() {
clock_fab.setOnClickListener {
fabClicked()
}
if (displayOtherTimeZones) {
updateTimeZones()
}
}
}
@ -76,8 +86,14 @@ class ClockFragment : Fragment() {
view.clock_time.text = formattedText
if (hours == 0 && minutes == 0 && seconds == 0) {
updateDate()
if (seconds == 0) {
if (displayOtherTimeZones) {
(view.time_zones_list.adapter as? TimeZonesAdapter)?.updateTimes()
}
if (hours == 0 && minutes == 0) {
updateDate()
}
}
updateHandler.postDelayed({
@ -100,9 +116,25 @@ class ClockFragment : Fragment() {
view.clock_date.text = dateString
}
private fun updateTimeZones() {
if (!displayOtherTimeZones) {
return
}
val selectedTimeZoneIDs = context!!.config.selectedTimeZones.map { it.toInt() }
val timeZones = getAllTimeZones().filter { selectedTimeZoneIDs.contains(it.id) } as ArrayList<MyTimeZone>
val currAdapter = view.time_zones_list.adapter
if (currAdapter == null) {
val timeZonesAdapter = TimeZonesAdapter(activity as SimpleActivity, timeZones, view.time_zones_list) {}
view.time_zones_list.adapter = timeZonesAdapter
} else {
(currAdapter as TimeZonesAdapter).updateItems(timeZones)
}
}
private fun fabClicked() {
AddTimeZonesDialog(activity as SimpleActivity) {
updateTimeZones()
}
}
}

View File

@ -35,7 +35,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/clock_date"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginTop="@dimen/activity_margin"
android:clipToPadding="false"
android:scrollbars="none"
android:visibility="gone"

View File

@ -2,7 +2,7 @@
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/time_zone_holder"
android:id="@+id/add_time_zone_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
@ -10,10 +10,10 @@
android:focusable="true">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/time_zone_title"
android:id="@+id/add_time_zone_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/time_zone_checkbox"
android:layout_toLeftOf="@+id/add_time_zone_checkbox"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
@ -22,12 +22,12 @@
tools:text="GMT-11:00 Midway"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/time_zone_checkbox"
android:id="@+id/add_time_zone_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/time_zone_title"
android:layout_alignBottom="@+id/add_time_zone_title"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/time_zone_title"
android:layout_alignTop="@+id/add_time_zone_title"
android:layout_marginRight="@dimen/activity_margin"
android:clickable="false"
android:gravity="center"/>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/time_zone_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/selector">
<RelativeLayout
android:id="@+id/time_zone_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/time_zone_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/time_zone_time"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/big_text_size"
tools:text="GMT-11:00 Midway"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/time_zone_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/time_zone_title"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:gravity="center_vertical"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/actionbar_text_size"
tools:text="11:00"/>
</RelativeLayout>
</FrameLayout>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_edit"
android:icon="@drawable/ic_rename"
android:title="@string/rename"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_select_all"
android:icon="@drawable/ic_select_all"
android:title="@string/select_all"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_remove"
android:icon="@drawable/ic_minus_circle"
android:title="@string/remove"
app:showAsAction="ifRoom"/>
</menu>