Coder Social home page Coder Social logo

basic-android-kotlin-compose-training-race-tracker's Introduction

Race tracker app

The Race tracker app simulates the progress of two players in a race. The idea is to demonstrate basic concepts of Kotlin coroutines. A user can start, pause, or reset the race with buttons in the UI.

Pre-requisites

  • Experience with building apps using Jetpack Compose.
  • How to create and run a project in Android Studio.
  • Familiar with Kotlin coroutines

Getting Started

  1. Install Android Studio, if you don't already have it.
  2. Download the sample.
  3. Import the sample into Android Studio.
  4. Build and run the sample.

basic-android-kotlin-compose-training-race-tracker's People

Contributors

android-dev-lxl avatar owenscott-gds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

basic-android-kotlin-compose-training-race-tracker's Issues

Android Basics: Introduction to Coroutines in Kotlin Playground

https://developer.android.com/codelabs/basic-android-kotlin-compose-coroutines-kotlin-playground?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-coroutines-kotlin-playground#1

step 6

then you can wrap your code in a call to measureTimeMillis() which will return the time it in milliseconds that it takes to run the block of code passed in. 

should read

then you can wrap your code in a call to measureTimeMillis() which will return the time in milliseconds that it takes to run the block of code passed in. 

Android Basics: Introduction to Coroutines in Kotlin Playground

Android Basics: Introduction to Coroutines in Android Studio

suspend fun run() {
    try {
        while (currentProgress < maxProgress) {
            delay(progressDelayMillis)
            currentProgress += progressIncrement
        }
    } catch (e: CancellationException) {
        Log.e("RaceParticipant", "$name: ${e.message}")
        throw e // Always re-throw CancellationException.
    }
}

Cannot catch an error

Android Basics: Introduction to Coroutines in Android Studio - Carlifying parts 3,4, 6 and 7

It should be mentioned in Parts 3,4 and 6 that the following import should be added to the classes RaceParticipant.kt, RaceTrackerApp.kt and RaceParticipantTest.kt.

import kotlinx.coroutines.*

delay, launch etc. appear with error otherwise.

In Part 7 focusing on testing, the build.grale.kts that needs to be updated is the one of the module. This could be clarified.

It should also be clarified which imports to do:

import org.junit.Test
import org.junit.Assert.assertEquals

In part 7 focusing on testing, it is written: 6. Run the test to confirm that it passes. It would be good to write how to run the tests and have a screenshot for users new to JUnit.

image

Android Basics: Introduction to Coroutines in Kotlin Playground

I think i found a little mistake
when you call the method inside the launch{}, it should be printForecast() and NOT getForecast() when you explain about the
"Get a reference to the job for the coroutine that fetches the forecast, and store it in a variable called job."

Android Basics: Introduction to Coroutines in Kotlin Playground

https://developer.android.com/codelabs/basic-android-kotlin-compose-coroutines-kotlin-playground?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-coroutines-kotlin-playground#3

From the output, you can observe that `getTemperature()` throws an exception.

this is not true; from the output it is known only that getWeatherReport function threw an exception - only looking at the source code we know that it was getTemperature() as the throwing of the exception is hard coded

Android Basics: Introduction to Coroutines in Kotlin Playground - 3. Asynchronous code - dead link

