Minimal Kotlin Multiplatform project with SwiftUI, Jetpack Compose, Wear Compose, Compose for Desktop, Compose for Web and Kotlin/JS + React clients along with Ktor backend.
Find a file
2020-07-21 12:11:41 +02:00
.github/workflows trigger github actions build for PRs as well 2020-07-05 13:16:35 +01:00
app update to Compose dev14 and Coil 0.1.6 2020-06-25 22:21:28 +01:00
backend backend module update 2020-05-16 22:21:45 +01:00
buildSrc Add FloorPlan plugin for ER diagram generation 2020-07-05 13:38:42 +02:00
common comment out floor plan changes until build issue is resolved 2020-07-05 13:17:47 +01:00
gradle/wrapper Update gradle to 6.5.1 2020-07-21 12:11:41 +02:00
ios/PeopleInSpaceSwiftUI add koin to multiplatform shared code 2020-05-15 17:57:24 +01:00
macOS/PeopleInSpace add koin to multiplatform shared code 2020-05-15 17:57:24 +01:00
watchos/PeopleInSpaceWatch add koin to multiplatform shared code 2020-05-15 17:57:24 +01:00
web kotlin/js updates including use of koin 2020-05-28 21:14:56 +01:00
.gitignore iniital commit 2019-12-22 20:25:01 +00:00
build.gradle.kts comment out floor plan changes until build issue is resolved 2020-07-05 13:17:47 +01:00
gradle.properties iniital commit 2019-12-22 20:25:01 +00:00
gradlew Update gradle to 6.5.1 2020-07-21 12:11:41 +02:00
gradlew.bat Update gradle to 6.5.1 2020-07-21 12:11:41 +02:00
README.md Fix typo on the title of Kotlin Coroutines 2020-06-29 05:58:17 +03:00
settings.gradle.kts initial cut of backend/web modules 2020-04-26 19:29:28 +01:00

PeopleInSpace

Minimal Kotlin Multiplatform project using Jetpack Compose and SwiftUI. Currently running on

  • Android
  • iOS
  • watchOS
  • macOS
  • Web

It makes use of basic API (http://open-notify.org/Open-Notify-API/People-In-Space/) to show list of people currently in space (inspired by https://kousenit.org/2019/12/19/a-few-astronomical-examples-in-kotlin/)! The list is shown on Android using Jetpack Compose and on iOS using SwiftUI

Related posts:

Note that this repository very much errs on the side of mimimalism to help more clearly illustrate key moving parts of a Koltin Multiplatform project and also to hopefully help someone just starting to explore KMP to get up and running for first time (and is of course primarily focussed on use of Jetpack Compose and SwiftUI). If you're at stage of moving beyond this then I'd definitely recommend checking out KaMPKit

Note: You need to use Android Studio v4.2 (currently preview/alpha version). Have tested on XCode v11.3. When opening iOS/watchOS/macOS projects remember to open .xcworkspace file (and not .xcodeproj one)

Update Jan 14th 2020: This now also includes WatchOS version thanks to Neal Sanche

The following is pretty much all the code used (along with gradle files/resources etc). I did say it was minimal!!

Update Jan 25th 2020: Have added SQLDelight support for locally persisting data (across all the platforms). I haven't updated code below yet as I think it still has value in demonstrating what a minimum Kotlin Multiplatform project would be.

Update April 15th 2020: Added macOS support

Update May 2nd 2020: Added basic Kotlin/JS support

Update May 15th 2020: Using Koin in shared multiplatform code

iOS SwiftUI Code

struct ContentView: View {
    @ObservedObject var peopleInSpaceViewModel = PeopleInSpaceViewModel(repository: PeopleInSpaceRepository())

    var body: some View {
        NavigationView {
            List(peopleInSpaceViewModel.people, id: \.name) { person in
                PersonView(person: person)
            }
            .navigationBarTitle(Text("PeopleInSpace"), displayMode: .large)
            .onAppear(perform: {
                self.peopleInSpaceViewModel.fetch()
            })
        }
    }
}

struct PersonView : View {
    var person: Assignment

    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text(person.name).font(.headline)
                Text(person.craft).font(.subheadline)
            }
        }
    }

