add a dialog for selecting extra timezones

This commit is contained in:
tibbi 2018-03-01 17:33:40 +01:00
parent d629ccaf94
commit e76847476e
7 changed files with 182 additions and 1 deletions

View File

@ -0,0 +1,89 @@
package com.simplemobiletools.clock.adapters
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.interfaces.MyAdapterListener
import kotlinx.android.synthetic.main.item_add_time_zone.view.*
import java.util.*
class SelectTimeZonesAdapter(val activity: SimpleActivity, val timeZones: ArrayList<MyTimeZone>) : RecyclerView.Adapter<SelectTimeZonesAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
private val config = activity.config
private val textColor = config.textColor
private val backgroundColor = config.backgroundColor
private val primaryColor = activity.getAdjustedPrimaryColor()
init {
val selectedTimeZones = config.selectedTimeZones
timeZones.forEachIndexed { index, myTimeZone ->
if (selectedTimeZones.contains(myTimeZone.id.toString())) {
selectedPositions.add(index)
}
}
}
private fun toggleItemSelection(select: Boolean, pos: Int) {
if (select) {
if (itemViews[pos] != null) {
selectedPositions.add(pos)
}
} else {
selectedPositions.remove(pos)
}
itemViews[pos]?.time_zone_checkbox?.isChecked = select
}
private val adapterListener = object : MyAdapterListener {
override fun toggleItemSelectionAdapter(select: Boolean, position: Int) {
toggleItemSelection(select, position)
}
override fun getSelectedPositions() = selectedPositions
override fun itemLongClicked(position: Int) {}
}
fun getSelectedItemsSet(): HashSet<String> {
val selectedItemsSet = HashSet<String>(selectedPositions.size)
selectedPositions.forEach { selectedItemsSet.add(timeZones[it].id.toString()) }
return selectedItemsSet
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = activity.layoutInflater.inflate(R.layout.item_add_time_zone, parent, false)
return ViewHolder(view, adapterListener)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val timeZone = timeZones[position]
itemViews.put(position, holder.bindView(timeZone, textColor, primaryColor, backgroundColor))
toggleItemSelection(selectedPositions.contains(position), position)
}
override fun getItemCount() = timeZones.size
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)
time_zone_checkbox.setColors(textColor, primaryColor, backgroundColor)
time_zone_holder.setOnClickListener {
adapterListener.toggleItemSelectionAdapter(!time_zone_checkbox.isChecked, adapterPosition)
}
}
return itemView
}
}
}

View File

@ -0,0 +1,31 @@
package com.simplemobiletools.clock.dialogs
import android.support.v7.app.AlertDialog
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.adapters.SelectTimeZonesAdapter
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.helpers.getAllTimeZones
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_select_time_zones.view.*
class AddTimeZonesDialog(val activity: SimpleActivity, private val callback: () -> Unit) {
private var view = activity.layoutInflater.inflate(R.layout.dialog_select_time_zones, null)
init {
view.select_time_zones_list.adapter = SelectTimeZonesAdapter(activity, getAllTimeZones())
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
private fun dialogConfirmed() {
val selectedTimeZones = (view?.select_time_zones_list?.adapter as? SelectTimeZonesAdapter)?.getSelectedItemsSet() ?: LinkedHashSet()
activity.config.selectedTimeZones = selectedTimeZones
callback()
}
}

View File

@ -7,6 +7,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.dialogs.AddTimeZonesDialog
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.updateTextColors
@ -99,6 +101,8 @@ class ClockFragment : Fragment() {
}
private fun fabClicked() {
AddTimeZonesDialog(activity as SimpleActivity) {
}
}
}

View File

@ -15,4 +15,8 @@ class Config(context: Context) : BaseConfig(context) {
var displayOtherTimeZones: Boolean
get() = prefs.getBoolean(DISPLAY_OTHER_TIME_ZONES, false)
set(displayOtherTimeZones) = prefs.edit().putBoolean(DISPLAY_OTHER_TIME_ZONES, displayOtherTimeZones).apply()
var selectedTimeZones: Set<String>
get() = prefs.getStringSet(SELECTED_TIME_ZONES, HashSet())
set(selectedTimeZones) = prefs.edit().putStringSet(SELECTED_TIME_ZONES, selectedTimeZones).apply()
}

View File

@ -5,10 +5,11 @@ import com.simplemobiletools.clock.models.MyTimeZone
// shared preferences
const val SHOW_SECONDS = "show_seconds"
const val DISPLAY_OTHER_TIME_ZONES = "display_other_time_zones"
const val SELECTED_TIME_ZONES = "selected_time_zones"
const val TABS_COUNT = 3
val timezones = arrayListOf(
fun getAllTimeZones() = arrayListOf(
MyTimeZone(1, "GMT-11:00 Midway", "Pacific/Midway"),
MyTimeZone(2, "GMT-10:00 Honolulu", "Pacific/Honolulu"),
MyTimeZone(3, "GMT-09:00 Anchorage", "America/Anchorage"),

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/select_time_zones_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/select_time_zones_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/>
</RelativeLayout>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/time_zone_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<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_checkbox"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/normal_text_size"
tools:text="GMT-11:00 Midway"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/time_zone_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/time_zone_title"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/time_zone_title"
android:layout_marginRight="@dimen/activity_margin"
android:clickable="false"
android:gravity="center"/>
</RelativeLayout>