Having Settings management code scattered around might be a bit unwieldy; so, we will create a separate class, called Settings, for this purpose:
public class Settings {
}
Also, we will ensure that it will be instantiated only once using a singleton pattern:
public class Settings {
private static Settings INSTANCE;
private Settings(Context context) {
SharedPreferences preferences
= PreferenceManager.getDefaultSharedPreferences(context);
}
public synchronized static Settings get(Context context) {
if (INSTANCE != null) {
return INSTANCE;
}
INSTANCE = new Settings(context);
return INSTANCE;
}
}
The Settings class will expose two Subjects: one will be used to retrieve followed symbols and the second will be used to get monitored keywords for Twitter. Let's add two Subjects:
private Subject<List<String>> keywordsSubject
= BehaviorSubject.create();
private Subject<List<String>> symbolsSubject
= BehaviorSubject.create();
Now, let's expose them using getters:
public Observable<List<String>> getMonitoredKeywords() {
return keywordsSubject;
}
public Observable<List<String>> getMonitoredSymbols() {
return symbolsSubject;
}
Here, we've chosen to use BehaviorSubject because it will ensure that whenever we subscribe, it will emit the last available values from settings and will continue issuing new items whenever there are updates.