Skip to content

Commit 2552557

Browse files
committed
update: halfway to updating the final native UI step
1 parent f5c7e3f commit 2552557

2 files changed

Lines changed: 34 additions & 51 deletions

File tree

topics/multiplatform-onboard/multiplatform-dependencies.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ The `kotlinx-datetime` library, which has full multiplatform support, is the mos
7676
the number of days from today until the start of next year using the `datetime` date arithmetic and form the phrase to be displayed:
7777

7878
```kotlin
79-
@OptIn(ExperimentalTime::class)
8079
fun daysUntilNewYear(): Int {
8180
val today = Clock.System.todayIn(TimeZone.currentSystemDefault())
8281
val closestNewYear = LocalDate(today.year + 1, 1, 1)

topics/multiplatform-onboard/multiplatform-upgrade-app.md

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ and display the date of the last successful launch of a SpaceX rocket.
3333

3434
You'll need to add the following multiplatform libraries in your project:
3535

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
3938
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.
4140

4241
### kotlinx.coroutines
4342

4443
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:
4645

4746
```kotlin
4847
kotlin {
@@ -56,13 +55,13 @@ kotlin {
5655
}
5756
```
5857

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
6059
of `kotlinx.coroutines`.
6160

6261
### kotlinx.serialization
6362

6463
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:
6665

6766
```kotlin
6867
plugins {
@@ -73,10 +72,10 @@ plugins {
7372

7473
### Ktor
7574

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:
7877

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
8079
the content in a specific format.
8180
* Add the `ktor-serialization-kotlinx-json` dependency to instruct Ktor to use the JSON format and `kotlinx.serialization`
8281
as a serialization library. Ktor will expect JSON data and deserialize it into a data class when receiving responses.
@@ -108,14 +107,14 @@ kotlin {
108107

109108
Synchronize the Gradle files by clicking the **Sync Gradle Changes** button.
110109

111-
## Create API requests
110+
## Set up API requests
112111

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.
115114

116-
### Add a data model
115+
### Create a data model
117116

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
119118
and add a data class which stores data from the SpaceX API:
120119

121120
```kotlin
@@ -135,14 +134,14 @@ data class RocketLaunch (
135134
)
136135
```
137136

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
139138
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.
142141

143142
### Connect HTTP client
144143

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.
146145
2. Add the `httpClient` property to retrieve rocket launch information through an HTTP GET request:
147146

148147
```kotlin
@@ -165,28 +164,14 @@ data class RocketLaunch (
165164
```
166165

167166
* 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-
* The JSON serializer here is configured in a way that it prints JSON in a more readable manner with the `prettyPrint` property. It
169-
is more flexible when reading malformed JSON with `isLenient`,
167+
* The JSON serializer here is configured in such a way that it prints JSON in a more readable manner with the `prettyPrint` property.
168+
This is more flexible when reading malformed JSON with `isLenient`,
170169
and it ignores keys that haven't been declared in the rocket launch model with `ignoreUnknownKeys`.
171170
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:
173173
174174
```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-
190175
class RocketComponent {
191176
// ...
192177
@@ -198,10 +183,12 @@ data class RocketLaunch (
198183
199184
* `httpClient.get()` is also a suspending function
200185
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.
203189

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):
205192

206193
```kotlin
207194
class RocketComponent {
@@ -214,9 +201,7 @@ data class RocketLaunch (
214201
}
215202
```
216203

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:
220205

221206
```kotlin
222207
import kotlinx.datetime.TimeZone
@@ -227,7 +212,6 @@ data class RocketLaunch (
227212
class RocketComponent {
228213
// ...
229214

230-
@OptIn(ExperimentalTime::class)
231215
private suspend fun getDateOfLastSuccessfulLaunch(): String {
232216
val rockets: List<RocketLaunch> =
233217
httpClient.get("https://api.spacexdata.com/v4/launches").body()
@@ -240,10 +224,10 @@ data class RocketLaunch (
240224
}
241225
```
242226

243-
The date will be in the "MMMM DD, YYYY" format, for example, OCTOBER 5, 2022.
227+
The date will be in the "MMMM DD, YYYY" format, for example, "OCTOBER 5, 2022".
244228

245-
7. Add another suspending function, `launchPhrase()`, which will create a message using the `getDateOfLastSuccessfulLaunch()`
246-
function:
229+
6. To the same class, add another suspending function, `launchPhrase()`,
230+
which will create a message using the `getDateOfLastSuccessfulLaunch()` function:
247231

248232
```kotlin
249233
class RocketComponent {
@@ -261,8 +245,8 @@ data class RocketLaunch (
261245

262246
### Create the flow
263247

264-
You can use flows instead of suspending functions. They emit a sequence of values instead of a single value that
265-
suspending functions return.
248+
You can use [flows](https://kotlinlang.org/docs/flow.html) instead of simply calling suspending functions.
249+
Flows emit a sequence of values as the values are coming in returning a single value like suspending functions.
266250

267251
1. Open the `Greeting.kt` file in the `shared/src/commonMain/kotlin` directory.
268252
2. Add a `rocketComponent` property to the `Greeting` class. The property will store the message with the last successful launch date:
@@ -538,7 +522,7 @@ wrappers.
538522
}
539523
```
540524

541-
3. Also in the `shared/build.gradle.kts` file, opt-in to the experimental `@ObjCName` annotation:
525+
3. Also, in the `shared/build.gradle.kts` file, opt-in to the experimental `@ObjCName` annotation:
542526

543527
```kotlin
544528
kotlin {

0 commit comments

Comments
 (0)