Share
Embed
Android Architecture: From Medieval to Modern
The biggest decision you need to make at the beginning of each project is which architecture to use. This lays down the foundation on which the project will be built up. You want to build fast, test faster and be able to implement each change on the fly. Is that really possible? I will tell you how we do this at Infinum. The main point is that an architecture is always evolving so should you!
Ivan Maric
October 16, 2018
Transcript
95 slides
-
USAGE • Display UI • User interaction • Manipulate storage
• Network calls • Business logic • Track user location • Take picture • Play media -
USAGE • Display UI • User interaction • Manipulate storage
• Network calls • Business logic • Track user location • Take picture • Play media • World peace -
INFINUM ANDROID TEAM • 10+ members (25+ now) • 10+
ongoing projects • Maintenance • Skill level: Junior - Pro -
V P M Model Presenter View Activity 1 Fragment 1
CustomViews 1 Presenter 1 Presenter 1 Presenter 1 Interactor Repository Util/Manager Interface Interface -
END RESULT • Keep It Simple Stupid Views • Testable
Presenters • Maintainable project • Easy transition -
private void login() { loginViewModel.loginWithToken().observe(this, withObserver( loginUserResource -> startActivityAndFinish(DashboardActivity.buildIntent(this)), throwable
-> { ViewUtils.setVisibility(scanButton, View.VISIBLE); onError(throwable); }, this::onError )); }a -
private void login() { loginViewModel.loginWithToken().observe(this, withObserver( loginUserResource -> startActivityAndFinish(DashboardActivity.buildIntent(this)), throwable
-> { ViewUtils.setVisibility(scanButton, View.VISIBLE); onError(throwable); }, this::onError )); }a -
private void login() { loginViewModel.loginWithToken().observe(this, withObserver( loginUserResource -> startActivityAndFinish(DashboardActivity.buildIntent(this)), throwable
-> { ViewUtils.setVisibility(scanButton, View.VISIBLE); onError(throwable); }, this::onError )); }a -
private void login() { loginViewModel.loginWithToken().observe(this, withObserver( loginUserResource -> startActivityAndFinish(DashboardActivity.buildIntent(this)), throwable
-> { ViewUtils.setVisibility(scanButton, View.VISIBLE); onError(throwable); }, this::onError )); }a -
private void checkForUpdates() { versionViewModel.checkForUpdates().observe(this, result -> { if (result
instanceof NewUpdate) { showUpdateDialog(((NewUpdate) result).url); } else if (result instanceof NoUpdate) { login(); } else if (result instanceof Error) { showErrorDialog(); } else { Timber.wtf(new IllegalArgumentException("Impossible case")); }a }); }b -
private void checkForUpdates() { versionViewModel.checkForUpdates().observe(this, result -> { if (result
instanceof NewUpdate) { showUpdateDialog(((NewUpdate) result).url); } else if (result instanceof NoUpdate) { login(); } else if (result instanceof Error) { showErrorDialog(); } else { Timber.wtf(new IllegalArgumentException("Impossible case")); }a }); }b -
private void checkForUpdates() { versionViewModel.checkForUpdates().observe(this, result -> { if (result
instanceof NewUpdate) { showUpdateDialog(((NewUpdate) result).url); } else if (result instanceof NoUpdate) { login(); } else if (result instanceof Error) { showErrorDialog(); } else { Timber.wtf(new IllegalArgumentException("Impossible case")); }a }); }b -
private void checkForUpdates() { versionViewModel.checkForUpdates().observe(this, result -> { if (result
instanceof NewUpdate) { showUpdateDialog(((NewUpdate) result).url); } else if (result instanceof NoUpdate) { login(); } else if (result instanceof Error) { showErrorDialog(); } else { Timber.wtf(new IllegalArgumentException("Impossible case")); }a }); }b -
private void checkForUpdates() { versionViewModel.checkForUpdates().observe(this, result -> { if (result
instanceof NewUpdate) { showUpdateDialog(((NewUpdate) result).url); } else if (result instanceof NoUpdate) { login(); } else if (result instanceof Error) { showErrorDialog(); } else { Timber.wtf(new IllegalArgumentException("Impossible case")); }a }); }b -
MODEL VIEW INTENT • http://hannesdorfmann.com/android/mosby3-mvi-1 • Solving basic problems •
Unidirectional flow • Define the real model -
viewModel.viewStateRender().observe(this, Observer { when (it) { is TwoFaNeeded -> displayCodeInput()
is WrongCredentials -> showCredentialError() is LoginSuccess -> startActivity<DashboardActivity>() is OpenForgotPassword -> startActivity<ForgotPasswordFlowActivity>() }a }) VIEW -
VIEW viewModel.viewStateRender().observe(this, Observer { when (it) { is TwoFaNeeded ->
displayCodeInput() is WrongCredentials -> showCredentialError() is LoginSuccess -> startActivity<DashboardActivity>() is OpenForgotPassword -> startActivity<ForgotPasswordFlowActivity>() }a }) -
viewModel.viewStateRender().observe(this, Observer { when (it) { is TwoFaNeeded -> displayCodeInput()
is WrongCredentials -> showCredentialError() is LoginSuccess -> startActivity<DashboardActivity>() is OpenForgotPassword -> startActivity<ForgotPasswordFlowActivity>() }a }) VIEW -
viewModel.viewStateRender().observe(this, Observer { when (it) { is TwoFaNeeded -> displayCodeInput()
is WrongCredentials -> showCredentialError() is LoginSuccess -> startActivity<DashboardActivity>() is OpenForgotPassword -> startActivity<ForgotPasswordFlowActivity>() }a }) VIEW -
viewModel.viewStateRender().observe(this, Observer { when (it) { is TwoFaNeeded -> displayCodeInput()
is WrongCredentials -> showCredentialError() is LoginSuccess -> startActivity<DashboardActivity>() is OpenForgotPassword -> startActivity<ForgotPasswordFlowActivity>() }a }) VIEW -
fun action(action: LoginAction) { when (action) { is CheckCredentials ->
verifyCredentials(action.username, action.password) is ForgotPassword -> viewStateLiveData.postValue(OpenForgotPassword()) }a }b ACTION -
fun action(action: LoginAction) { when (action) { is CheckCredentials ->
verifyCredentials(action.username, action.password) is ForgotPassword -> viewStateLiveData.postValue(OpenForgotPassword()) }a }b ACTION -
fun action(action: LoginAction) { when (action) { is CheckCredentials ->
verifyCredentials(action.username, action.password) is ForgotPassword -> viewStateLiveData.postValue(OpenForgotPassword()) }a }b ACTION -
fun action(action: LoginAction) { when (action) { is CheckCredentials ->
verifyCredentials(action.username, action.password) is ForgotPassword -> viewStateLiveData.postValue(OpenForgotPassword()) }a }b ACTION -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onSuccess(response: AuthResponse) { viewStateLiveData.postValue(LoginSuccess()) }a override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onSuccess(response: AuthResponse) { viewStateLiveData.postValue(LoginSuccess()) }a }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onSuccess(response: AuthResponse) { viewStateLiveData.postValue(LoginSuccess()) }a }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onSuccess(response: AuthResponse) { viewStateLiveData.postValue(LoginSuccess()) }aA }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onSuccess(response: AuthResponse) { viewStateLiveData.postValue(LoginSuccess()) }aA }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
private fun verifyCredentials(username: String, password: String) { authInteractor.execute(LoginData(user = username,
password = password)) .subscribe(object : ErrorHandlingSingleObserver<AuthResponse>() { override fun onErrorResponse(error: ErrorResponse) { when (error.errorCode) { ErrorCode.UserDisabled -> commonStateLiveData.value = ErrorMessage(R.string.error_user_disabled) ErrorCode.IncorrectCredentials -> viewStateLiveData.postValue(WrongCredentials()) }b }c }) }d -
sealed class LoadingState sealed class ErrorState data class CommonState(a val
loadingState: LoadingState, val errorState: ErrorState )b open class ViewState<State>(c var viewState: State? = null, var commonState: CommonState = IDLE_STATE )d -
sealed class LoadingState sealed class ErrorState data class CommonState(a val
loadingState: LoadingState, val errorState: ErrorState )b open class ViewState<State>(c var viewState: State? = null, var commonState: CommonState = IDLE_STATE )d -
sealed class LoadingState sealed class ErrorState data class CommonState(a val
loadingState: LoadingState, val errorState: ErrorState )b open class ViewState<State>(c var viewState: State? = null, var commonState: CommonState = IDLE_STATE )d -
sealed class LoadingState sealed class ErrorState data class CommonState(a val
loadingState: LoadingState, val errorState: ErrorState )b open class ViewState<State>(c var viewState: State? = null, var commonState: CommonState = IDLE_STATE )d -
sealed class LoadingState sealed class ErrorState data class CommonState(a val
loadingState: LoadingState, val errorState: ErrorState )b open class ViewState<State>(c var viewState: State? = null, var commonState: CommonState = IDLE_STATE )d -
protected var viewState: State? = null get() = mutableLiveData.value?.viewState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(viewState = value)) } else { mutableLiveData.value?.viewState = value mutableLiveData.post() }a }b -
protected var viewState: State? = null get() = mutableLiveData.value?.viewState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(viewState = value)) } else { mutableLiveData.value?.viewState = value mutableLiveData.post() }a }b -
protected var viewState: State? = null get() = mutableLiveData.value?.viewState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(viewState = value)) } else { mutableLiveData.value?.viewState = value mutableLiveData.post() }a }b -
protected var commonState: CommonState = IDLE_STATE get() = mutableLiveData.value?.commonState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(commonState = value)) } else { mutableLiveData.value?.commonState = value mutableLiveData.post() } } -
protected var commonState: CommonState = IDLE_STATE get() = mutableLiveData.value?.commonState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(commonState = value)) } else { mutableLiveData.value?.commonState = value mutableLiveData.post() } } -
protected var commonState: CommonState = IDLE_STATE get() = mutableLiveData.value?.commonState ?:
field set(value) { val viewState = mutableLiveData.value if (viewState == null) { mutableLiveData.postValue(ViewState(commonState = value)) } else { mutableLiveData.value?.commonState = value mutableLiveData.post() } } -
fun <State> LiveData<ViewState<State>>.render(baseActivity: BaseActivity, action: (State) -> Unit) { observe(baseActivity,
ViewModelObserver(baseActivity, action)) } class ViewModelObserver<T>(val baseActivity: RenderComponent, val action: (T) -> Unit) : Observer<ViewState<T>> { private var lastState: T? = null private var lastCommonState: CommonState? = null override fun onChanged(t: ViewState<T>?) { if (t == null) { Timber.e("LiveData.value is [NULL]") return } val state = t.viewState if (t.commonState != lastCommonState) { baseActivity.handleBaseState(t.commonState) lastCommonState = t.commonState } if (state != null && state != lastState) { lastState = state action(state) } } } -
fun <State> LiveData<ViewState<State>>.render(baseActivity: BaseActivity, action: (State) -> Unit) { observe(baseActivity,
ViewModelObserver(baseActivity, action)) } class ViewModelObserver<T>(val baseActivity: RenderComponent, val action: (T) -> Unit) : Observer<ViewState<T>> { private var lastState: T? = null private var lastCommonState: CommonState? = null override fun onChanged(t: ViewState<T>?) { if (t == null) { Timber.e("LiveData.value is [NULL]") return } val state = t.viewState if (t.commonState != lastCommonState) { baseActivity.handleBaseState(t.commonState) lastCommonState = t.commonState } if (state != null && state != lastState) { lastState = state action(state) } } } -
fun <State> LiveData<ViewState<State>>.render(baseActivity: BaseActivity, action: (State) -> Unit) { observe(baseActivity,
ViewModelObserver(baseActivity, action)) } class ViewModelObserver<T>(val baseActivity: RenderComponent, val action: (T) -> Unit) : Observer<ViewState<T>> { private var lastState: T? = null private var lastCommonState: CommonState? = null override fun onChanged(t: ViewState<T>?) { if (t == null) { Timber.e("LiveData.value is [NULL]") return } val state = t.viewState if (t.commonState != lastCommonState) { baseActivity.handleBaseState(t.commonState) lastCommonState = t.commonState } if (state != null && state != lastState) { lastState = state action(state) } } } -
fun <State> LiveData<ViewState<State>>.render(baseActivity: BaseActivity, action: (State) -> Unit) { observe(baseActivity,
ViewModelObserver(baseActivity, action)) } class ViewModelObserver<T>(val baseActivity: RenderComponent, val action: (T) -> Unit) : Observer<ViewState<T>> { private var lastState: T? = null private var lastCommonState: CommonState? = null override fun onChanged(t: ViewState<T>?) { if (t == null) { Timber.e("LiveData.value is [NULL]") return } val state = t.viewState if (t.commonState != lastCommonState) { baseActivity.handleBaseState(t.commonState) lastCommonState = t.commonState } if (state != null && state != lastState) { lastState = state action(state) } } } -
MVP VS MVVM interface LoginView : BaseMvp.View { fun updatesFound(String
version) fun loginSuccess() } sealed class LoginState class LoginUpdatesFound(val version: String) : LoginState() class LoginSuccess : LoginState() -
MVP VS MVVM class BaseMvp { interface View { fun
showProgress() fun hideProgress() fun showError(title: String, message: String) } } sealed class CommonState class CommonLoading() : CommonState() class CommonIdle : CommonState() class CommonError(val title: String, val message: String) : CommonState() -
Presenter View ViewModel View MVP VS MVVM REFERENCE AND DIRECT
CALLS LIVE DATA AND OBSERVER -
LIFECYCLE • Live data lifecycle aware • observe(lifecycleOwner, Observer{ })
• Right time notifications -
NETWORK CALL • Cancel callback • State changed without notification
• Latest known state rendered -
ORIENTATION CHANGE • ViewModel is not destroyed • State preserved
without SaveInstance -
ORIENTATION CHANGE • ViewModel is not destroyed • State preserved
without SaveInstance • Latest known state rendered -
FRAGMENT COMMUNICATION ACTIVITY FRAGMENT FRAGMENT FRAGMENT FRAGMENT Ivan ivan.maric@infinum.hr Infinum
{ "name":"Ivan", "mail":"[email protected]", "company":"Infinum" } -
Visit infinum.co or find us on social networks: infinum.co infinumco
infinumco infinum Thank you! [email protected] @MARIC_IV