You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: topics/multiplatform-onboard/multiplatform-upgrade-app.md
+34-50Lines changed: 34 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,16 +33,15 @@ and display the date of the last successful launch of a SpaceX rocket.
33
33
34
34
You'll need to add the following multiplatform libraries in your project:
35
35
36
-
*[`kotlinx.coroutines`](https://github.com/Kotlin/kotlinx.coroutines), to use coroutines for asynchronous code,
37
-
which allows simultaneous operations.
38
-
*[`kotlinx.serialization`](https://github.com/Kotlin/kotlinx.serialization), to deserialize JSON responses into objects of entity classes used to process
36
+
*[`kotlinx.coroutines`](https://github.com/Kotlin/kotlinx.coroutines), to use coroutines for simultaneous operations.
37
+
*[`kotlinx.serialization`](https://github.com/Kotlin/kotlinx.serialization), to deserialize JSON responses of the SpaceX API into objects of entity classes used to process
39
38
network operations.
40
-
*[Ktor](https://ktor.io/), a framework to create an HTTP client for retrieving data over the internet.
39
+
*[Ktor](https://ktor.io/), a framework for sending and retrieving data over HTTP.
41
40
42
41
### kotlinx.coroutines
43
42
44
43
To add `kotlinx.coroutines` to your project, specify a dependency in the common source set. To do so, add the following
45
-
line to the `shared/build.gradle.kts` file:
44
+
line to the `sharedLogic/build.gradle.kts` file:
46
45
47
46
```kotlin
48
47
kotlin {
@@ -56,13 +55,13 @@ kotlin {
56
55
}
57
56
```
58
57
59
-
The Kotlin Multiplatform Gradle plugin automatically adds a dependency to the platform-specific (iOS and Android) parts
58
+
The Kotlin Multiplatform Gradle plugin automatically adds a dependency on the platform-specific (iOS and Android) artifacts
60
59
of `kotlinx.coroutines`.
61
60
62
61
### kotlinx.serialization
63
62
64
63
To use the `kotlinx.serialization` library, set up a corresponding Gradle plugin.
65
-
To do that, add the following line to the existing `plugins {}` block at the very beginning of the `shared/build.gradle.kts` file:
64
+
To do that, add the following line to the existing `plugins {}` block at the very beginning of the `sharedLogic/build.gradle.kts` file:
66
65
67
66
```kotlin
68
67
plugins {
@@ -73,10 +72,10 @@ plugins {
73
72
74
73
### Ktor
75
74
76
-
You need to add the core dependency (`ktor-client-core`) to the common source set of the shared module.
77
-
You also need to add supporting dependencies:
75
+
Add the base Ktor client dependency (`ktor-client-core`) to the common source set of the shared module,
76
+
along with these supporting dependencies:
78
77
79
-
* Add the `ContentNegotiation`functionality (`ktor-client-content-negotiation`), which allows serializing and deserializing
78
+
* Add the `ContentNegotiation`library (`ktor-client-content-negotiation`), which allows serializing and deserializing
80
79
the content in a specific format.
81
80
* Add the `ktor-serialization-kotlinx-json` dependency to instruct Ktor to use the JSON format and `kotlinx.serialization`
82
81
as a serialization library. Ktor will expect JSON data and deserialize it into a data class when receiving responses.
@@ -108,14 +107,14 @@ kotlin {
108
107
109
108
Synchronize the Gradle files by clicking the **Sync Gradle Changes** button.
110
109
111
-
## Create API requests
110
+
## Set up API requests
112
111
113
-
You'll need the [SpaceX API](https://github.com/r-spacex/SpaceX-API/tree/master/docs#rspacex-api-docs) to retrieve data, and you'll use a single method to
114
-
get the list of all launches from the **v4/launches** endpoint.
112
+
You'll use the [SpaceX API](https://github.com/r-spacex/SpaceX-API/tree/master/docs#rspacex-api-docs) to retrieve data,
113
+
specifically the list of all launches from the **v4/launches** endpoint.
115
114
116
-
### Add a data model
115
+
### Create a data model
117
116
118
-
In the `shared/src/commonMain/.../greetingkmp` directory, create a new `RocketLaunch.kt` file
117
+
In the `sharedLogic/src/commonMain/.../greeting` directory, create a new `RocketLaunch.kt` file
119
118
and add a data class which stores data from the SpaceX API:
120
119
121
120
```kotlin
@@ -135,14 +134,14 @@ data class RocketLaunch (
135
134
)
136
135
```
137
136
138
-
* The `RocketLaunch` class is marked with the `@Serializable` annotation, so that the `kotlinx.serialization` plugin can
137
+
* The `RocketLaunch` class is marked with the `@Serializable` annotation so that the `kotlinx.serialization` plugin can
139
138
automatically generate a default serializer for it.
140
-
* The `@SerialName` annotation allows you to redefine field names, making it possible to declare properties in data classes
141
-
with more readable names.
139
+
* The `@SerialName` annotation allows you to redefine field names, making it possible to declare properties with more readable names
140
+
in data classes.
142
141
143
142
### Connect HTTP client
144
143
145
-
1. In the `shared/src/commonMain/.../greetingkmp` directory, create a new `RocketComponent` class.
144
+
1. In the `sharedLogic/src/commonMain/.../greeting` directory, create a new `RocketComponent` class.
146
145
2. Add the `httpClient` property to retrieve rocket launch information through an HTTP GET request:
147
146
148
147
```kotlin
@@ -165,28 +164,14 @@ data class RocketLaunch (
165
164
```
166
165
167
166
*The [`ContentNegotiation`](https://ktor.io/docs/serialization-client.html#register_json) Ktor plugin and the JSON serializer deserialize the result of the GET request.
168
-
*TheJSON serializer here is configured in a way that it prints JSONin a more readable manner with the `prettyPrint` property.It
169
-
is more flexible when reading malformed JSON with `isLenient`,
167
+
*TheJSON serializer here is configured insuch a way that it prints JSONin a more readable manner with the `prettyPrint` property.
168
+
Thisis more flexible when reading malformed JSON with `isLenient`,
170
169
and it ignores keys that haven't been declared in the rocket launch model with `ignoreUnknownKeys`.
171
170
172
-
3. Add the `getDateOfLastSuccessfulLaunch()` suspending function to `RocketComponent`:
171
+
3. Add the `getDateOfLastSuccessfulLaunch()` [suspending function](https://kotlinlang.org/docs/coroutines-basics.html) to `RocketComponent`,
172
+
which will retrieve information about rocket launches asynchronously:
173
173
174
174
```kotlin
175
-
class RocketComponent {
176
-
// ...
177
-
178
-
private suspend fun getDateOfLastSuccessfulLaunch(): String {
179
-
180
-
}
181
-
}
182
-
```
183
-
184
-
4. Call the `httpClient.get()` function to retrieve information about rocket launches:
185
-
186
-
```kotlin
187
-
import io.ktor.client.request.get
188
-
import io.ktor.client.call.body
189
-
190
175
class RocketComponent {
191
176
// ...
192
177
@@ -198,10 +183,12 @@ data class RocketLaunch (
198
183
199
184
* `httpClient.get()` is also a suspending function
200
185
because it needs to retrieve data over the network asynchronously without blocking threads.
201
-
* Suspending functions can only be called from coroutines or other suspending functions. This is why `getDateOfLastSuccessfulLaunch()`
202
-
was marked with the `suspend` keyword. The network request is executed in the HTTP client's thread pool.
186
+
* Suspending functions can only be called from coroutines or other suspending functions.
187
+
This is why `getDateOfLastSuccessfulLaunch()` was marked with the `suspend` keyword.
188
+
The network request is executed in the HTTP client's thread pool.
203
189
204
-
5. Update the function again to find the last successful launch in the list:
190
+
4. After the HTTP request call, add the call to get the last successful launch in the list
191
+
(the list of launches is sorted by date from oldest to newest):
205
192
206
193
```kotlin
207
194
classRocketComponent {
@@ -214,9 +201,7 @@ data class RocketLaunch (
214
201
}
215
202
```
216
203
217
-
The list of rocket launches is sorted by date from oldest to newest.
218
-
219
-
6. Convert the launch date from UTC to your local date and format the output:
204
+
5. Convert the launch date from UTC to your local date, then format the output:
0 commit comments