mirror of https://github.com/tasks/tasks
Use coroutines in filter provider
parent
2c57d06980
commit
85e8cb49cc
@ -1,129 +0,0 @@
|
||||
package org.tasks.activities;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import com.todoroo.andlib.utility.AndroidUtilities;
|
||||
import com.todoroo.astrid.adapter.FilterAdapter;
|
||||
import com.todoroo.astrid.api.Filter;
|
||||
import dagger.hilt.android.AndroidEntryPoint;
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import javax.inject.Inject;
|
||||
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;
|
||||
|
||||
@AndroidEntryPoint
|
||||
public class FilterSelectionActivity extends InjectingAppCompatActivity {
|
||||
|
||||
public static final String EXTRA_RETURN_FILTER = "extra_include_filter";
|
||||
public static final String EXTRA_FILTER = "extra_filter";
|
||||
private static final String EXTRA_FILTER_NAME = "extra_filter_name";
|
||||
private static final String EXTRA_FILTER_SQL = "extra_filter_query";
|
||||
private static final String EXTRA_FILTER_VALUES = "extra_filter_values";
|
||||
|
||||
@Inject @ApplicationContext Context context;
|
||||
@Inject DialogBuilder dialogBuilder;
|
||||
@Inject FilterAdapter filterAdapter;
|
||||
@Inject FilterProvider filterProvider;
|
||||
@Inject LocalBroadcastManager localBroadcastManager;
|
||||
@Inject Preferences preferences;
|
||||
@Inject DefaultFilterProvider defaultFilterProvider;
|
||||
|
||||
private CompositeDisposable disposables;
|
||||
private Filter selected;
|
||||
private final BroadcastReceiver refreshReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
refresh();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Intent intent = getIntent();
|
||||
boolean returnFilter = intent.getBooleanExtra(EXTRA_RETURN_FILTER, false);
|
||||
int 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) -> {
|
||||
final Filter selectedFilter = (Filter) filterAdapter.getItem(which);
|
||||
Intent data = new Intent();
|
||||
if (returnFilter) {
|
||||
data.putExtra(EXTRA_FILTER, selectedFilter);
|
||||
}
|
||||
if (widgetId != -1) {
|
||||
new WidgetPreferences(context, 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(RESULT_OK, data);
|
||||
dialog.dismiss();
|
||||
})
|
||||
.setOnDismissListener(dialog -> finish())
|
||||
.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
disposables = new CompositeDisposable();
|
||||
|
||||
localBroadcastManager.registerRefreshListReceiver(refreshReceiver);
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
||||
localBroadcastManager.unregisterReceiver(refreshReceiver);
|
||||
|
||||
disposables.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
filterAdapter.save(outState);
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
disposables.add(
|
||||
Single.fromCallable(() -> filterProvider.getFilterPickerItems())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(items -> filterAdapter.setData(items, selected)));
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
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 dagger.hilt.android.qualifiers.ApplicationContext
|
||||
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 @ApplicationContext lateinit var context: Context
|
||||
@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(context, 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"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue