diff --git a/Habitica/res/values/strings.xml b/Habitica/res/values/strings.xml index 7893f6526..b1a069c2d 100644 --- a/Habitica/res/values/strings.xml +++ b/Habitica/res/values/strings.xml @@ -25,7 +25,7 @@ Custom Day Start - User Push Notifications + Use Push Notifications Push Notifications Set your push notifications settings You won a Challenge! @@ -936,4 +936,9 @@ Learn more You can purchase this customization from the Time Travelers shop Go shopping + Email Notifications + Use Email Notifications + Subscription Reminders + Kicked from group + Guidance with setting up your Habitica Account diff --git a/Habitica/res/xml/preferences_fragment.xml b/Habitica/res/xml/preferences_fragment.xml index 1adcef4e7..f88a0744a 100644 --- a/Habitica/res/xml/preferences_fragment.xml +++ b/Habitica/res/xml/preferences_fragment.xml @@ -273,9 +273,88 @@ android:title="@string/preference_push_unjoined_guild_mention" android:layout="@layout/preference_child_summary"/> - + + + + + + + + + + + + + + + + + + + + + + + AuthenticationPreferenceFragment() "api" -> APIPreferenceFragment() "pushNotifications" -> PushNotificationsPreferencesFragment() + "emailNotifications" -> EmailNotificationsPreferencesFragment() else -> null } } diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/EmailNotificationsPreferencesFragment.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/EmailNotificationsPreferencesFragment.kt new file mode 100644 index 000000000..41048a0a0 --- /dev/null +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/preferences/EmailNotificationsPreferencesFragment.kt @@ -0,0 +1,80 @@ +package com.habitrpg.android.habitica.ui.fragments.preferences + +import android.content.SharedPreferences +import android.os.Bundle +import androidx.preference.CheckBoxPreference +import com.habitrpg.android.habitica.HabiticaBaseApplication +import com.habitrpg.android.habitica.helpers.RxErrorHandler +import com.habitrpg.android.habitica.models.user.User +import io.reactivex.functions.Consumer + +class EmailNotificationsPreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnSharedPreferenceChangeListener { + + private var isInitialSet: Boolean = true + private var isSettingUser: Boolean = false + + override fun onCreate(savedInstanceState: Bundle?) { + HabiticaBaseApplication.userComponent?.inject(this) + super.onCreate(savedInstanceState) + } + + override fun onResume() { + super.onResume() + preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this) + } + + override fun onPause() { + super.onPause() + preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this) + } + + override fun setupPreferences() { /* no-on */ } + + override fun setUser(user: User?) { + super.setUser(user) + isSettingUser = !isInitialSet + updatePreference("preference_email_you_won_challenge", user?.preferences?.emailNotifications?.wonChallenge) + updatePreference("preference_email_received_a_private_message", user?.preferences?.emailNotifications?.newPM) + updatePreference("preference_email_gifted_gems", user?.preferences?.emailNotifications?.giftedGems) + updatePreference("preference_email_gifted_subscription", user?.preferences?.emailNotifications?.giftedSubscription) + updatePreference("preference_email_invited_to_party", user?.preferences?.emailNotifications?.invitedParty) + updatePreference("preference_email_invited_to_guild", user?.preferences?.emailNotifications?.invitedGuild) + updatePreference("preference_email_your_quest_has_begun", user?.preferences?.emailNotifications?.questStarted) + updatePreference("preference_email_invited_to_quest", user?.preferences?.emailNotifications?.invitedQuest) + updatePreference("preference_email_important_announcements", user?.preferences?.emailNotifications?.majorUpdates) + updatePreference("preference_email_kicked_group", user?.preferences?.emailNotifications?.kickedGroup) + updatePreference("preference_email_onboarding", user?.preferences?.emailNotifications?.onboarding) + updatePreference("preference_email_subscription_reminders", user?.preferences?.emailNotifications?.subscriptionReminders) + isSettingUser = false + isInitialSet = false + } + + private fun updatePreference(key: String, isChecked: Boolean?) { + val preference = (findPreference(key) as? CheckBoxPreference) + preference?.isChecked = isChecked == true + } + + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { + if (isSettingUser) { + return + } + val pathKey = when (key) { + "preference_email_you_won_challenge" -> "wonChallenge" + "preference_email_received_a_private_message" -> "newPM" + "preference_email_gifted_gems" -> "giftedGems" + "preference_email_gifted_subscription" -> "giftedSubscription" + "preference_email_invited_to_party" -> "invitedParty" + "preference_email_invited_to_guild" -> "invitedGuild" + "preference_email_your_quest_has_begun" -> "questStarted" + "preference_email_invited_to_quest" -> "invitedQuest" + "preference_email_important_announcements" -> "majorUpdates" + "preference_email_kicked_group" -> "kickedGroup" + "preference_email_onboarding" -> "onboarding" + "preference_email_subscription_reminders" -> "subscriptionReminders" + else -> null + } + if (pathKey != null) { + compositeSubscription.add(userRepository.updateUser(user, "preferences.emailNotifications.$pathKey", sharedPreferences.getBoolean(key, false)).subscribe(Consumer { }, RxErrorHandler.handleEmptyError())) + } + } +} \ No newline at end of file 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 634185767..37a0ca815 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 @@ -44,6 +44,7 @@ class PreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnShare private var timePreference: TimePreference? = null private var pushNotificationsPreference: PreferenceScreen? = null + private var emailNotificationsPreference: PreferenceScreen? = null private var classSelectionPreference: Preference? = null private var serverUrlPreference: ListPreference? = null @@ -60,8 +61,12 @@ class PreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnShare pushNotificationsPreference = findPreference("pushNotifications") as? PreferenceScreen - val userPushNotifications = preferenceManager.sharedPreferences.getBoolean("usePushNotifications", true) - pushNotificationsPreference?.isEnabled = userPushNotifications + val usePushNotifications = preferenceManager.sharedPreferences.getBoolean("usePushNotifications", true) + pushNotificationsPreference?.isEnabled = usePushNotifications + + emailNotificationsPreference = findPreference("emailNotifications") as? PreferenceScreen + val useEmailNotifications = preferenceManager.sharedPreferences.getBoolean("useEmailNotifications", true) + emailNotificationsPreference?.isEnabled = useEmailNotifications classSelectionPreference = findPreference("choose_class") @@ -161,6 +166,10 @@ class PreferencesFragment : BasePreferencesFragment(), SharedPreferences.OnShare pushNotificationManager.removePushDeviceUsingStoredToken() } } + "useEmailNotifications" -> { + val useEmailNotifications = sharedPreferences.getBoolean(key, false) + emailNotificationsPreference?.isEnabled = useEmailNotifications + } "cds_time" -> { val timeval = sharedPreferences.getString("cds_time", "00:00") val pieces = timeval?.split(":".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()