Android Boilerplate “Ireng Jambon” as easy as create view
This a android boilerplate called “Ireng Jambon” desain for easy use and many purpose android application. Design inspire by Fernado Cejas for 2 main package core and feature, combine with easy ViewModel Android architecture implementation.
4 Easy Step Usage
- Set Basic Url and Set Endpoint
productFlavors {
dev {
applicationIdSuffix ".dev"
versionNameSuffix "-dev"
buildConfigField "String", "BASE_URL", "\"https://myserver.com\""
}
prod {
buildConfigField "String", "BASE_URL", "\"https://myserver.com\""
}
}
Set base url in flavoring app build.gradle
interface Api {
@POST("login")
fun login() : BaseResult
@GET("square/retrofit/contributors")
fun getContributor(): Observable<List<Contributor>>
//add endpoint here
Create Model
Api.kt
interface for define endpoint, param and return result.
2. Create Model
data class BaseResult(val code:Int, val message:String)
data class Contributor(val login:String, val avatar_url:String)
create data class for data model and add Serializable for data serialization key.
3. Create View Model and Provide it in the modul
class MainViewModel @Inject constructor(val api:Api) :ViewModel() {
public var state:MutableLiveData<State> = MutableLiveData()
public var data:MutableLiveData<Pair<List<Contributor>?, Throwable>> = MutableLiveData()
public fun getData(){
asyncRxExecutor(api.getContributor(), state, {
data.postValue(
Pair(it, Throwable("null"))
)
}, {
data.postValue(
Pair(null, it)
)
})
}
}
- Use high order function to wrap all request all request in
ViewModel
Extention
fun <T> ViewModel.asyncRxExecutor(heavyFunction: Observable<T>, state: MutableLiveData<State>, response : (response : T?) -> Unit, error: (error : Throwable) -> Unit) {
state.postValue(State.LOADING)
heavyFunction.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
response(it)
state.postValue(State.COMPLETE)
},{
state.postValue(State.COMPLETE)
error(it)
}
)
}
- There is 2 State for flag the data process
State.LOADING
andState.COMPLETE
- Use
Pair<DataSuccess,DataError>
and store data in MuatableLiveData for reactive update.
Build for easy and fast started kit for many purpose android application. The main concern is the developer just focus the the feature than the architecture flow.
Still add more improvement. If interest just fork and contribute here