Skip to content

Commit

Permalink
launch the coroutine and switch the coroutine Dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-XWB committed Sep 9, 2024
1 parent 09662df commit 6235410
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LaunchCoroutinesActivity"
android:exported="true"
android:theme="@style/Theme.CourseCoroutines">
</activity>
</application>

</manifest>
66 changes: 66 additions & 0 deletions app/src/main/java/com/novar/supercorouutine/1_launch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.novar.supercorouutine

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.novar.supercorouutine.ui.theme.CourseCoroutinesTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.Executors
import kotlin.concurrent.thread
import kotlin.coroutines.EmptyCoroutineContext

class LaunchCoroutinesActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()// Enable edge-to-edge display
setContent {
CourseCoroutinesTheme {

}
}

println("Main thread: ${Thread.currentThread().name}")


// launch a new thread
thread {

}

// launch a new thread with executor
val executor = Executors.newCachedThreadPool()
executor.execute {
println("Executor thread: ${Thread.currentThread().name}")
}

//switch to the main thread
// handler
var handle = Handler(Looper.getMainLooper())
handle.post {

}

// view
val view: View = View(this)
view.post {

}

//CoroutineScope
val coroutineScope = CoroutineScope(EmptyCoroutineContext)
coroutineScope.launch {
println("Coroutine thread: ${Thread.currentThread().name}")
}

coroutineScope.launch(Dispatchers.IO) {
println("Coroutine thread: ${Thread.currentThread().name}")
}

}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/novar/supercorouutine/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class MainActivity : ComponentActivity() {
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
modifier = modifier,
color = androidx.compose.ui.graphics.Color.Red
)
}

Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/novar/supercorouutine/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ fun SuperCorouutineTheme(
else -> LightColorScheme
}

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}

@Composable
fun CourseCoroutinesTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}

darkTheme -> DarkColorScheme
else -> LightColorScheme
}

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources>

<style name="Theme.SuperCorouutine" parent="android:Theme.Material.Light.NoActionBar" />

<style name="Theme.CourseCoroutines" parent="android:Theme.Material.Light.NoActionBar" />
</resources>

0 comments on commit 6235410

Please sign in to comment.