├── Kotlin-Samples-Android ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ │ ├── CoursorIterationTest.kt │ │ │ ├── ParcelizeTest.kt │ │ │ └── SharedPreferencesTest.kt │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── kotlin │ │ └── samuel │ │ │ └── urbanowicz │ │ │ └── MainActivity.kt │ │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── Kotlin-Samples-JS ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── src │ └── main │ │ └── kotlin │ │ └── chapter9 │ │ └── recipe2 │ │ └── AlertDialogApp.kt └── test_app.html ├── Kotlin-Samples ├── .gitignore ├── build.gradle ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ ├── kotlin │ ├── chapter1 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ └── Recipe6.kt │ ├── chapter2 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ ├── Recipe7.kt │ │ ├── Recipe8.kt │ │ ├── Recipe9.kt │ │ └── recipe4 │ │ │ ├── LightBulb.java │ │ │ └── Recipe4.kt │ ├── chapter3 │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ └── Recipe7.kt │ ├── chapter4 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ ├── Recipe7.kt │ │ ├── Recipe8.kt │ │ └── Recipe9.kt │ ├── chapter5 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ ├── Recipe7.kt │ │ ├── Recipe8.kt │ │ ├── Recipe_2_class_diagram_original.jpg │ │ └── Recipe_2_class_diagram_refactored.png │ ├── chapter6 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ └── Recipe7.kt │ ├── chapter7 │ │ ├── Recipe1.kt │ │ ├── Recipe2.kt │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe6.kt │ │ ├── Recipe7.kt │ │ └── Recipe8.kt │ ├── chapter8 │ │ ├── Recipe7.kt │ │ ├── Recipe8.kt │ │ └── Recipe9.kt │ └── chapter9 │ │ ├── Recipe3.kt │ │ ├── Recipe4.kt │ │ ├── Recipe5.kt │ │ ├── Recipe7.kt │ │ ├── Recipe8.kt │ │ ├── recipe1 │ │ ├── ColoredText.kt │ │ └── JavaApp.java │ │ └── recipe6.kt │ └── resources │ ├── file1.txt │ ├── file2.txt │ └── subdirectory │ └── sample_file_2.txt ├── LICENSE └── README.md /Kotlin-Samples-Android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | defaultConfig { 8 | applicationId "com.example.unclesam.myapplication" 9 | minSdkVersion 15 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | androidExtensions { 22 | experimental = true 23 | } 24 | sourceSets { 25 | main.java.srcDirs += 'src/main/kotlin' 26 | androidTest.java.srcDirs += 'src/androidTest/kotlin' 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation 'androidx.appcompat:appcompat:1.0.+' 33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.2' 34 | 35 | // Coroutines 36 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.3' 37 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.2' 38 | 39 | // Android KTX 40 | implementation 'androidx.core:core-ktx:1.0.+' 41 | 42 | // Architectural components 43 | // https://developer.android.com/topic/libraries/architecture/ 44 | implementation "android.arch.lifecycle:runtime:1.1.1" 45 | 46 | // Test 47 | testImplementation 'junit:junit:4.12' 48 | androidTestImplementation 'androidx.test:runner:+' 49 | androidTestImplementation 'androidx.test.espresso:espresso-core:+' 50 | androidTestImplementation 'androidx.test:rules:+' 51 | androidTestImplementation 'androidx.core:core-ktx:1.0.+' 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/proguard-rules.pro: -------------------------------------------------------------------------------- 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 22 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/androidTest/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/androidTest/kotlin/CoursorIterationTest.kt: -------------------------------------------------------------------------------- 1 | import android.Manifest.permission.READ_CONTACTS 2 | import android.Manifest.permission.WRITE_CONTACTS 3 | import android.database.Cursor 4 | import android.provider.ContactsContract 5 | import androidx.test.InstrumentationRegistry 6 | import androidx.test.rule.GrantPermissionRule 7 | import org.junit.Rule 8 | import org.junit.Test 9 | 10 | 11 | class CoursorIterationTest { 12 | @Rule 13 | @JvmField 14 | val contactsPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(READ_CONTACTS) 15 | 16 | @Test 17 | fun iterateContacts() { 18 | val NOT_SPECIFIED = "" 19 | val content = InstrumentationRegistry.getContext().contentResolver 20 | val projection = arrayOf(ContactsContract.Data.DISPLAY_NAME) 21 | val cursor = 22 | content.query(ContactsContract.Contacts.CONTENT_URI, 23 | projection, 24 | NOT_SPECIFIED, 25 | emptyArray(), 26 | NOT_SPECIFIED) 27 | 28 | val contacts = cursor.use { 29 | val contactsList = mutableListOf() 30 | while (it.moveToNext()) { 31 | val contactName = it.getString(ContactsContract.Data.DISPLAY_NAME) 32 | contactsList.add(contactName) 33 | } 34 | contactsList 35 | } 36 | 37 | contacts.take(10).mapNotNull { 38 | println(it) 39 | } 40 | } 41 | } 42 | 43 | fun Cursor.getString(columnName: String): String? { 44 | return getString(getColumnIndex(columnName)) 45 | } -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/androidTest/kotlin/ParcelizeTest.kt: -------------------------------------------------------------------------------- 1 | import android.os.Bundle 2 | import android.os.Parcelable 3 | import androidx.test.runner.AndroidJUnit4 4 | import kotlinx.android.parcel.Parcelize 5 | import org.junit.Assert.assertEquals 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | @RunWith(AndroidJUnit4::class) 10 | class ParcelizeTest { 11 | 12 | @Test 13 | fun testUserParcelisation() { 14 | // given 15 | val originalUser = User("Bob", Address("Rue de Paris", "123", "Warsaw")) 16 | val bundle = Bundle() 17 | 18 | // when 19 | bundle.putParcelable("my_user", originalUser) 20 | 21 | // then 22 | val deserialisedUser = bundle.get("my_user") as User 23 | assertEquals(originalUser, deserialisedUser) 24 | } 25 | } 26 | 27 | @Parcelize 28 | data class User(val name: String, val address: Address): Parcelable 29 | 30 | @Parcelize 31 | data class Address(val street: String, val number: String, val city: String): Parcelable -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/androidTest/kotlin/SharedPreferencesTest.kt: -------------------------------------------------------------------------------- 1 | import android.preference.PreferenceManager 2 | import androidx.core.content.edit 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | import org.junit.Assert.assertSame 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | @RunWith(AndroidJUnit4::class) 10 | class SharedPreferencesTest { 11 | 12 | @Test 13 | fun testSharedPrefs() { 14 | // given 15 | val sharedPrefs = getDefaultSharedPreferences() 16 | val userName: String = "Gonzo" 17 | 18 | // when 19 | sharedPrefs.edit { 20 | putString("user_name", userName) 21 | } 22 | 23 | // then 24 | val DEFAULT_VALUE = "empty" 25 | val fetchedUserName = sharedPrefs.getString("user_name", DEFAULT_VALUE) 26 | assertSame(userName, fetchedUserName) 27 | } 28 | 29 | private fun getDefaultSharedPreferences() = 30 | PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getContext()) 31 | } -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/main/kotlin/samuel/urbanowicz/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package samuel.urbanowicz 2 | 3 | import android.os.Bundle 4 | import android.view.View 5 | import androidx.appcompat.app.AppCompatActivity 6 | import androidx.lifecycle.GenericLifecycleObserver 7 | import androidx.lifecycle.Lifecycle 8 | import androidx.lifecycle.LifecycleOwner 9 | import com.example.unclesam.myapplication.R 10 | import kotlinx.android.synthetic.main.activity_main.* 11 | import kotlinx.coroutines.experimental.android.UI 12 | import kotlinx.coroutines.experimental.delay 13 | import kotlinx.coroutines.experimental.launch 14 | import kotlinx.coroutines.experimental.withContext 15 | 16 | class MainActivity: AppCompatActivity() { 17 | 18 | val sampleContextAwareViewProperty: View by LifecycleAwareLazy(lifecycle) { 19 | View(this) 20 | } 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_main) 25 | text_field.text = "Bonjour!" 26 | 27 | val job = launch { 28 | var counter = 1 29 | while (true) { 30 | delay(1000) 31 | counter++ 32 | withContext(UI) { 33 | text_field.text = counter.toString() 34 | } 35 | } 36 | } 37 | 38 | cancel_btn.setOnClickListener { 39 | job.cancel() 40 | } 41 | 42 | 43 | } 44 | } 45 | 46 | class LifecycleAwareLazy(lifecycle: Lifecycle, val initializer: () -> T): Lazy, GenericLifecycleObserver { 47 | 48 | init { 49 | lifecycle.addObserver(this) 50 | } 51 | 52 | private object UNINITIALIZED_VALUE 53 | private var _value: Any? = UNINITIALIZED_VALUE 54 | 55 | override val value: T 56 | get() { 57 | if (_value === UNINITIALIZED_VALUE) { 58 | _value = initializer.invoke() 59 | } 60 | return _value as T 61 | } 62 | 63 | override fun isInitialized(): Boolean = _value != UNINITIALIZED_VALUE 64 | 65 | // GenericLifecycleObserver 66 | override fun onStateChanged(source: LifecycleOwner?, event: Lifecycle.Event?) { 67 | when (event) { 68 | Lifecycle.Event.ON_STOP -> { 69 | _value = UNINITIALIZED_VALUE 70 | } 71 | Lifecycle.Event.ON_DESTROY -> { 72 | } 73 | else -> return 74 | } 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /Kotlin-Samples-Android/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 |