WatchOS SwiftUI Code

struct ContentView: View {
    @ObservedObject var peopleInSpaceViewModel = PeopleInSpaceViewModel(repository: PeopleInSpaceRepository())
    
    var body: some View {
        VStack {
            List(peopleInSpaceViewModel.people, id: \.name) { person in
                PersonView(person: person)
            }
            .onAppear(perform: {
                self.peopleInSpaceViewModel.fetch()
            })
        }
    }
}

struct PersonView : View {
    var person: Assignment
    
    var body: some View {
        NavigationLink(person.name, destination: Text(person.craft).font(.subheadline))
    }
}

iOS Swift ViewModel

class PeopleInSpaceViewModel: ObservableObject {
    @Published var people = [Assignment]()

    private let repository: PeopleInSpaceRepository
    init(repository: PeopleInSpaceRepository) {
        self.repository = repository
    }
    
    func fetch() {
        repository.fetchPeople(success: { data in
            self.people = data
        })
    }
}

Android Jetpack Compose code

class MainActivity : AppCompatActivity() {
    private val peopleInSpaceViewModel: PeopleInSpaceViewModel by viewModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            val peopleState = peopleInSpaceViewModel.peopleInSpace.observeAsState()
            mainLayout(peopleState)
        }
    }
}

@Composable
fun mainLayout(peopleState: State<List<Assignment>?>) {
    MaterialTheme {
        Column {
            TopAppBar(
                title = {
                    Text("People In Space")
                }
            )
            AdapterList(data = peopleState.value!!) { person ->
                Row(person)
            }

        }
    }
}


@Composable
fun Row(person: Assignment) {
    Text(
        text = "${person.name} (${person.craft})",
        modifier = Modifier.padding(16.dp)
    )
}

Android Kotlin ViewModel

class PeopleInSpaceViewModel(peopleInSpaceRepository: PeopleInSpaceRepository) : ViewModel() {
    val peopleInSpace = MutableLiveData<List<Assignment>>(emptyList())

    init {
        viewModelScope.launch {
            val people = peopleInSpaceRepository.fetchPeople()
            peopleInSpace.value = people
        }
    }
}

Kotlin/JS client

val App = functionalComponent<RProps> { _ ->
    val scope = MainScope()
    val api = PeopleInSpaceApi()

    val (people, setPeople) = useState(emptyList<Assignment>())

    useEffect(dependencies = listOf()) {
        scope.launch {
            setPeople(api.fetchPeople().people)
        }
    }

    h1 {
        +"People In Space"
    }
    ul {
        people.forEach { item ->
            li {
                +"${item.name} (${item.craft})"
            }
        }
    }
}

Shared Kotlin Repository

class PeopleInSpaceRepository {
    private val peopleInSpaceApi = PeopleInSpaceApi()

    suspend fun fetchPeople() : List<Assignment> {
        val result = peopleInSpaceApi.fetchPeople()
        return result.people
    }


    fun fetchPeople(success: (List<Assignment>) -> Unit) {
        GlobalScope.launch(Dispatchers.Main) {
            success(fetchPeople())
        }
    }
}

Shared Kotlin API Client Code (using Ktor and Kotlinx Serialization library)

@Serializable
data class AstroResult(val message: String, val number: Int, val people: List<Assignment>)

@Serializable
data class Assignment(val craft: String, val name: String)

class PeopleInSpaceApi {
    private val baseUrl = "http://api.open-notify.org"

    private val client by lazy {
        HttpClient() {
            install(JsonFeature) {
                serializer = KotlinxSerializer(Json(JsonConfiguration(strictMode = false)))
            }
        }
    }

    suspend fun fetchPeople() = client.get<AstroResult>("$baseUrl/astros.json")
}

Languages, libraries and tools used