Skip to content

Commit

Permalink
Added coroutine activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramakrishna Joshi committed Sep 21, 2022
1 parent 3d1a065 commit 63c6c73
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 46 deletions.
20 changes: 4 additions & 16 deletions .idea/codeStyles/Project.xml

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

2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

5 changes: 2 additions & 3 deletions .idea/gradle.xml

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

10 changes: 9 additions & 1 deletion .idea/misc.xml

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

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

21 changes: 16 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
compileSdkVersion 30
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.unittestinginandroid"
minSdkVersion 22
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -20,6 +20,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

}

dependencies {
Expand Down Expand Up @@ -65,7 +75,8 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"

// Room
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
// Room - Mac M1 requires 2.4.0-alpha03 version atleast
def room_version = "2.4.0-alpha03"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".playground.UnderstandingCoroutineActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">

<!--
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
-->
</activity>
<activity android:name=".TestActivity" />
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class MainActivity : AppCompatActivity() {
// suspend function can be called for either another suspend function
// or from a coroutine context
private suspend fun networkCall() {
delay(5000)
delay(5000) // delay is nothing but another suspend function that Delays coroutine
// for a given time without blocking a thread and resumes it after a specified time.
logInfo("network call executed in thread : ${Thread.currentThread()}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package com.example.unittestinginandroid
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*

class TestActivity : AppCompatActivity() {

Expand All @@ -23,6 +20,7 @@ class TestActivity : AppCompatActivity() {
}
}


runBlocking {
job.join()
Log.d(TAG, "Coroutine Finished")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.unittestinginandroid.playground

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.unittestinginandroid.R
import kotlinx.android.synthetic.main.activity_understanding_coroutine.*
import kotlinx.coroutines.*

class UnderstandingCoroutineActivity : AppCompatActivity() {
private val TAG = this::class.java.simpleName

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_understanding_coroutine)

buttonLaunchCoroutine.setOnClickListener {
logInfo("Button Clicked")

GlobalScope.launch(Dispatchers.IO) {
logInfo("Coroutine Running in Thread ${Thread.currentThread()}")

val resultA = function1()
val resultB = function2()
logInfo("After Network Call")
val result = resultA + resultB
logInfo("result is $result")
}
logInfo("Button Click Executed")
}

buttonRunBlocking.setOnClickListener {
logInfo("Button Clicked")
runBlocking {
logInfo("Coroutine Running in Thread ${Thread.currentThread()}")
async {
function1()
}
async {
function2()
}
logInfo("Control after calling both functions")
}
logInfo("Button Click Executed")
}
}

//One important thumb rule is Suspend function should be called only from a coroutine or
// another suspend function
suspend fun function1(): Int {
logInfo("function1 : Running in Thread ${Thread.currentThread()}")
logInfo("function1 : Delay function called")
delay(3000)
logInfo("function1 : Delay function executed")
return 10
}

suspend fun function2(): Int {
logInfo("function2 : Running in Thread ${Thread.currentThread()}")
logInfo("function2 : Delay function called")
delay(10000)
logInfo("function2 : Delay function executed")
return 20
}

private fun logInfo(message: String) {
Log.d(TAG, message)
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.4.10'
ext.kotlin_version = '1.6.21'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit 63c6c73

Please sign in to comment.