From 327f76db72f3c4f0f436a187fae4294fc511fe72 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 31 Aug 2022 11:33:51 +0200 Subject: [PATCH] Implement copy shared tasks setting --- .../res/layout/preference_category_groups.xml | 45 +++++++++++++++++++ Habitica/res/values/strings.xml | 2 + Habitica/res/xml/preferences_fragment.xml | 6 +++ .../preferences/PreferencesFragment.kt | 35 +++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 Habitica/res/layout/preference_category_groups.xml diff --git a/Habitica/res/layout/preference_category_groups.xml b/Habitica/res/layout/preference_category_groups.xml new file mode 100644 index 000000000..5a5594291 --- /dev/null +++ b/Habitica/res/layout/preference_category_groups.xml @@ -0,0 +1,45 @@ + + + + + + + \ No newline at end of file diff --git a/Habitica/res/values/strings.xml b/Habitica/res/values/strings.xml index b67a26474..188ed85a3 100644 --- a/Habitica/res/values/strings.xml +++ b/Habitica/res/values/strings.xml @@ -1252,4 +1252,6 @@ Self Improvement Spirituality Time Management + Accountability + Show assigned and open tasks on your personal task lists + Copy shared tasks diff --git a/Habitica/res/xml/preferences_fragment.xml b/Habitica/res/xml/preferences_fragment.xml index dd811f5ae..fc1e89013 100644 --- a/Habitica/res/xml/preferences_fragment.xml +++ b/Habitica/res/xml/preferences_fragment.xml @@ -191,6 +191,12 @@ android:title="@string/day_start_adjustment"/> + + diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/PreferencesFragment.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/PreferencesFragment.kt index 5577c4353..cf08b664c 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/PreferencesFragment.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/PreferencesFragment.kt @@ -7,9 +7,11 @@ import android.os.Bundle import android.view.View import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog +import androidx.lifecycle.lifecycleScope import androidx.preference.CheckBoxPreference import androidx.preference.ListPreference import androidx.preference.Preference +import androidx.preference.PreferenceCategory import androidx.preference.PreferenceScreen import com.habitrpg.android.habitica.BuildConfig import com.habitrpg.android.habitica.HabiticaBaseApplication @@ -33,6 +35,8 @@ import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog import com.habitrpg.android.habitica.ui.views.insufficientCurrency.InsufficientGemsDialog import com.habitrpg.common.habitica.helpers.AppTestingLevel import com.habitrpg.common.habitica.helpers.LanguageHelper +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.launch import java.util.Locale import javax.inject.Inject @@ -64,6 +68,8 @@ class PreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnShare override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) listView.itemAnimator = null + + userRepository.retrieveTeamPlans().subscribe({}, RxErrorHandler.handleEmptyError()) } override fun setupPreferences() { @@ -355,6 +361,35 @@ class PreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnShare emailNotificationsPreference?.isEnabled = useEmailNotifications useEmailPreference?.isChecked = useEmailNotifications + lifecycleScope.launch { + val teams = userRepository.getTeamPlans().firstOrNull() ?: return@launch + val context = context ?: return@launch + val groupCategory = findPreference("groups_category") + groupCategory?.removeAll() + if (teams.isEmpty()) { + groupCategory?.isVisible = false + } else { + groupCategory?.isVisible = true + for (team in teams) { + val newPreference = CheckBoxPreference(context) + newPreference.title = team.summary + newPreference.key = "copy_tasks-${team.id}" + newPreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue -> + val currentIds = user?.preferences?.tasks?.mirrorGroupTasks?.toMutableList() ?: mutableListOf() + if (newValue == true && !currentIds.contains(team.id)) { + currentIds.add(team.id) + } else if (newValue == false && currentIds.contains(team.id)) { + currentIds.remove(team.id) + } + userRepository.updateUser("preferences.tasks.mirrorGroupTasks", currentIds).subscribe({}, RxErrorHandler.handleEmptyError()) + true + } + groupCategory?.addPreference(newPreference) + newPreference.isChecked = user?.preferences?.tasks?.mirrorGroupTasks?.contains(team.id) == true + } + } + } + if (configManager.testingLevel() == AppTestingLevel.STAFF || BuildConfig.DEBUG) { serverUrlPreference?.isVisible = true taskListPreference?.isVisible = true