Presentation Layer

The presentation layer in Android is responsible for displaying data to the user and capturing user input.

In Android development, the presentation layer refers to the part of the architecture that handles the user interface (UI) and user experience (UX). This layer is responsible for displaying data to the user and capturing user interactions. It typically involves activities, fragments, view components, and view models. Here’s a more detailed breakdown:

ViewModels

The ViewModel class is a part of Android's architecture components and follows the MVVM (Model-View-ViewModel) design pattern. ViewModels store and manage UI-related data in a lifecycle-conscious way. They are responsible for preparing data for the UI and handling the business logic associated with user interactions. For example:

class MovieViewModel(
    private val movieUseCase: MovieUseCase,
) : ViewModel() {

    private val _detail = MutableLiveData<Resource<MovieDetail>>()
    val detail: LiveData<Resource<MovieDetail>> get() = _detail

    fun detail(id: Int) {
        viewModelScope.launch {
            movieUseCase.getDetailMovie(id).collectLatest {
                _detail.value = it
            }
        }
    }
}

And if you're using jetpack compose, you can use StateFlow. For example:

class MovieViewModel(
    private val movieUseCase: MovieUseCase,
) : ViewModel() {

    private val _detail = MutableStateFlow<Resource<MovieDetail>>()
    val detail: StateFlow<Resource<MovieDetail>> get() = _detail

    fun detail(id: Int) {
        viewModelScope.launch {
            movieUseCase.getDetailMovie(id).collectLatest {
                _detail.value = it
            }
        }
    }
}

This MovieViewModel class provides a way to retrieve movie details using the movieUseCase and expose them to the UI layer through the detail live data variable. The detail() function can be called to trigger the retrieval of movie details for a specific movie ID.

Activities/Fragments

Activities are the main building blocks of the presentation layer. They represent a single screen in the application and contain the UI elements that the user interacts with. Fragments are modular pieces of UI that can be reused across multiple activities. They are similar to activities, but they do not have their own lifecycle.

class MovieActivity : BaseActivity<ActivityMainBinding>() {

    private val movieViewModel: MovieViewModel by viewModel()

    override fun getViewBinding(): ActivityMainBinding =
        ActivityMainBinding.inflate(layoutInflater)

    override fun initIntent() {}

    override fun initUI() {}

    override fun initAction() {}

    override fun initProcess() {
        movieViewModel.detail(676)
    }

    override fun initObservers() {
        movieViewModel.detail.observerLiveData(this,
            onLoading = {
                // TODO(): Loading
            },
            onEmpty = {
                // TODO(): Empty
            },
            onFailure = { message ->
                // TODO(): Failure
            },
            onSuccess = {
                // TODO(): Success
            }
        )
    }

}

observerLiveData is onelib extension function for handling UI State.

Composable Function

A composable function is a key concept in Jetpack Compose, which is a modern toolkit for building native Android UI. Composable functions are annotated with @Composable and define UI components in a declarative way. They are used to describe the UI and its behavior in a straightforward, readable, and reusable manner.

@Composable
fun MovieScreen(
    viewModel: MovieViewModel = viewModel()
){
    LaunchedEffect(Unit) {
        viewModel.detail(666)
    }
    
    viewModel.result.collectStateFlow(
        onLoading = {
            // TODO(): Loading
        },
        onEmpty = {
            // TODO(): Empty
        },
        onFailure = { message ->
            // TODO(): Failure
        },
        onSuccess = {
            // TODO(): Success
        }
    )
}

collectStateFlow is onelib extension function for handling UI State for compose.

Last updated