close
close

Examples of SOLID com Jetpack Compose – parte S

A new series of art articles has appeared somehow, a principle for SOLID. You can link to use another version of this article while using any of the following articles, not a definitive destination when it comes to actual facts.

There are topics in development that are of great importance, and SOLID is one of them. There are situations where there is no single observation, with a view to the first place, that can implement some of our principles and that can be an example of an implementation that does not apply to an entrepreneur in business.

In this series of articles we will use the ideas of SOLID with Jetpack Compose ever with this technology that performs much better than our current ideas.

The important thing is that this is the case S – One Principle of Responsibility (SRP) or Princípio da Responsabilidade ÚnicaThe fact is that a class (not a single case, a composable) has a unique responsibility, or you can create a unique situation or mud.

In this example you can make an analysis of such a case HomeScreen:

Image description

And no HomeScreenThe designer has developed all his fluxes to ensure that they interact with their use. First of all, it is important that you monitor your conversations; In this case, if the care arrangement is implemented, there is a list of conteúdos. The designer can no longer use or no longer maintain a number of elements in his or her way of working.

Image description

It is a development as comum XMLThe question is whether some elements appear in the tela and ocultados. During its implementation activitiesdecide when it develops. For example, if you see a list of posts, or “new” being developed, this is a loose format; if there is no more conteúdo, it is a great part of the retention; If the problem occurs, there is nothing to worry about.

In this case, you may be doing a simple implementation, but it may be that one of the many commonalities and exceptional cases may cause a problem with certain conditions, which may be complicated, so that there is no more code, a new kind of unacceptable way to correct.

Image description

Implementando o Single-responsibility principle (SRP)

Not a single example, it’s about sharing the contents of the components, a series of functions being defined or the flow being displayed on the screen. Seeing the basics of Jetpack Compose, you can create a view with a simple functionality and anotação @Composableand there is nothing wrong with this kind of thing.

Vamos defines your responsibility by:

  • Loading: carregando conteúdo
  • Content: have a list of conteúdos
  • Empty: foi carregado uma lista vazia
  • Error: love um erro no carregamento

A simple solution for troubleshooting the following components:

@Composable
fun HomeLoadingScreen(){...}

@Composable
fun HomeContentScreen(
   contentList: ListString>
   onNewClick: ()-> Unit
){...}

@Composable
fun HomeEmptyScreen(
   onNewClick: ()-> Unit
){...}

@Composable
fun HomeErrorScreen(
   onOkClick: ()-> Unit
){...}
Go to full screen mode

Exit full screen mode

FIM Often, when we use the SRP, this becomes clear, because we are aware of the separation of responsibility, ultimately, it may no longer be so. First of all, you should know that it has a function HomeScreen The fact is that the company’s functions are no longer profitable. It depends on a country situation. Alem da HomeScreentemos a HomeViewModel you can’t vary anything else MutableLiveData you can do this the same way you use the app.

O tipo do nosso MutableLiveData there is a class of selada HomeUIState those classes and objects represent their responsibility (or accountability).

@Composable
fun HomeScreen(viewModel: HomeViewModel){
   val state = viewModel.uiState.colectAsState()
}

class HomeViewModel(): ViewModel() {
   var uiState: MutableLiveDataHomeUIState>()

...


   sealed class HomeUIState() {
      object Loading: HomeUIState()
      data class Content (val contentList: ListString>): HomeUIState()
      object Empty: HomeUIState()
      object Error: HomeUIState()
   }
}
Go to full screen mode

Exit full screen mode

Now we can hear more and more people who haven’t done this uiState (O MutableLiveData). It is a position that represents moments as your career APIyou are a coleta no sharedPreferences.

class HomeViewModel(): ViewModel() {
   private fun onInit() {
      uiState.postValue(Loading)
   }

   private fun onGetListSuccess(list: ListString>) {
      //verify if is empty
      if(list.isEmpty()){
         uiState.postValue(Empty)
      } else {
         uiState.postValue(Content(list))
      }
   }

   private fun onGetListFailure() {
      uiState.postValue(Error)
   }
}
Go to full screen mode

Exit full screen mode

Por fim, like telas em HomeScreen it displays an image of a um When:

@Composable
fun HomeScreen(viewModel: HomeViewModel){
   val state = viewModel.uiState.colectAsState()

   when(state) {
      is Loading -> HomeLoadingScreen()
      is Content -> HomeContentScreen(it.contentList){ viewModel.createNewContent()}
      is Empty -> HomeEmptyScreen{ viewModel.createNewContent()}
      is Error -> HomeErrorStreen{ viewModel.loadContents() }
   }
}
Go to full screen mode

Exit full screen mode

In short, it is a unique responsibility for:

  • HomeLoadingScreen: Show our corresponding components when you follow your conversation.
  • HomeContentScreen: view our correspondent components as they appear on a list of conteúdos.
  • HomeEmptyScreen: View our corresponding components when creating a list of vazias.
  • HomeErrorScreen: Please check our corresponding components if there are any errors.
  • HomeScreen: receive our receipts en escort or que exibir.

No article can take into account our other principles STURDY usando Jetpack Composing. You can list the following items:

  • Examples of SOLID com Jetpack Compose – parte S
  • Examples of SOLID com Jetpack Compose – parte O
  • Examples of SOLID com Jetpack Compose – parte L
  • Examples of SOLID com Jetpack Compose – Part I
  • Examples of SOLID com Jetpack Compose – parte D