Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Architecture Components In Real Life Season 2

1,762 views

Published on

@ Mobile Conf 2019 - 24 August 2019

Published in: Technology
  • Login to see the comments

Architecture Components In Real Life Season 2

  1. 1. Architecture Components In Real Life Season 2 @akexorcist Lovely Android Developer @ Nextzy
  2. 2. Architecture Component • Lifecycles • LiveData • ViewModel • Room
  3. 3. Architecture Component • Lifecycles • LiveData • ViewModel • Room • Navigation • Paging • ViewModel SavedState • Data Binding
  4. 4. Dialog Event Listener Use case
  5. 5. Activity/Fragment DialogFragment Normal CreateEvent Listener
  6. 6. Activity/Fragment DialogFragment Activity destroyed then restored CreateEvent Listener
  7. 7. Activity/Fragment DialogFragment Handle dialog event with view model ViewModel
  8. 8. Activity/Fragment DialogFragment Handle dialog event with view model ViewModel Create
  9. 9. Activity/Fragment DialogFragment Handle dialog event with view model ViewModel Event Listener Event Listener
  10. 10. Activity/Fragment DialogFragment Handle dialog event with view model Mapper Event Listener Event Listener ViewModel LiveDataLiveData
  11. 11. DialogManager.showConfirm( message = message, key = KEY_DELETE_CONFIRMATION, fragmentManager = supportFragmentManager )
  12. 12. dialogViewModel.confirmation.observe(this, object : ConfirmDialogListener { override fun onConfirmButtonClick(key: String?, data: Bundle?) { when (key) { KEY_DELETE_CONFIRMATION -> deleteTask(true) } } override fun onCancelButtonClick(key: String?, data: Bundle?) { } })
  13. 13. MVVM + Clean Architecture With Architecture Components
  14. 14. Reason • Able to write the testable code 
 • Suitable for write the code with others
  15. 15. Activity/Fragment ViewModel Repository Database Network
  16. 16. Activity/Fragment ViewModel UseCase Database Network Repository
  17. 17. Data Layer Presentation Layer Domain Layer Activity/Fragment ViewModel UseCase Database Network Repository
  18. 18. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase
  19. 19. LiveDataLiveDataLiveData LiveData Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase
  20. 20. class ValidateUsernameUseCase : UseCase<String?, Boolean>() { override fun execute(parameters: String?): Boolean { return parameters?.isNotEmpty() ?: false } }
  21. 21. class ValidateUsernameUseCase : UseCase<String?, Boolean>() { override fun execute(parameters: String?): Boolean { return parameters?.isNotEmpty() ?: false } }
  22. 22. class ValidateUsernameUseCase : UseCase<String?, Boolean>() { override fun execute(parameters: String?): Boolean { return parameters?.isNotEmpty() ?: false } }
  23. 23. class ValidateUsernameUseCase : UseCase<String?, Boolean>() { override fun execute(parameters: String?): Boolean { return parameters?.isNotEmpty() ?: false } }
  24. 24. class GetProfileUseCase constructor( private val repository: ProfileRepository ) : MediatorUseCase<GetProfileRequest, String?>() { override fun execute(parameters: GetProfileRequest) { result.addSource(repository.getTermCondition(parameters)) { response -> result.postValue(response) } } }
  25. 25. class GetProfileUseCase constructor( private val repository: ProfileRepository ) : MediatorUseCase<GetProfileRequest, String?>() { override fun execute(parameters: GetProfileRequest) { result.addSource(repository.getTermCondition(parameters)) { response -> result.postValue(response) } } }
  26. 26. class GetProfileUseCase constructor( private val repository: ProfileRepository ) : MediatorUseCase<GetProfileRequest, String?>() { override fun execute(parameters: GetProfileRequest) { result.addSource(repository.getTermCondition(parameters)) { response -> result.postValue(response) } } }
  27. 27. class GetProfileUseCase constructor( private val repository: ProfileRepository ) : MediatorUseCase<GetProfileRequest, String?>() { override fun execute(parameters: GetProfileRequest) { result.addSource(repository.getTermCondition(parameters)) { response -> result.postValue(response) } } }
  28. 28. class ValidateUsernameUseCase : UseCase<String?, Boolean>() { override fun execute(parameters: String?): Boolean { return parameters?.isNotEmpty() ?: false } } class GetProfileUseCase constructor( private val repository: ProfileRepository ) : MediatorUseCase<GetProfileRequest, String?>() { override fun execute(parameters: GetProfileRequest) { result.addSource(repository.getTermCondition(parameters)) { response -> result.postValue(response) } } } Synchronous Logic Asynchronous Logic
  29. 29. LiveDataLiveDataLiveData LiveData Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase
  30. 30. class ValidateUsernameUseCaseTest { @Before fun setup() { MockitoAnnotations.initMocks(this) } @Test fun `Validate the username with normally username - should be true`() { val useCase = ValidateUsernameUseCase() val username = "Akexorcist" val result = useCase.executeNow(username) Assert.assertTrue(result is Result.Success) Assert.assertTrue((result as Result.Success).data ?: false) } @Test fun `Validate the username with empty username - should be false`() { val useCase = ValidateUsernameUseCase() val username = ""
  31. 31. class ValidateUsernameUseCaseTest { @Before fun setup() { MockitoAnnotations.initMocks(this) } @Test fun `Validate the username with normally username - should be true`() { val useCase = ValidateUsernameUseCase() val username = "Akexorcist" val result = useCase.executeNow(username) Assert.assertTrue(result is Result.Success) Assert.assertTrue((result as Result.Success).data ?: false) } @Test fun `Validate the username with empty username - should be false`() { val useCase = ValidateUsernameUseCase() val username = ""
  32. 32. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase
  33. 33. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase Data Layer Presentation Layer Domain Layer
  34. 34. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase Developer A Developer B
  35. 35. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase Developer A Developer B
  36. 36. Activity/Fragment ViewModel UseCase Database Network Repository UseCaseUseCase Developer A Developer B
  37. 37. Activity/Fragment ViewModel UseCase Database Network Repository Data Layer Presentation Layer Domain Layer Navigation Data Binding ViewModel SavedState Coroutine
  38. 38. Activity/Fragment Paging Data Layer Presentation Layer Domain Layer
  39. 39. Do the Proof of Concept every time when you learn something new
  40. 40. Thank you! @akexorcist Lovely Android Developer @ Nextzy

×