Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Rx and Preferences
@hotchemi
November 24, 2016
Programming
2
130
Rx and Preferences
http://www.meetup.com/Tokyo-Android-Meetup/events/235119373
@hotchemi
November 24, 2016
Tweet
Share
More Decks by @hotchemi
See All by @hotchemi
kompile-testing internal
hotchemi
0
210
The things we’ve learned from iOS×React Native hybrid development
hotchemi
2
4.2k
React Nativeを活用したアプリ開発体制/sapuri meetup
hotchemi
3
6.9k
Type-Safe i18n on RN
hotchemi
2
840
Navigation in a hybrid app
hotchemi
3
980
PermissionsDispatcher × Kotlin
hotchemi
0
1.9k
kotlin compiler plugin
hotchemi
1
520
Introducing PermissionsDispatcher
hotchemi
1
110
khronos
hotchemi
4
1.6k
Other Decks in Programming
See All in Programming
JSのウェブフレームワークで高速なルーターを実装する方法
usualoma
1
1.8k
Get Ready for Jakarta EE 10
ivargrimstad
0
2.5k
無限スクロールビューライブラリ 二つの設計思想比較
harumak
0
250
クックパッドマートの失敗したデータ設計 Before / After 大放出
mokuzon
0
170
Running Laravel/PHP on AWS (AWS Builders Day Taiwan 2022)
dwchiang
0
150
Springin‘でみんなもクリエイターに!
ueponx
0
220
競プロのすすめ
uya116
0
670
Independently together: better developer experience & App performance
bcinarli
0
180
Reactは何を提供するLibraryなのか?
taro28
3
380
io22 extended What's new in app performance
veronikapj
0
340
Chart実装が楽になりました。
keisukeyamagishi
0
120
Imperative is dead, long live Declarative! | Appdevcon
prof18
0
100
Featured
See All Featured
Documentation Writing (for coders)
carmenhchung
48
2.6k
What's in a price? How to price your products and services
michaelherold
229
9.4k
Designing for Performance
lara
597
63k
Git: the NoSQL Database
bkeepers
PRO
415
59k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
119
28k
Producing Creativity
orderedlist
PRO
334
37k
The Invisible Customer
myddelton
110
11k
Building an army of robots
kneath
299
40k
Fontdeck: Realign not Redesign
paulrobertlloyd
73
4.1k
Principles of Awesome APIs and How to Build Them.
keavy
113
15k
Navigating Team Friction
lara
175
11k
KATA
mclloyd
7
8.7k
Transcript
Rx and Preferences Shintaro Katafuchi/hotchemi
• Shared Preferences • Just a XML based KVS •
boolean,String,float,int,long,Set<String> • Tend to be messy to manage keys
public class PreferenceManager { private static final String KEY_USER_ID
= "user_id"; public putUserId(int userId) { prefs.edit().putInt(KEY_USER_ID, userId).apply(); } public int getUserId() { return prefs.getUserId(KEY_USER_ID, -1); } public boolean hasUserId() { return prefs.contains(KEY_USER_ID); } public void removeUserId() { prefs.edit().remove(KEY_USER_ID).apply(); } }
private static final String SHARED_PREFS_NAME = "HogeActivity"; private static
final String LAST_UPDATE_TICK_KEY = "lastUpdateTick"; private static final String LAST_DAILY_TASK_TICK_KEY = "lastDailyTaskTick"; private static final String LAST_SPLASH_EVENT_KEY = "lastSplashEventTick"; private static final String FCM_TOKEN_KEY = "fcmToken"; private static final String GCM_REGISTRATION_ID_KEY = "gcmRegistrationId"; private static final String GCM_APP_VERSION_KEY = "gcmAppVersion"; private static final String INVALIDATED_COUNT_KEY = "invalidatedCount"; private static final String LAUNCH_COUNT_KEY = "launchCount"; private static final String FIRST_BOOT_TICK_KEY = "firstBootTick"; private static final String RATING_DIALOG_KEY = "ratingDialogKey";
• Rx and Preferences • Deal with every “data” as
Observable • We need a subscription mechanism • Yes, RxJava! • Compound with other Rx libraries
Android Application Architecture https://labs.ribot.co.uk/android-application-architecture-8b6e34acda65#.74al8nhsz
None
None
• What I wanna do… • manage keys easily •
subscribe a data with Rx
hotchemi/tiamat https://github.com/hotchemi/tiamat
apply plugin: 'android-apt' dependencies { compile ‘com.github.hotchemi:tiamat:0.8.1’ provided ‘com.github.hotchemi:tiamat-compiler:0.8.1’ }
Install
@Pref("sample") class Sample { @Key(name = "long_value") long longValue =
false; // you can define default value like stringValue @Key(name = "string_value") String stringValue = "default_value"; @Key(name = "boolean_value") boolean booleanValue; @Key(name = "int_value") int intValue; @Key(name = "float_value") float floatValue; @Key(name = "set_string") Set<String> setStringValue; } Define a model
RxPreferences preferences = new SampleSharedPreferences(context); preferences.putStringValue(string); preferences.putStringValue(string, defaultValue); preferences.getStringValue(); preferences.hasStringValue();
preferences.removeStringValue(); Generate codes
// as primitive boolean value = preferences.getBooleanValue().asValue(); // as Observable
Observable<Boolean> value = preferences.getBooleanValue().asObservable(); // as Action Action1<? super Boolean> value = preferences.getBooleanValue().asAction(); As RxJava
subscriptions.add(preference.asObservable() .observeOn(AndroidSchedulers.mainThread()) .subscribe(RxCompoundButton.checked(checkBox))); subscriptions.add(RxCompoundButton.checkedChanges(checkBox) .skip(1) .subscribe(preference.asAction())); With Rx libraries
None
Everything is a stream!