Skip to content

Commit e99e736

Browse files
author
Lyla
committed
TFCM.05-Solution-ImplementInstructorTopicFollowing
1 parent 55e824f commit e99e736

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

app/src/main/java/android/example/com/squawker/following/FollowingPreferenceFragment.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
*/
1616
package android.example.com.squawker.following;
1717

18+
import android.content.SharedPreferences;
1819
import android.example.com.squawker.R;
1920
import android.os.Bundle;
21+
import android.support.v7.preference.Preference;
2022
import android.support.v7.preference.PreferenceFragmentCompat;
23+
import android.support.v7.preference.SwitchPreferenceCompat;
24+
import android.util.Log;
25+
26+
import com.google.firebase.messaging.FirebaseMessaging;
2127

2228

2329
/**
2430
* Shows the list of instructors you can follow
2531
*/
26-
// TODO (1) Implement onSharedPreferenceChangeListener
27-
public class FollowingPreferenceFragment extends PreferenceFragmentCompat {
32+
public class FollowingPreferenceFragment extends PreferenceFragmentCompat implements
33+
SharedPreferences.OnSharedPreferenceChangeListener {
2834

2935
private final static String LOG_TAG = FollowingPreferenceFragment.class.getSimpleName();
3036

@@ -33,16 +39,52 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
3339
// Add visualizer preferences, defined in the XML file in res->xml->preferences_squawker
3440
addPreferencesFromResource(R.xml.following_squawker);
3541
}
36-
// TODO (2) When a SharedPreference changes, check which preference it is and subscribe or
37-
// un-subscribe to the correct topics.
3842

39-
// Ex. FirebaseMessaging.getInstance().subscribeToTopic("key_lyla");
40-
// subscribes to Lyla's squawks.
43+
/**
44+
* Triggered when shared preferences changes. This will be triggered when a person is followed
45+
* or un-followed
46+
*
47+
* @param sharedPreferences SharedPreferences file
48+
* @param key The key of the preference which was changed
49+
*/
50+
@Override
51+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
52+
53+
Preference preference = findPreference(key);
54+
if (preference != null && preference instanceof SwitchPreferenceCompat) {
55+
// Get the current state of the switch preference
56+
boolean isOn = sharedPreferences.getBoolean(key, false);
57+
if (isOn) {
58+
// The preference key matches the following key for the associated instructor in
59+
// FCM. For example, the key for Lyla is key_lyla (as seen in
60+
// following_squawker.xml). The topic for Lyla's messages is /topics/key_lyla
61+
62+
// Subscribe
63+
FirebaseMessaging.getInstance().subscribeToTopic(key);
64+
Log.d(LOG_TAG, "Subscribing to " + key);
65+
} else {
66+
// Un-subscribe
67+
FirebaseMessaging.getInstance().unsubscribeFromTopic(key);
68+
Log.d(LOG_TAG, "Un-subscribing to " + key);
69+
}
70+
}
71+
}
72+
4173

42-
// HINT: Checkout res->xml->following_squawker.xml. Note how the keys for each of the
43-
// preferences matches the topic to subscribe to for each instructor.
74+
@Override
75+
public void onCreate(Bundle savedInstanceState) {
76+
super.onCreate(savedInstanceState);
77+
// Add the shared preference change listener
78+
getPreferenceScreen().getSharedPreferences()
79+
.registerOnSharedPreferenceChangeListener(this);
80+
}
4481

45-
// TODO (3) Make sure to register and unregister this as a Shared Preference Change listener, in
46-
// onCreate and onDestroy.
82+
@Override
83+
public void onDestroy() {
84+
super.onDestroy();
85+
// Remove the shared preference change listener
86+
getPreferenceScreen().getSharedPreferences()
87+
.unregisterOnSharedPreferenceChangeListener(this);
88+
}
4789

4890
}

0 commit comments

Comments
 (0)