mirror of https://github.com/tasks/tasks
Replace FilterSelectionActivity
* Add FilterPicker DialogFragment * Add FilterPicker compose component * Rename FilterSelectionActivity to WidgetFilterSelectionActivitypull/1964/head
parent
d157bbec53
commit
532fea0d29
@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.adapter
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.BaseAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import com.todoroo.astrid.api.FilterListItem
|
||||
import com.todoroo.astrid.api.FilterListItem.Type.*
|
||||
import org.tasks.billing.Inventory
|
||||
import org.tasks.filters.NavigationDrawerSubheader
|
||||
import org.tasks.themes.ColorProvider
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class FilterAdapter @Inject constructor(
|
||||
private val activity: Activity,
|
||||
private val locale: Locale,
|
||||
private val inventory: Inventory,
|
||||
private val colorProvider: ColorProvider,
|
||||
private val subheaderClickHandler: SubheaderClickHandler,
|
||||
) : BaseAdapter() {
|
||||
private var selected: Filter? = null
|
||||
private var items: List<FilterListItem> = ArrayList()
|
||||
|
||||
fun save(outState: Bundle) {
|
||||
outState.putParcelableArrayList(TOKEN_FILTERS, getItems())
|
||||
outState.putParcelable(TOKEN_SELECTED, selected)
|
||||
}
|
||||
|
||||
fun restore(savedInstanceState: Bundle) {
|
||||
items = savedInstanceState.getParcelableArrayList(TOKEN_FILTERS) ?: ArrayList()
|
||||
selected = savedInstanceState.getParcelable(TOKEN_SELECTED)
|
||||
}
|
||||
|
||||
fun setData(items: List<FilterListItem>, selected: Filter?) {
|
||||
AndroidUtilities.assertMainThread()
|
||||
this.items = items
|
||||
this.selected = selected
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun getCount(): Int {
|
||||
AndroidUtilities.assertMainThread()
|
||||
return items.size
|
||||
}
|
||||
|
||||
override fun getItem(position: Int): FilterListItem {
|
||||
AndroidUtilities.assertMainThread()
|
||||
return items[position]
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int) = position.toLong()
|
||||
|
||||
/** Create or reuse a view */
|
||||
private fun newView(convertView: View?, parent: ViewGroup, viewType: FilterListItem.Type): View {
|
||||
return if (convertView != null) {
|
||||
convertView
|
||||
} else {
|
||||
val newView = LayoutInflater.from(parent.context).inflate(viewType.layout, parent, false)
|
||||
newView.tag = when (viewType) {
|
||||
ITEM -> FilterViewHolder(
|
||||
newView, false, locale, activity, inventory, colorProvider, null)
|
||||
ACTION -> ActionViewHolder(activity, newView, null)
|
||||
SEPARATOR -> SeparatorViewHolder(newView)
|
||||
SUBHEADER -> SubheaderViewHolder(newView, subheaderClickHandler)
|
||||
}
|
||||
newView
|
||||
}
|
||||
}
|
||||
|
||||
private fun getItems(): ArrayList<FilterListItem> {
|
||||
AndroidUtilities.assertMainThread()
|
||||
return ArrayList(items)
|
||||
}
|
||||
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
val item = getItem(position)
|
||||
val view = newView(convertView, parent, item.itemType)
|
||||
val viewHolder = view.tag as RecyclerView.ViewHolder
|
||||
when (item.itemType) {
|
||||
ITEM -> (viewHolder as FilterViewHolder).bind(item, item == selected, 0)
|
||||
ACTION -> (viewHolder as ActionViewHolder).bind(item)
|
||||
SUBHEADER ->
|
||||
(viewHolder as SubheaderViewHolder).bind((item as NavigationDrawerSubheader))
|
||||
else -> {}
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
override fun getViewTypeCount() = VIEW_TYPE_COUNT
|
||||
|
||||
override fun isEnabled(position: Int) = getItem(position).itemType == ITEM
|
||||
|
||||
override fun getItemViewType(position: Int) = getItem(position).itemType.ordinal
|
||||
|
||||
companion object {
|
||||
private const val TOKEN_FILTERS = "token_filters"
|
||||
private const val TOKEN_SELECTED = "token_selected"
|
||||
private val VIEW_TYPE_COUNT = values().size
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package org.tasks.activities
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.fragment.app.setFragmentResult
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.tasks.R
|
||||
import org.tasks.compose.pickers.FilterPicker
|
||||
import org.tasks.dialogs.DialogBuilder
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class FilterPicker : DialogFragment() {
|
||||
@Inject lateinit var dialogBuilder: DialogBuilder
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
return dialogBuilder
|
||||
.newDialog()
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setContent {
|
||||
FilterPicker(
|
||||
selected = arguments?.getParcelable(EXTRA_FILTER),
|
||||
onSelected = { filter ->
|
||||
val data = Bundle()
|
||||
data.putParcelable(EXTRA_FILTER, filter)
|
||||
if (filter.valuesForNewTasks != null) {
|
||||
data.putString(
|
||||
EXTRA_FILTER_VALUES,
|
||||
AndroidUtilities.mapToSerializedString(filter.valuesForNewTasks))
|
||||
}
|
||||
setFragmentResult(SELECT_FILTER, data)
|
||||
dismiss()
|
||||
}
|
||||
)
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
override fun onCancel(dialog: DialogInterface) {
|
||||
super.onCancel(dialog)
|
||||
setFragmentResult(SELECT_FILTER, Bundle())
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val SELECT_FILTER = "select_filter"
|
||||
const val EXTRA_FILTER = "extra_filter"
|
||||
const val EXTRA_FILTER_VALUES = "extra_filter_values"
|
||||
|
||||
fun newFilterPicker(
|
||||
selected: Filter?,
|
||||
): FilterPicker {
|
||||
val dialog = FilterPicker()
|
||||
val arguments = Bundle()
|
||||
arguments.putParcelable(EXTRA_FILTER, selected)
|
||||
dialog.arguments = arguments
|
||||
return dialog
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package org.tasks.activities
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import com.todoroo.astrid.api.FilterListItem
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import org.tasks.LocalBroadcastManager
|
||||
import org.tasks.R
|
||||
import org.tasks.billing.Inventory
|
||||
import org.tasks.data.CaldavDao
|
||||
import org.tasks.data.GoogleTaskDao
|
||||
import org.tasks.filters.FilterProvider
|
||||
import org.tasks.filters.NavigationDrawerSubheader
|
||||
import org.tasks.preferences.Preferences
|
||||
import org.tasks.themes.ColorProvider
|
||||
import org.tasks.themes.CustomIcons
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class FilterPickerViewModel @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val filterProvider: FilterProvider,
|
||||
private val localBroadcastManager: LocalBroadcastManager,
|
||||
private val inventory: Inventory,
|
||||
private val colorProvider: ColorProvider,
|
||||
private val preferences: Preferences,
|
||||
private val googleTaskDao: GoogleTaskDao,
|
||||
private val caldavDao: CaldavDao,
|
||||
) : ViewModel() {
|
||||
|
||||
data class ViewState(
|
||||
val filters: List<FilterListItem> = emptyList(),
|
||||
)
|
||||
|
||||
private val _viewState = MutableStateFlow(ViewState())
|
||||
val viewState: StateFlow<ViewState>
|
||||
get() = _viewState.asStateFlow()
|
||||
|
||||
private val refreshReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
|
||||
private fun refresh() = viewModelScope.launch {
|
||||
val items = filterProvider.filterPickerItems()
|
||||
_viewState.update { it.copy(filters = items) }
|
||||
}
|
||||
|
||||
fun onClick(subheader: NavigationDrawerSubheader) = viewModelScope.launch {
|
||||
val collapsed = !subheader.isCollapsed
|
||||
when (subheader.subheaderType) {
|
||||
NavigationDrawerSubheader.SubheaderType.PREFERENCE ->
|
||||
preferences.setBoolean(subheader.id.toInt(), collapsed)
|
||||
NavigationDrawerSubheader.SubheaderType.GOOGLE_TASKS ->
|
||||
googleTaskDao.setCollapsed(subheader.id, collapsed)
|
||||
NavigationDrawerSubheader.SubheaderType.CALDAV,
|
||||
NavigationDrawerSubheader.SubheaderType.TASKS,
|
||||
NavigationDrawerSubheader.SubheaderType.ETESYNC ->
|
||||
caldavDao.setCollapsed(subheader.id, collapsed)
|
||||
}
|
||||
localBroadcastManager.broadcastRefreshList()
|
||||
}
|
||||
|
||||
fun getIcon(filter: Filter): Int {
|
||||
if (filter.icon < 1000 || inventory.hasPro) {
|
||||
val icon = CustomIcons.getIconResId(filter.icon)
|
||||
if (icon != null) {
|
||||
return icon
|
||||
}
|
||||
}
|
||||
return R.drawable.ic_list_24px
|
||||
}
|
||||
|
||||
fun getColor(filter: Filter): Int {
|
||||
if (filter.tint != 0) {
|
||||
val color = colorProvider.getThemeColor(filter.tint, true)
|
||||
if (color.isFree || inventory.purchasedThemes()) {
|
||||
return color.primaryColor
|
||||
}
|
||||
}
|
||||
return context.getColor(R.color.text_primary)
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
localBroadcastManager.unregisterReceiver(refreshReceiver)
|
||||
}
|
||||
|
||||
init {
|
||||
localBroadcastManager.registerRefreshListReceiver(refreshReceiver)
|
||||
refresh()
|
||||
}
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
package org.tasks.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import com.todoroo.astrid.adapter.FilterAdapter
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.launch
|
||||
import org.tasks.LocalBroadcastManager
|
||||
import org.tasks.dialogs.DialogBuilder
|
||||
import org.tasks.filters.FilterProvider
|
||||
import org.tasks.injection.InjectingAppCompatActivity
|
||||
import org.tasks.preferences.DefaultFilterProvider
|
||||
import org.tasks.preferences.Preferences
|
||||
import org.tasks.widget.WidgetPreferences
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class FilterSelectionActivity : InjectingAppCompatActivity() {
|
||||
@Inject lateinit var dialogBuilder: DialogBuilder
|
||||
@Inject lateinit var filterAdapter: FilterAdapter
|
||||
@Inject lateinit var filterProvider: FilterProvider
|
||||
@Inject lateinit var localBroadcastManager: LocalBroadcastManager
|
||||
@Inject lateinit var preferences: Preferences
|
||||
@Inject lateinit var defaultFilterProvider: DefaultFilterProvider
|
||||
|
||||
private var selected: Filter? = null
|
||||
|
||||
private val refreshReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val intent = intent
|
||||
val returnFilter = intent.getBooleanExtra(EXTRA_RETURN_FILTER, false)
|
||||
val widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
|
||||
selected = intent.getParcelableExtra(EXTRA_FILTER)
|
||||
if (savedInstanceState != null) {
|
||||
filterAdapter.restore(savedInstanceState)
|
||||
}
|
||||
dialogBuilder
|
||||
.newDialog()
|
||||
.setSingleChoiceItems(filterAdapter, -1) { dialog, which ->
|
||||
val selectedFilter = filterAdapter.getItem(which) as Filter
|
||||
val data = Intent()
|
||||
if (returnFilter) {
|
||||
data.putExtra(EXTRA_FILTER, selectedFilter)
|
||||
}
|
||||
if (widgetId != -1) {
|
||||
WidgetPreferences(this, preferences, widgetId)
|
||||
.setFilter(defaultFilterProvider.getFilterPreferenceValue(selectedFilter))
|
||||
localBroadcastManager.reconfigureWidget(widgetId)
|
||||
}
|
||||
data.putExtra(EXTRA_FILTER_NAME, selectedFilter.listingTitle)
|
||||
data.putExtra(EXTRA_FILTER_SQL, selectedFilter.getSqlQuery())
|
||||
if (selectedFilter.valuesForNewTasks != null) {
|
||||
data.putExtra(
|
||||
EXTRA_FILTER_VALUES,
|
||||
AndroidUtilities.mapToSerializedString(selectedFilter.valuesForNewTasks))
|
||||
}
|
||||
setResult(Activity.RESULT_OK, data)
|
||||
dialog.dismiss()
|
||||
}
|
||||
.setOnDismissListener { finish() }
|
||||
.show()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
localBroadcastManager.registerRefreshListReceiver(refreshReceiver)
|
||||
refresh()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
localBroadcastManager.unregisterReceiver(refreshReceiver)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
filterAdapter.save(outState)
|
||||
}
|
||||
|
||||
private fun refresh() = lifecycleScope.launch {
|
||||
val items = filterProvider.filterPickerItems()
|
||||
filterAdapter.setData(items, selected)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val EXTRA_RETURN_FILTER = "extra_include_filter"
|
||||
const val EXTRA_FILTER = "extra_filter"
|
||||
private const val EXTRA_FILTER_NAME = "extra_filter_name"
|
||||
private const val EXTRA_FILTER_SQL = "extra_filter_query"
|
||||
private const val EXTRA_FILTER_VALUES = "extra_filter_values"
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package org.tasks.compose.pickers
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.ContentAlpha
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment.Companion.CenterVertically
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import org.tasks.R
|
||||
import org.tasks.activities.FilterPickerViewModel
|
||||
import org.tasks.compose.collectAsStateLifecycleAware
|
||||
import org.tasks.filters.NavigationDrawerSubheader
|
||||
|
||||
@Composable
|
||||
fun FilterPicker(
|
||||
viewModel: FilterPickerViewModel = viewModel(),
|
||||
selected: Filter?,
|
||||
onSelected: (Filter) -> Unit,
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(vertical = 12.dp)
|
||||
) {
|
||||
val filters = viewModel.viewState.collectAsStateLifecycleAware().value.filters
|
||||
filters.forEach { filter ->
|
||||
when (filter) {
|
||||
is NavigationDrawerSubheader -> {
|
||||
CollapsibleRow(
|
||||
text = filter.listingTitle,
|
||||
collapsed = filter.isCollapsed,
|
||||
onClick = { viewModel.onClick(filter) },
|
||||
)
|
||||
}
|
||||
is Filter -> {
|
||||
CheckableIconRow(
|
||||
icon = painterResource(id = viewModel.getIcon(filter)),
|
||||
tint = Color(viewModel.getColor(filter)),
|
||||
selected = filter == selected,
|
||||
onClick = { onSelected(filter) },
|
||||
) {
|
||||
Row(verticalAlignment = CenterVertically) {
|
||||
Text(
|
||||
text = filter.listingTitle,
|
||||
style = MaterialTheme.typography.body2.copy(
|
||||
fontWeight = FontWeight.Medium
|
||||
),
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
if (filter.principals > 0) {
|
||||
Icon(
|
||||
painter = painterResource(
|
||||
id = when (filter.principals) {
|
||||
1 -> R.drawable.ic_outline_perm_identity_24px
|
||||
in 2..Int.MAX_VALUE -> R.drawable.ic_outline_people_outline_24
|
||||
else -> 0
|
||||
}
|
||||
),
|
||||
modifier = Modifier.alpha(ContentAlpha.medium),
|
||||
contentDescription = null,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package org.tasks.widget
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import com.todoroo.astrid.api.Filter
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.tasks.LocalBroadcastManager
|
||||
import org.tasks.activities.FilterPicker.Companion.EXTRA_FILTER
|
||||
import org.tasks.activities.FilterPicker.Companion.SELECT_FILTER
|
||||
import org.tasks.activities.FilterPicker.Companion.newFilterPicker
|
||||
import org.tasks.injection.InjectingAppCompatActivity
|
||||
import org.tasks.preferences.DefaultFilterProvider
|
||||
import org.tasks.preferences.Preferences
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class WidgetFilterSelectionActivity : InjectingAppCompatActivity() {
|
||||
@Inject lateinit var preferences: Preferences
|
||||
@Inject lateinit var defaultFilterProvider: DefaultFilterProvider
|
||||
@Inject lateinit var localBroadcastManager: LocalBroadcastManager
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
|
||||
if (widgetId == -1) {
|
||||
throw RuntimeException("Missing ${AppWidgetManager.EXTRA_APPWIDGET_ID}")
|
||||
}
|
||||
supportFragmentManager
|
||||
.setFragmentResultListener(SELECT_FILTER, this) { _, result ->
|
||||
if (result.isEmpty) {
|
||||
finish()
|
||||
return@setFragmentResultListener
|
||||
}
|
||||
result.getParcelable<Filter>(EXTRA_FILTER)?.let { filter ->
|
||||
WidgetPreferences(this, preferences, widgetId)
|
||||
.setFilter(defaultFilterProvider.getFilterPreferenceValue(filter))
|
||||
localBroadcastManager.reconfigureWidget(widgetId)
|
||||
}
|
||||
setResult(RESULT_OK, Intent().putExtras(result))
|
||||
finish()
|
||||
}
|
||||
newFilterPicker(intent.getParcelableExtra(EXTRA_FILTER))
|
||||
.show(supportFragmentManager, FRAG_TAG_FILTER_PICKER)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val FRAG_TAG_FILTER_PICKER = "frag_tag_filter_picker"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue