Architecture Components

In Real Life
Season 2
@akexorcist

Lovely Android Developer @ Nextzy
Architecture

Component
• Lifecycles

• LiveData

• ViewModel

• Room
Architecture

Component
• Lifecycles

• LiveData

• ViewModel

• Room

• Navigation

• Paging

• ViewModel SavedState

• Data Binding
Dialog Event Listener
Use case
Activity/Fragment
DialogFragment
Normal
CreateEvent Listener
Activity/Fragment
DialogFragment
Activity destroyed then restored
CreateEvent Listener
Activity/Fragment DialogFragment
Handle dialog event with view model
ViewModel
Activity/Fragment DialogFragment
Handle dialog event with view model
ViewModel
Create
Activity/Fragment DialogFragment
Handle dialog event with view model
ViewModel
Event Listener Event Listener
Activity/Fragment DialogFragment
Handle dialog event with view model
Mapper
Event Listener Event Listener
ViewModel
LiveDataLiveData
DialogManager.showConfirm(
message = message,
key = KEY_DELETE_CONFIRMATION,
fragmentManager = supportFragmentManager
)
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?) {
}
})
MVVM + Clean Architecture
With Architecture Components
Reason
• Able to write the testable code 

• Suitable for write the code with others
Activity/Fragment
ViewModel
Repository
Database Network
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
Data Layer
Presentation Layer
Domain Layer
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
LiveDataLiveDataLiveData
LiveData
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
class ValidateUsernameUseCase : UseCase<String?, Boolean>() {
override fun execute(parameters: String?): Boolean {
return parameters?.isNotEmpty() ?: false
}
}
class ValidateUsernameUseCase : UseCase<String?, Boolean>() {
override fun execute(parameters: String?): Boolean {
return parameters?.isNotEmpty() ?: false
}
}
class ValidateUsernameUseCase : UseCase<String?, Boolean>() {
override fun execute(parameters: String?): Boolean {
return parameters?.isNotEmpty() ?: false
}
}
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)
}
}
}
class GetProfileUseCase constructor(
private val repository: ProfileRepository
) : MediatorUseCase<GetProfileRequest, String?>() {
override fun execute(parameters: GetProfileRequest) {
result.addSource(repository.getTermCondition(parameters)) { response ->
result.postValue(response)
}
}
}
class GetProfileUseCase constructor(
private val repository: ProfileRepository
) : MediatorUseCase<GetProfileRequest, String?>() {
override fun execute(parameters: GetProfileRequest) {
result.addSource(repository.getTermCondition(parameters)) { response ->
result.postValue(response)
}
}
}
class GetProfileUseCase constructor(
private val repository: ProfileRepository
) : MediatorUseCase<GetProfileRequest, String?>() {
override fun execute(parameters: GetProfileRequest) {
result.addSource(repository.getTermCondition(parameters)) { response ->
result.postValue(response)
}
}
}
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
LiveDataLiveDataLiveData
LiveData
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
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 = ""
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 = ""
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
Data Layer
Presentation Layer
Domain Layer
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
Developer A
Developer B
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
Developer A
Developer B
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
UseCaseUseCase
Developer A
Developer B
Activity/Fragment
ViewModel
UseCase
Database Network
Repository
Data Layer
Presentation Layer
Domain Layer
Navigation

Data Binding

ViewModel SavedState
Coroutine
Activity/Fragment
Paging
Data Layer
Presentation Layer
Domain Layer
Do the 

Proof of Concept 

every time when you learn
something new
Thank you!
@akexorcist

Lovely Android Developer @ Nextzy

Architecture Components In Real Life Season 2