In the section:
Note: As a real-world example of async(), you can check out this part of the [Now in Android app](https://github.com/android/nowinandroid). In the [SyncWorker](https://github.com/android/nowinandroid/blob/main/sync/work/src/main/java/com/google/samples/apps/nowinandroid/sync/workers/SyncWorker.kt#L65) class, the call to sync() returns a boolean if the sync to a particular backend was successful. If any of the sync operations failed, then the app needs to perform a retry.
The link to SyncWorker.kt links through a non existant java folder, should be kotlin

Android Basics: Introduction to Coroutines in Kotlin Playground

Hi,

I have a question about the Cancellation in Kotlin coroutines in the codelab for Android Basics.
In the following link: https://developer.android.com/codelabs/basic-android-kotlin-compose-coroutines-kotlin-playground?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-1#3

I have seen that in the step 2 of the Cancellation it says that when using the temperature.cancel() it will not show the temperature value because it was cancelled, however I can see that if the ${temperature.await() is not removed I get the following error:

image

Testing the code here I can see that if I just remove the ${temperature.await() from the code (without adding the temperature.cancel()) I get the same result as in the step 3.

My question is, what is the usability of the cancellation if it doesn't work without completelly removing the return of the function?

Thank you very much in advance for your time!

Android Basics: Introduction to Coroutines in Kotlin Playground

Add print statements to see what thread you are on by calling Thread.currentThread().name.

https://developer.android.com/codelabs/basic-android-kotlin-compose-coroutines-kotlin-playground?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-coroutines-kotlin-playground#4

code is not properly indented

should be

runBlocking {
        println("${Thread.currentThread().name} - runBlocking function")
        launch {
            println("${Thread.currentThread().name} - launch function")
            withContext(Dispatchers.Default) {
                println("${Thread.currentThread().name} - withContext function")
                delay(1000)
                println("10 results found.")
            }
            println("${Thread.currentThread().name} - end of launch function")
        }
        println("Loading...")
    }

Android Basics: Introduction to Coroutines in Kotlin Playground

Course: Android Basics with Compose
Module: Connect to the Internet - Get Data
Section: Introduction to Coroutines in Kotlin Playground

In this section, the content briefs about Cancellation in Coroutines. There is an issue with the explanation here. Please refer to the below image

image

In this example, it is mentioned that "The weather report only consists of the weather forecast Sunny, but not the temperature because that coroutine was cancelled."
But the reason why weather report only consists of weather forecast Sunny but not temperature because we are not returning temperature back.

  1. If the temperature coroutine is cancelled and if we try to return temperature, it returns following error - DeferredCoroutine was cancelled
  2. If coroutine is not cancelled and if we don't return temperature, the code will print weather forecast Sunny without temperature anyway

So cancel and temperature not being in the weather forecast have no relation and the example given does not make sense

Android Basics: Introduction to Coroutines in Kotlin Playground

In the part 4.Exception and Cancelation at the section Cancelation, when executing this piece of code :

import kotlinx.coroutines.*
import kotlin.system.*

suspend fun getForecast(): String {
    delay(1000)
    return "Sunny"
}

suspend fun getTemperature(): String {
    delay(1000)
    return "30\u00b0C"
}

suspend fun getWeatherReport() = coroutineScope {
    val forecast = async { getForecast() }
    val temperature = async { getTemperature() }

    delay(200)
    temperature.cancel()
    
    "${forecast.await()} ${temperature.await()}"
}

fun main() {
    val time = measureTimeMillis {
        runBlocking {
            println("Weather forecast")
            println(getWeatherReport())
        	println("Have a good day!")
        }
    }
    println("Execution time ${time / 1000.0} seconds")   
}

I have the following issue:

Weather forecast
Exception in thread "main" kotlinx.coroutines.JobCancellationException: DeferredCoroutine was cancelled
 at kotlinx.coroutines.JobSupport.cancel (JobSupport.kt:1558) 
 at kotlinx.coroutines.Job$DefaultImpls.cancel$default (Job.kt:199) 
 at FileKt$getWeatherReport$2.invokeSuspend (File.kt:19) 
Caused by: kotlinx.coroutines.JobCancellationException: DeferredCoroutine was cancelled
at kotlinx.coroutines.JobSupport .cancel(JobSupport.kt:1558)
at kotlinx.coroutines.Job$DefaultImpls .cancel$default(Job.kt:199)
at FileKt$getWeatherReport$2 .invokeSuspend(File.kt:19)

I am supposed to have the following message :

Weather forecast
Sunny
Have a good day!

Android Basics: Introduction to Coroutines in Kotlin Playground

Good day. I may have misunderstood something, but in this section you contradict yourself. You first say that the launch() function Dispatchers.Default as a default dispatcher (Fig. 1), but then you give an example where in launch() you output the stream name and "main" is output, that means that Dispatchers.Main is used by default for some reason (Fig. 2). So, what is used in launch() by default: Dispatchers.Main or Dispatchers.Default
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.