add a sorting dialog

This commit is contained in:
tibbi 2017-12-10 16:19:50 +01:00
parent 8678a7d109
commit cb6c3fedec
6 changed files with 152 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.contacts.BuildConfig
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.adapters.ContactsAdapter
import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact
@ -36,6 +37,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
finish()
}
}
storeStateVariables()
}
override fun onResume() {
@ -58,6 +61,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.sort -> showSortingDialog()
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
R.id.about -> launchAbout()
else -> return super.onOptionsItemSelected(item)
@ -65,14 +69,20 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
return true
}
private fun storeStateVariables() {
storedUseEnglish = config.useEnglish
private fun showSortingDialog() {
ChangeSortingDialog(this) {
initContacts()
}
}
private fun launchAbout() {
startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_MULTISELECT, BuildConfig.VERSION_NAME)
}
private fun storeStateVariables() {
storedUseEnglish = config.useEnglish
}
private fun initContacts() {
ContactsHelper(this).getContacts {
Contact.sorting = config.sorting
@ -113,6 +123,6 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
override fun refreshItems() {
initContacts()
}
}

View File

@ -0,0 +1,64 @@
package com.simplemobiletools.contacts.dialogs
import android.support.v7.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.config
import kotlinx.android.synthetic.main.dialog_change_sorting.view.*
class ChangeSortingDialog(val activity: BaseSimpleActivity, val callback: () -> Unit) {
private var currSorting = 0
private var config = activity.config
private var view = activity.layoutInflater.inflate(R.layout.dialog_change_sorting, null)
init {
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.sort_by)
}
currSorting = config.sorting
setupSortRadio()
setupOrderRadio()
}
private fun setupSortRadio() {
val sortingRadio = view.sorting_dialog_radio_sorting
val sortBtn = when {
currSorting and SORT_BY_NUMBER != 0 -> sortingRadio.sorting_dialog_radio_number
else -> sortingRadio.sorting_dialog_radio_name
}
sortBtn.isChecked = true
}
private fun setupOrderRadio() {
val orderRadio = view.sorting_dialog_radio_order
var orderBtn = orderRadio.sorting_dialog_radio_ascending
if (currSorting and SORT_DESCENDING != 0) {
orderBtn = orderRadio.sorting_dialog_radio_descending
}
orderBtn.isChecked = true
}
private fun dialogConfirmed() {
val sortingRadio = view.sorting_dialog_radio_sorting
var sorting = when (sortingRadio.checkedRadioButtonId) {
R.id.sorting_dialog_radio_number -> SORT_BY_NUMBER
else -> SORT_BY_NAME
}
if (view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) {
sorting = sorting or SORT_DESCENDING
}
config.sorting = sorting
callback()
}
}

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sorting_dialog_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/sorting_dialog_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<RadioGroup
android:id="@+id/sorting_dialog_radio_sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/name"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/number"/>
</RadioGroup>
<include
layout="@layout/divider"/>
<RadioGroup
android:id="@+id/sorting_dialog_radio_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_ascending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/ascending"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_descending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/descending"/>
</RadioGroup>
</LinearLayout>
</ScrollView>

View File

@ -1,6 +1,11 @@
<?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/sort"
android:icon="@drawable/ic_sort"
android:title="@string/sort_by"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/settings"
android:title="@string/settings"

View File

@ -1,4 +1,5 @@
<resources>
<string name="app_name">Jednoduché kontakty</string>
<string name="app_launcher_name">Kontakty</string>
<string name="number">Číslo</string>
</resources>

View File

@ -1,4 +1,5 @@
<resources>
<string name="app_name">Simple Contacts</string>
<string name="app_launcher_name">Contacts</string>
<string name="number">Number</string>
</resources>