Skip to content

Commit fd8e719

Browse files
Initial commit with UI thread blocking demonstration
0 parents  commit fd8e719

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1143
-0
lines changed

.gitignore

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#built application files
3+
*.apk
4+
*.ap_
5+
6+
# files for the dex VM
7+
*.dex
8+
9+
# Java class files
10+
*.class
11+
12+
# generated files
13+
bin/
14+
gen/
15+
16+
# Local configuration file (sdk path, etc)
17+
local.properties
18+
19+
# Windows thumbnail db
20+
Thumbs.db
21+
22+
# OSX files
23+
.DS_Store
24+
25+
# Android Studio
26+
.idea/
27+
.gradle
28+
build/
29+
*.iml
30+
captures/
31+
.externalNativeBuild

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion 29
6+
defaultConfig {
7+
applicationId "com.techyourchance.coroutines"
8+
minSdkVersion 21
9+
targetSdkVersion 29
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
14+
signingConfigs {
15+
release {
16+
storeFile file('../release.keystore')
17+
storePassword 'release'
18+
keyAlias 'release'
19+
keyPassword 'release'
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
minifyEnabled true
26+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27+
signingConfig signingConfigs.release
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility JavaVersion.VERSION_1_8
32+
targetCompatibility JavaVersion.VERSION_1_8
33+
}
34+
35+
}
36+
37+
dependencies {
38+
implementation fileTree(dir: 'libs', include: ['*.jar'])
39+
implementation 'androidx.appcompat:appcompat:1.2.0'
40+
41+
implementation 'com.ncapdevi:frag-nav:3.3.0'
42+
43+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
44+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
45+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
46+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="com.techyourchance.coroutines"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<application
6+
android:name="com.techyourchance.coroutines.MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name="com.techyourchance.coroutines.MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.techyourchance.coroutines
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import android.widget.ImageButton
6+
import android.widget.TextView
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.techyourchance.coroutines.common.ScreensNavigator
9+
import com.techyourchance.coroutines.common.ToolbarDelegate
10+
import com.techyourchance.coroutines.common.dependencyinjection.ActivityCompositionRoot
11+
12+
class MainActivity : AppCompatActivity(), ToolbarDelegate {
13+
14+
private lateinit var screensNavigator: ScreensNavigator
15+
private lateinit var btnBack: ImageButton
16+
private lateinit var txtScreenTitle: TextView
17+
18+
val compositionRoot by lazy {
19+
ActivityCompositionRoot(this,(application as MyApplication).applicationCompositionRoot)
20+
}
21+
22+
override fun onCreate(savedInstanceState: Bundle?) {
23+
super.onCreate(savedInstanceState)
24+
setContentView(R.layout.activity_main)
25+
26+
screensNavigator = compositionRoot.screensNavigator
27+
screensNavigator.init(savedInstanceState)
28+
29+
btnBack = findViewById(R.id.btn_back)
30+
btnBack.setOnClickListener { screensNavigator.navigateUp() }
31+
32+
txtScreenTitle = findViewById(R.id.txt_screen_title)
33+
}
34+
35+
override fun onSaveInstanceState(outState: Bundle) {
36+
super.onSaveInstanceState(outState)
37+
screensNavigator.onSaveInstanceState(outState)
38+
}
39+
40+
override fun onBackPressed() {
41+
if (!screensNavigator.navigateBack()) {
42+
super.onBackPressed()
43+
}
44+
}
45+
46+
override fun setScreenTitle(screenTitle: String) {
47+
txtScreenTitle.text = screenTitle
48+
}
49+
50+
override fun showUpButton() {
51+
btnBack.visibility = View.VISIBLE
52+
}
53+
54+
override fun hideUpButton() {
55+
btnBack.visibility = View.INVISIBLE
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.techyourchance.coroutines
2+
3+
import android.app.Application
4+
import com.techyourchance.coroutines.common.dependencyinjection.ApplicationCompositionRoot
5+
6+
class MyApplication : Application() {
7+
8+
val applicationCompositionRoot = ApplicationCompositionRoot()
9+
10+
override fun onCreate() {
11+
super.onCreate()
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.techyourchance.coroutines.common
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.fragment.app.Fragment
6+
import com.techyourchance.coroutines.MainActivity
7+
8+
abstract class BaseFragment : Fragment() {
9+
10+
protected open val screenTitle = ""
11+
12+
protected val compositionRoot get() = (requireActivity() as MainActivity).compositionRoot
13+
14+
protected lateinit var screensNavigator: ScreensNavigator
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
screensNavigator = compositionRoot.screensNavigator
19+
}
20+
21+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
22+
val toolbarManipulator = compositionRoot.toolbarManipulator
23+
toolbarManipulator.setScreenTitle(screenTitle)
24+
if (screensNavigator.isRootScreen()) {
25+
toolbarManipulator.hideUpButton()
26+
} else {
27+
toolbarManipulator.showUpButton()
28+
}
29+
}
30+
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.techyourchance.coroutines.common
2+
3+
import android.os.Bundle
4+
import com.ncapdevi.fragnav.FragNavController
5+
import com.ncapdevi.fragnav.FragNavController.RootFragmentListener
6+
import com.techyourchance.coroutines.demonstrations.uithread.UiThreadDemoFragment
7+
import com.techyourchance.coroutines.home.HomeFragment
8+
9+
class ScreensNavigator(private val mFragNavController: FragNavController) {
10+
11+
fun init(savedInstanceState: Bundle?) {
12+
mFragNavController.rootFragmentListener = object : RootFragmentListener {
13+
override val numberOfRootFragments get() = 1
14+
15+
override fun getRootFragment(index: Int) = HomeFragment.newInstance()
16+
}
17+
18+
mFragNavController.initialize(FragNavController.TAB1, savedInstanceState)
19+
}
20+
21+
fun onSaveInstanceState(outState: Bundle?) {
22+
mFragNavController.onSaveInstanceState(outState)
23+
}
24+
25+
fun isRootScreen() = mFragNavController.isRootFragment
26+
27+
fun navigateBack(): Boolean {
28+
if (mFragNavController.isRootFragment) {
29+
return false
30+
} else {
31+
mFragNavController.popFragment()
32+
return true
33+
}
34+
}
35+
36+
fun navigateUp() {
37+
mFragNavController.popFragment()
38+
}
39+
40+
fun toHomeScreen() {
41+
mFragNavController.clearStack()
42+
mFragNavController.pushFragment(HomeFragment.newInstance())
43+
}
44+
45+
fun toUiThreadDemonstration() {
46+
mFragNavController.pushFragment(UiThreadDemoFragment.newInstance())
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.techyourchance.coroutines.common
2+
3+
import android.util.Log
4+
5+
object ThreadInfoLogger {
6+
7+
private const val TAG = "ThreadInfoLogger"
8+
9+
fun logThreadInfo(message: String) {
10+
Log.i(TAG, "$message; thread name: ${Thread.currentThread().name}; thread ID: ${Thread.currentThread().id}")
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.techyourchance.coroutines.common
2+
3+
interface ToolbarDelegate {
4+
fun setScreenTitle(screenTitle: String)
5+
fun showUpButton()
6+
fun hideUpButton()
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.techyourchance.coroutines.common.dependencyinjection
2+
3+
import androidx.fragment.app.FragmentActivity
4+
import com.ncapdevi.fragnav.FragNavController
5+
import com.techyourchance.coroutines.R
6+
import com.techyourchance.coroutines.common.ScreensNavigator
7+
import com.techyourchance.coroutines.common.ToolbarDelegate
8+
9+
class ActivityCompositionRoot(
10+
private val activity: FragmentActivity,
11+
private val appCompositionRoot: ApplicationCompositionRoot
12+
) {
13+
14+
val toolbarManipulator get() = activity as ToolbarDelegate
15+
16+
val screensNavigator: ScreensNavigator by lazy {
17+
ScreensNavigator(fragNavController)
18+
}
19+
20+
private val fragNavController get() = FragNavController(fragmentManager, R.id.frame_content)
21+
22+
private val fragmentManager get() = activity.supportFragmentManager
23+
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.techyourchance.coroutines.common.dependencyinjection
2+
3+
class ApplicationCompositionRoot

0 commit comments

Comments
 (0)