Skip to content

KMM shared module for SpaceX mobile app (Quanti recruitment process).

License

Notifications You must be signed in to change notification settings

Qase/mobile-assignment-kmm

Repository files navigation

mobile-assignment-kmm

fetchRockets module for solution application.

Simple library in Kotlin Multiplatform Mobile that provides REST API requests.

Those are used in our example app in Quanti mobile-assignment.

KMM is used for both iOS (Swift) and Android (Kotlin) versions of mobile development. It is a modern way of creating shared code for apps. This project offers use cases of RocketClient. Such use cases do the work of creating API requests, serializing (parsing) data from json to struct/class and then providing the data to platform-specific projects.

API requests

 fetchAllRockets(): RocketResult<List<RocketKMM>>
 fetchRocketById(rocketId: String): RocketResutl<RocketKMM>
 fetchFailRockets: RocketResult<RocketException> // Made for testing error handling

RocketKMM is basically a DTO model for rocket API - https://api.spacexdata.com/v4/rockets/.

  • Additional information is available in the SpaceX API.

RocketResult is custom result type (Success and Failure) used because Swift cannot handle built-in Result type and casts it as a Any?.

NativeCoroutines

  • All functions are using a @NativeCoroutines modifier provided via special library: KMP-Native-Coroutines.
  • The library basically creates "new" functions, that are thread-safe. Those functions are called differently.

How to use it in Swift

Calling basic functions in Swift is very easy, just declare the struct and then use the functions like so:

 let rocketApi = RocketApi()
 let rockets = try await rocketApi.fetchAllRockets()

and handle errors in do-catch clause.

Functions in NativeCoroutines are handled this way:

 let rockets = try await asyncFunction(for: rocketApi.fetchAllRockets())
  • Note that the asyncFunction is from the NativeCoroutines library that needs to be imported.

Full implementation in Swift

 do {
      let rockets = try await asyncFunction(for: rocketApi.fetchAllRockets())
      //MARK: Even though warning is saying "always fails" it in fact does not fail at all. Swift is confused about KMM. - Ignore this warrning
      switch rockets {
      case let success as RocketResultSuccess<AnyObject>:
         //Custom mapping into domain model
      case let failure as RocketResult<RocketException>:
         //Custoom error mapping to domain error
      default:
        throw DomainError.undefinedError
      }
 } catch {
   throw error
 }

About

KMM shared module for SpaceX mobile app (Quanti recruitment process).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published