Trying to set a repeating alarm from a PreferenceFragment but not working. How can I fix this?

53 Views Asked by At

So I have a settings fragment that extends to the PreferenceFragmentCompat inside the main activity of my android app that can setup has a switch to turn on alarm. However, the repeating alarm is not working and I'm not quite sure why.

Here is the code for some of the related methods in my fragment that I call:

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
    if (key == USERNAME){
        val username = sharedPreferences.getString(USERNAME, DEFAULT_VALUE)
        usernamePreference.summary = username
    }
    if (reminderPreference.isChecked){
        saveReminder(true,sharedPreferences)
        alarmReceiver.setRepeatingAlarm(fragmentContext,"RepeatingAlarm","01:20","Github reminder")
    } else {
        saveReminder(false,sharedPreferences)
        alarmReceiver.cancelAlarm(fragmentContext)
    }
}

private fun saveReminder(state: Boolean,sharedPreferences: SharedPreferences) {
    val settingsPreference = SettingsPreference(fragmentContext)
    settings = SettingsModel()
    settings.reminder = state
    settings.username = sharedPreferences.getString(USERNAME,DEFAULT_VALUE)
    settingsPreference.setSettings(settings)
}

Here are also some code from the SettingsPreference class:

internal class SettingsPreference(context: Context){
companion object{
    private const val PREFS_NAME = "settings_pref"
    private const val USERNAME_KEY = "username"
    private const val REMINDER_KEY = "switch_preference"
}

private val preferences = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE)

fun setSettings(value: SettingsModel){
    val editor = preferences.edit()
    editor.putString(USERNAME_KEY,value.username)
    editor.putBoolean(REMINDER_KEY,value.reminder)
    editor.apply()
}

fun getSettings(): SettingsModel{
    val model = SettingsModel()
    model.username = preferences.getString(USERNAME_KEY,"username")
    model.reminder = preferences.getBoolean(REMINDER_KEY,false)
    return model
}

}

The full code can be seen in my github repository: https://github.com/briancatraguna/GitHubClone

UPDATE: Maybe its because of my fragment context that is causing the problem?

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    addPreferencesFromResource(R.xml.preferences)
    init()
    setSummaries()
}

private fun init(){
    USERNAME = resources.getString(R.string.key_username)
    usernamePreference = findPreference<EditTextPreference>(USERNAME) as EditTextPreference

    LANGUAGE = resources.getString(R.string.language)
    languageSettings = findPreference<Preference>(LANGUAGE) as Preference

    REMINDER = resources.getString(R.string.switch_preference)
    reminderPreference = findPreference<SwitchPreference>(REMINDER) as SwitchPreference

    alarmReceiver = AlarmReceiver()

    fragmentContext = activity?.applicationContext!!
    fragmentContext.registerReceiver(alarmReceiver, IntentFilter())
    val settingsPreference = SettingsPreference(fragmentContext)
    if (settingsPreference.getSettings().reminder){
        reminderPreference.isChecked = true
    } else {
        reminderPreference.isChecked = false
    }
}
0

There are 0 best solutions below