Create Your First Fully Cross-Platform Mobile App With Compose Multiplatform 3/4 — Domain Layer And ViewModels

Build a cross-platform iOS and Android app using the Jetpack Compose API with a single codebase

Yanneck Reiß

--

In this article series, we build a fully cross-platform mobile app with Kotlin Multiplatform (KMP) in combination with Compose Multiplatform for iOS.

In the previous part, we saw how to set up dependency injection and our database. In this part, we go one step further to the domain layer and set up our use cases, proceed to the UI or application layer, and set up our view models that will later connect our Compose Multiplatform UI code with the business logic.

Below, you can find the table of contents for this article series.

  1. Introduction
  2. Dependency Injection and Database
  3. Use Cases and view models (you are here)
  4. Navigation system and Compose Multiplatform UI code

1 Creating Our Use Cases

As discussed in the previous articles, we have two cases for business logic. One is for creating a new note on the “Create Note” screen, and one is for observing our data in the database in the notes overview.

In the code below, you can see the implementation for the observation of the notes table content:

import com.myapplication.Note
import data.db.NotesRepository
import kotlinx.coroutines.flow.Flow

class WatchNotesUseCase(
private val notesRepository: NotesRepository
) {

fun call(): Flow<List<Note>> = notesRepository.watchNotes()
}

As you can see, the use case does not need more than receiving the NotesRepository as an input parameter and forwarding the watchNotes() function.

Next, we take a look at the insert case:

import data.db.NotesRepository

class CreateNoteUseCase(
private val notesRepository: NotesRepository
) {

suspend fun call(content: String) {
notesRepository.insertNote(content = content)
}
}

--

--

Yanneck Reiß

Follow me on my journey as a professional mobile and fullstack developer