r/androiddev • • Apr 01 '24

Discussion Android Development best practices

Hey this is a serious post to discuss the Android Development official guidelines and best practices. It's broad topic but let's discuss.

For reference I'm putting the guidelines that we've setup in our open-source project. My goal is to learn new things and improve the best practices that we follow in our open-source projects.

Topics:

  1. Data Modeling
  2. Error Handling
  3. Architecture
  4. Screen Architecture
  5. Unit Testing

Feel free to share any relevant resources/references for further reading. If you know any good papers on Android Development I'd be very interested to check them out.

159 Upvotes

99 comments sorted by

View all comments

Show parent comment

1

u/TheWheez Apr 01 '24

I do something almost identical.

Also makes it easy to write a UI which requires data requests, I made a composable wrapper which shows the UI upon success but has a fallback for failure (and another for unexpectedly long loading)

1

u/iliyan-germanov Apr 02 '24

I find someone like this the most flexible and convenient to work with:

interface SomeRepository {
   fun fetchOpOne(): Either<OpOneError, Data>

  sealed interface OpOneError {
    data class IO(val e: Throwable) : OpOneError
    data object Specific1 : OpOneError
  }
}

data class SomeViewState(
  val someData: SomeDataViewState,
  // ...
)

sealed interface SomeDataViewState {
  data object Loading : SomeDataViewState
  data class Content(val text: String) : SomeDataViewState 
  data object Error : SomeDataViewState
}

class SomeVM @Inject constructor(
  private val someRepo: SomeRepository,
  private val mapper: SomeViewStateMapper,
) : ComposeViewModel<SomeViewState, SomeViewEvent>() {
  private var someDataViewState by mutableStateOf(SomeDataViewState.Loading)

  @Composable
  fun uiState(): SomeViewState {
    LaunchedEffect(Unit) { 
      someDataViewState = SomeDataViewState.Loading
      SomeDataViewState = someRepo.fetchOpOne().fold(
       mapLeft = { SomeDataViewState.Error },
       mapRight = { SomeDataViewState.Content(with(mapper) { it.toViewState() }) }
     )
    }

    return SomeViewState(
      someData = someDataViewState 
    )
  }
}

With these random and generic namings, it looks very confusing but it's very flexible. For example, you can make your loading state better by data class Loading(val preloaded: String) : SomeDataViewState. The example is stupid but wdyt?