Skip to content

Commit

Permalink
update: first suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-pavlov committed Jul 18, 2023
1 parent 85ec378 commit ecb5535
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,19 @@ IDE will show a warning:
1. Open the `Greeting.kt` file and try to access one of the Java classes, `java.util.Random().nextBoolean()`, inside the `greet()` function:

```kotlin
import java.util.Random

fun greet(): String {
val firstWord = if (Random().nextBoolean()) "Hi!" else "Hello!"
}
```

Android Studio highlights that `Random` class is unresolved because you can't call specific Java functions from the common Kotlin code.
2. Follow IDE's suggestions and replace it with `kotlin.random.Random` from the Kotlin standard library. The code now compiles successfully.
3. Add a bit of unpredictability to the greeting:
2. Follow IDE's suggestions and replace it with `kotlin.random.Random` from the Kotlin standard library.
This is a multiplatform library that works on all platforms and is included automatically as a dependency.
The code should now compile successfully.
3. Add a bit of unpredictability to the greeting. Update the shared code with the `reversed()` function
from the Kotlin standard library for reversing the text:
```kotlin
import kotlin.random.Random
Expand All @@ -100,8 +105,7 @@ IDE will show a warning:
fun greet(): String {
val firstWord = if (Random.nextBoolean()) "Hi!" else "Hello!"
return firstWord + "\n" +
"Guess what it is! > ${platform.name.reversed()}!"
return "$firstWord\nGuess what it is! > ${platform.name.reversed()}!"
}
}
```
Expand All @@ -115,8 +119,8 @@ The common source set can define an interface or an expected declaration. Then e
in this case `androidMain` and `iosMain`, has to provide actual platform-specific implementations for the expected
declarations from the common source set.

While building the code for a specific target,
the Kotlin compiler will automatically substitute the expected declarations with the actual ones for this target.
While building the code for a specific target, the Kotlin compiler will automatically substitute
the invocation of the expected declarations with the invocation of the actual implementation for this target.

1. When creating a project in Android Studio, you get a template with the `Platform.kt` file in the `commonMain` module:

Expand Down Expand Up @@ -169,9 +173,9 @@ the Kotlin compiler will automatically substitute the expected declarations with
actual fun getPlatform(): Platform = IOSPlatform()
```

During compilation, the Kotlin compiler automatically substitutes the expected declaration of the `getPlatform()` function
with its correct actual implementations.
The Android app uses the `AndroidPlatform` implementation, while the iOS app uses the `IOSPlatform` implementation.
During compilation, Kotlin automatically substitutes the invocation of the `getPlatform()` expected declaration
with the invocation of its correct actual implementation.
The Android app uses the `AndroidPlatform()` implementation, while the iOS app uses the `IOSPlatform()` implementation.

Now you can run the apps to ensure everything works.

Expand Down Expand Up @@ -216,8 +220,7 @@ such as properties and classes. Let's implement an expected property:
fun greet(): String {
val firstWord = if (Random.nextBoolean()) "Hi!" else "Hello!"

return firstWord + " [$num]\n" +
"Guess what it is! > ${platform.name.reversed()}!"
return "$firstWord [$num]\nGuess what it is! > ${platform.name.reversed()}!"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ multiplatform support, is the most convenient way to work with dates in your sha
```kotlin
class Greeting {
private val platform: Platform = getPlatform()

fun greet(): List<String> = buildList {
add(if (Random.nextBoolean()) "Hi!" else "Hello!")
add(
"Guess what it is! > ${platform.name.reversed()}!" +
"\nThere are only ${daysUntilNewYear()} days left until New Year! πŸŽ†"
)
add("Guess what it is! > ${platform.name.reversed()}!")
add("\nThere are only ${daysUntilNewYear()} days left until New Year! πŸŽ†")
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Make some changes and see how it is reflected in the UI:
}
```
Here the `LazyColumn` composable shows the list of `Text` items.
Here the `LazyColumn` composable shows the list of `Text` items, adds padding around the content and a space between the list items.
5. Follow Android Studio's suggestions to import the missing dependencies.
6. Update the preview as well, passing a list as an argument:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,14 @@ data class RocketLaunch (
class Greeting {
// ...
@Throws(Exception::class)
suspend fun greet(): String {
suspend fun greet(): List<String> = buildList {
val rockets: List<RocketLaunch> =
httpClient.get("https://api.spacexdata.com/v4/launches").body()
val lastSuccessLaunch = rockets.last { it.launchSuccess == true }
add(if (Random.nextBoolean()) "Hi!" else "Hello!")
add(
"Guess what it is! > ${platform.name.reversed()}!" +
"\nThere are only ${daysUntilNewYear()} days left until New Year! πŸŽ†" +
"\nThe last successful launch was ${lastSuccessLaunch.launchDateUTC} πŸš€"
)
add("Guess what it is! > ${platform.name.reversed()}!")
add("\nThere are only ${daysUntilNewYear()} days left until New Year! πŸŽ†")
add("\nThe last successful launch was ${lastSuccessLaunch.launchDateUTC} πŸš€")
}
}
```
Expand Down

0 comments on commit ecb5535

Please sign in to comment.