Some simple kotlin extension methods and tools can speed up the development of Android.
dependencies {
// add dependency, please replace x.y.z to the latest version
implementation "com.xueqiu.toolbox:toolbox:x.y.z"
}
// init gson manager
GsonManager.init(GsonOptions().setLowercaseWithUnderscores(true).setSerializeSpecialFloatingPointValues(true))
// then use it everywhere
val gson = GsonManager.getGson()
Create a storage class which extends BaseStorage
object AppStore {
var testData by AppStorage("test", "fake value")
class AppStorage<T>(key: String, defValue: T) : BaseStorage<T>(key, defValue) {
override fun getStorage(): SharedPreferences =
AppContext.INSTANCE.getSharedPreferences("app", Context.MODE_PRIVATE)
}
}
Then use it
// write data
AppStore.testData = "test data"
// read data
val data = AppStore.testData
// post message
Courier.post(TestEvent(params))
// and receive message
Courier.toObservable(TestEvent::class.java)
.observeOn(MainSchedulers.mainThread)
.subscribe {
// do something
}