From 5f3b706b813331368de81677f9a8ae0ac4defc7f Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 11 Aug 2022 12:55:25 -0500 Subject: [PATCH] More calendar picker updates --- .../org/tasks/calendars/CalendarPicker.kt | 65 +++++-------------- ...alendarPickerList.kt => CalendarPicker.kt} | 47 ++++++++++++-- .../preferences/fragments/TaskDefaults.kt | 19 +++--- 3 files changed, 67 insertions(+), 64 deletions(-) rename app/src/main/java/org/tasks/compose/{CalendarPickerList.kt => CalendarPicker.kt} (64%) diff --git a/app/src/main/java/org/tasks/calendars/CalendarPicker.kt b/app/src/main/java/org/tasks/calendars/CalendarPicker.kt index ae0cc4aae..a307b026d 100644 --- a/app/src/main/java/org/tasks/calendars/CalendarPicker.kt +++ b/app/src/main/java/org/tasks/calendars/CalendarPicker.kt @@ -1,78 +1,49 @@ package org.tasks.calendars -import android.Manifest import android.app.Activity import android.app.Dialog import android.content.Intent import android.os.Bundle -import androidx.compose.runtime.LaunchedEffect import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import com.google.accompanist.permissions.ExperimentalPermissionsApi -import com.google.accompanist.permissions.rememberMultiplePermissionsState import dagger.hilt.android.AndroidEntryPoint -import org.tasks.compose.CalendarPickerList -import org.tasks.compose.collectAsStateLifecycleAware +import org.tasks.compose.CalendarPicker import org.tasks.dialogs.DialogBuilder import javax.inject.Inject @AndroidEntryPoint class CalendarPicker : DialogFragment() { - private val viewModel: CalendarPickerViewModel by viewModels() - @Inject lateinit var dialogBuilder: DialogBuilder - @OptIn(ExperimentalPermissionsApi::class) override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return dialogBuilder .newDialog() .setContent { - val hasPermissions = rememberMultiplePermissionsState( - permissions = listOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), - onPermissionsResult = { result -> - if (result.values.all { it }) { - viewModel.loadCalendars() - } - } + CalendarPicker( + selected = arguments?.getString(EXTRA_SELECTED), + onSelected = { + targetFragment!!.onActivityResult( + targetRequestCode, + Activity.RESULT_OK, + Intent().putExtra(EXTRA_CALENDAR_ID, it?.id) + ) + dismiss() + }, ) - if (hasPermissions.allPermissionsGranted) { - CalendarPickerList( - calendars = viewModel.viewState.collectAsStateLifecycleAware().value.calendars, - selected = arguments?.getString(EXTRA_SELECTED), - onClick = { - targetFragment!!.onActivityResult( - targetRequestCode, - Activity.RESULT_OK, - Intent() - .putExtra(EXTRA_CALENDAR_ID, it?.id) - .putExtra(EXTRA_CALENDAR_NAME, it?.name) - ) - dismiss() - }, - ) - } - LaunchedEffect(hasPermissions) { - if (!hasPermissions.allPermissionsGranted) { - hasPermissions.launchMultiplePermissionRequest() - } - } } .show() } companion object { const val EXTRA_CALENDAR_ID = "extra_calendar_id" - const val EXTRA_CALENDAR_NAME = "extra_calendar_name" private const val EXTRA_SELECTED = "extra_selected" - fun newCalendarPicker(target: Fragment?, rc: Int, selected: String?): CalendarPicker { - val arguments = Bundle() - arguments.putString(EXTRA_SELECTED, selected) - val fragment = CalendarPicker() - fragment.arguments = arguments - fragment.setTargetFragment(target, rc) - return fragment - } + fun newCalendarPicker(target: Fragment?, rc: Int, selected: String?) = + CalendarPicker().apply { + arguments = Bundle().apply { + putString(EXTRA_SELECTED, selected) + } + setTargetFragment(target, rc) + } } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/compose/CalendarPickerList.kt b/app/src/main/java/org/tasks/compose/CalendarPicker.kt similarity index 64% rename from app/src/main/java/org/tasks/compose/CalendarPickerList.kt rename to app/src/main/java/org/tasks/compose/CalendarPicker.kt index 88e50520b..9225c3d39 100644 --- a/app/src/main/java/org/tasks/compose/CalendarPickerList.kt +++ b/app/src/main/java/org/tasks/compose/CalendarPicker.kt @@ -1,5 +1,6 @@ package org.tasks.compose +import android.Manifest import android.content.res.Configuration import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding @@ -10,20 +11,54 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Block import androidx.compose.material.icons.outlined.Event import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.lifecycle.viewmodel.compose.viewModel +import com.google.accompanist.permissions.ExperimentalPermissionsApi +import com.google.accompanist.permissions.rememberMultiplePermissionsState import com.google.android.material.composethemeadapter.MdcTheme import org.tasks.R import org.tasks.calendars.AndroidCalendar +import org.tasks.calendars.CalendarPickerViewModel + +@OptIn(ExperimentalPermissionsApi::class) +@Composable +fun CalendarPicker( + viewModel: CalendarPickerViewModel = viewModel(), + selected: String?, + onSelected: (AndroidCalendar?) -> Unit, +) { + val hasPermissions = rememberMultiplePermissionsState( + permissions = listOf(Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR), + onPermissionsResult = { result -> + if (result.values.all { it }) { + viewModel.loadCalendars() + } + } + ) + if (hasPermissions.allPermissionsGranted) { + CalendarPickerList( + calendars = viewModel.viewState.collectAsStateLifecycleAware().value.calendars, + selected = selected, + onSelected = onSelected, + ) + } + LaunchedEffect(hasPermissions) { + if (!hasPermissions.allPermissionsGranted) { + hasPermissions.launchMultiplePermissionRequest() + } + } +} @Composable fun CalendarPickerList( calendars: List, selected: String?, - onClick: (AndroidCalendar?) -> Unit, + onSelected: (AndroidCalendar?) -> Unit, ) { val selectedCalendar = calendars.find { it.id == selected } MdcTheme { @@ -37,7 +72,7 @@ fun CalendarPickerList( tint = MaterialTheme.colors.onSurface, text = stringResource(id = R.string.dont_add_to_calendar), selected = selectedCalendar == null, - onClick = { onClick(null) }, + onClick = { onSelected(null) }, ) calendars.forEach { CheckableIconRow( @@ -45,7 +80,7 @@ fun CalendarPickerList( tint = Color(it.color), text = it.name, selected = selectedCalendar == it, - onClick = { onClick(it) } + onClick = { onSelected(it) } ) } } @@ -55,7 +90,7 @@ fun CalendarPickerList( @Preview(showBackground = true, widthDp = 320) @Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES, widthDp = 320) @Composable -fun CalendarPicker() { +fun CalendarPickerPreview() { MdcTheme { CalendarPickerList( calendars = listOf( @@ -64,7 +99,7 @@ fun CalendarPicker() { AndroidCalendar("3", "Personal", -10395295), ), selected = "2", - onClick = {}, + onSelected = {}, ) } } @@ -81,7 +116,7 @@ fun CalendarPickerNoneSelected() { AndroidCalendar("3", "Personal", -10395295), ), selected = null, - onClick = {}, + onSelected = {}, ) } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt b/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt index 78bfdead1..1d34ae75d 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt @@ -62,9 +62,7 @@ class TaskDefaults : InjectingPreferenceFragment() { .show(parentFragmentManager, FRAG_TAG_CALENDAR_PICKER) false } - val defaultCalendarName: String? = getDefaultCalendarName() - defaultCalendarPref.summary = defaultCalendarName - ?: getString(R.string.dont_add_to_calendar) + updateCalendarName() findPreference(R.string.p_default_list) .setOnPreferenceClickListener { @@ -138,9 +136,7 @@ class TaskDefaults : InjectingPreferenceFragment() { R.string.gcal_p_default, data!!.getStringExtra(CalendarPicker.EXTRA_CALENDAR_ID) ) - defaultCalendarPref.summary = - data.getStringExtra(CalendarPicker.EXTRA_CALENDAR_NAME) - ?: getString(R.string.dont_add_to_calendar) + updateCalendarName() } REQUEST_RECURRENCE -> if (resultCode == RESULT_OK) { preferences.setString( @@ -170,6 +166,12 @@ class TaskDefaults : InjectingPreferenceFragment() { updateDefaultLocation() } + private fun updateCalendarName() { + val calendarId = preferences.defaultCalendar + val name = calendarProvider.getCalendar(calendarId)?.name + defaultCalendarPref.summary = name ?: getString(R.string.dont_add_to_calendar) + } + override fun onResume() { super.onResume() @@ -180,11 +182,6 @@ class TaskDefaults : InjectingPreferenceFragment() { updateDefaultReminders() } - private fun getDefaultCalendarName(): String? { - val calendarId = preferences.defaultCalendar - return calendarProvider.getCalendar(calendarId)?.name - } - private fun updateRemoteListSummary() = lifecycleScope.launch { val defaultFilter = defaultFilterProvider.getDefaultList() findPreference(R.string.p_default_list).summary = defaultFilter.listingTitle