Skip to content

Commit 749753a

Browse files
Merge pull request #22 from GetStream/clean_up_and_suggestions
Clean up and readability
2 parents 09da996 + fb97611 commit 749753a

File tree

15 files changed

+162
-226
lines changed

15 files changed

+162
-226
lines changed

build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import com.example.chattutorial.Dependencies
21

32
buildscript {
43
repositories {
@@ -7,8 +6,8 @@ buildscript {
76

87
}
98
dependencies {
10-
classpath Dependencies.androidGradlePlugin
11-
classpath Dependencies.kotlinGradlePlugin
9+
classpath 'com.android.tools.build:gradle:4.1.0'
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
1211
}
1312
}
1413

buildSrc/build.gradle.kts

Lines changed: 0 additions & 7 deletions
This file was deleted.

buildSrc/src/main/kotlin/com/example/chattutorial/Configuration.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

buildSrc/src/main/kotlin/com/example/chattutorial/Dependencies.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Feb 11 11:52:35 EET 2020
1+
#Wed Oct 28 11:57:55 MDT 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

samplejava/build.gradle

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import com.example.chattutorial.Dependencies
2-
import com.example.chattutorial.Configuration
31

42
apply plugin: 'com.android.application'
53
apply plugin: 'kotlin-android'
64
apply plugin: 'kotlin-android-extensions'
75

86
android {
9-
compileSdkVersion Configuration.compileSdkVersion
7+
compileSdkVersion 29
108
defaultConfig {
119
applicationId "com.example.chattutorial"
12-
minSdkVersion Configuration.minSdkVersion
13-
targetSdkVersion Configuration.targetSdkVersion
14-
versionCode Configuration.versionCode
15-
versionName Configuration.versionName
16-
ndkVersion Configuration.ndkVersion
10+
minSdkVersion 21
11+
targetSdkVersion 30
12+
versionCode 1
13+
versionName "1.0"
14+
ndkVersion "21.3.6528147"
1715
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1816
}
1917

@@ -35,11 +33,12 @@ android {
3533
}
3634

3735
dependencies {
38-
implementation Dependencies.kotlinStdLib
36+
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10"
37+
implementation "com.github.getstream:stream-chat-android:4.3.1-beta-3"
38+
implementation "io.coil-kt:coil:1.0.0"
3939

40-
implementation Dependencies.lifecycleViewModel
41-
implementation Dependencies.streamAndroid
42-
implementation Dependencies.coil
43-
implementation Dependencies.appCompat
44-
implementation Dependencies.constraintLayout
40+
implementation "androidx.lifecycle:lifecycle-viewmodel:2.3.0-beta1"
41+
implementation "androidx.appcompat:appcompat:1.2.0"
42+
implementation "androidx.constraintlayout:constraintlayout:2.0.2"
43+
implementation 'androidx.activity:activity-ktx:1.1.0'
4544
}

samplekotlin/build.gradle

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import com.example.chattutorial.Dependencies
2-
import com.example.chattutorial.Configuration
31

42
apply plugin: 'com.android.application'
53
apply plugin: 'kotlin-android'
64
apply plugin: 'kotlin-android-extensions'
75

86
android {
9-
compileSdkVersion Configuration.compileSdkVersion
7+
compileSdkVersion 29
108
defaultConfig {
119
applicationId "com.example.chattutorial"
12-
minSdkVersion Configuration.minSdkVersion
13-
targetSdkVersion Configuration.targetSdkVersion
14-
versionCode Configuration.versionCode
15-
versionName Configuration.versionName
16-
ndkVersion Configuration.ndkVersion
10+
minSdkVersion 21
11+
targetSdkVersion 30
12+
versionCode 1
13+
versionName "1.0"
14+
ndkVersion "21.3.6528147"
1715
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1816
}
17+
1918
buildTypes {
2019
release {
2120
minifyEnabled false
@@ -24,9 +23,9 @@ android {
2423
}
2524

2625
compileOptions {
27-
sourceCompatibility JavaVersion.VERSION_1_8
2826
targetCompatibility JavaVersion.VERSION_1_8
2927
}
28+
3029
kotlinOptions {
3130
jvmTarget = JavaVersion.VERSION_1_8.toString()
3231
}
@@ -37,11 +36,12 @@ android {
3736
}
3837

3938
dependencies {
40-
implementation Dependencies.kotlinStdLib
39+
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10"
40+
implementation "com.github.getstream:stream-chat-android:4.3.1-beta-3"
41+
implementation "io.coil-kt:coil:1.0.0"
4142

42-
implementation Dependencies.lifecycleViewModel
43-
implementation Dependencies.streamAndroid
44-
implementation Dependencies.coil
45-
implementation Dependencies.appCompat
46-
implementation Dependencies.constraintLayout
43+
implementation "androidx.lifecycle:lifecycle-viewmodel:2.3.0-beta1"
44+
implementation "androidx.appcompat:appcompat:1.2.0"
45+
implementation "androidx.constraintlayout:constraintlayout:2.0.2"
46+
implementation 'androidx.activity:activity-ktx:1.1.0'
4747
}

samplekotlin/src/main/java/com/example/chattutorial/ChannelActivity.kt

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ package com.example.chattutorial
33
import android.content.Context
44
import android.content.Intent
55
import android.os.Bundle
6-
import androidx.activity.OnBackPressedCallback
6+
import androidx.activity.addCallback
7+
import androidx.activity.viewModels
78
import androidx.appcompat.app.AppCompatActivity
8-
import androidx.lifecycle.ViewModelProvider
99
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel
1010
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel
1111
import com.getstream.sdk.chat.viewmodel.bindView
1212
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory
1313
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel
1414
import com.getstream.sdk.chat.viewmodel.messages.bindView
1515
import io.getstream.chat.android.client.models.Channel
16-
import kotlinx.android.synthetic.main.activity_channel.channelHeaderView
17-
import kotlinx.android.synthetic.main.activity_channel.messageInputView
18-
import kotlinx.android.synthetic.main.activity_channel.messageListView
19-
16+
import kotlinx.android.synthetic.main.activity_channel.*
2017

2118
class ChannelActivity : AppCompatActivity(R.layout.activity_channel) {
2219

2320
override fun onCreate(savedInstanceState: Bundle?) {
2421
super.onCreate(savedInstanceState)
25-
val cid = checkNotNull(intent.getStringExtra(CID_KEY)) {"Specifying a channel id is required when starting ChannelActivity"}
22+
val cid = checkNotNull(intent.getStringExtra(CID_KEY)) {
23+
"Specifying a channel id is required when starting ChannelActivity"
24+
}
2625

2726
// step 1 - we create 3 separate ViewModels for the views so it's easy to customize one of the components
28-
val viewModelProvider = ViewModelProvider(this, ChannelViewModelFactory(cid))
29-
val channelHeaderViewModel = viewModelProvider.get(ChannelHeaderViewModel::class.java)
30-
val messageListViewModel = viewModelProvider.get(MessageListViewModel::class.java)
31-
val messageInputViewModel = viewModelProvider.get(MessageInputViewModel::class.java)
27+
val factory = ChannelViewModelFactory(cid)
28+
val channelHeaderViewModel: ChannelHeaderViewModel by viewModels { factory }
29+
val messageListViewModel: MessageListViewModel by viewModels { factory }
30+
val messageInputViewModel: MessageInputViewModel by viewModels { factory }
3231

3332
// TODO set custom AttachmentViewHolderFactory
3433

@@ -59,27 +58,19 @@ class ChannelActivity : AppCompatActivity(R.layout.activity_channel) {
5958
}
6059

6160
// step 6 - handle back button behaviour correctly when you're in a thread
62-
val backButtonHandler = {
61+
channelHeaderView.onBackClick = {
6362
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
6463
}
65-
channelHeaderView.onBackClick = { backButtonHandler() }
6664

67-
onBackPressedDispatcher.addCallback(
68-
this,
69-
object : OnBackPressedCallback(true) {
70-
override fun handleOnBackPressed() {
71-
backButtonHandler()
72-
}
73-
}
74-
)
65+
onBackPressedDispatcher.addCallback(this) {
66+
channelHeaderView.onBackClick()
67+
}
7568
}
7669

7770
companion object {
7871
private const val CID_KEY = "key:cid"
7972

80-
fun newIntent(context: Context, channel: Channel) =
81-
Intent(context, ChannelActivity::class.java).apply {
82-
putExtra(CID_KEY, channel.cid)
83-
}
73+
fun newIntent(context: Context, channel: Channel): Intent =
74+
Intent(context, ChannelActivity::class.java).putExtra(CID_KEY, channel.cid)
8475
}
8576
}

samplekotlin/src/main/java/com/example/chattutorial/ChannelActivity2.kt

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ package com.example.chattutorial
33
import android.content.Context
44
import android.content.Intent
55
import android.os.Bundle
6-
import androidx.activity.OnBackPressedCallback
6+
import androidx.activity.addCallback
7+
import androidx.activity.viewModels
78
import androidx.appcompat.app.AppCompatActivity
8-
import androidx.lifecycle.ViewModelProvider
99
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel
1010
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel
1111
import com.getstream.sdk.chat.viewmodel.bindView
1212
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory
1313
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel
1414
import com.getstream.sdk.chat.viewmodel.messages.bindView
1515
import io.getstream.chat.android.client.models.Channel
16-
import kotlinx.android.synthetic.main.activity_channel.channelHeaderView
17-
import kotlinx.android.synthetic.main.activity_channel.messageInputView
18-
import kotlinx.android.synthetic.main.activity_channel.messageListView
19-
16+
import kotlinx.android.synthetic.main.activity_channel.*
2017

2118
class ChannelActivity2 : AppCompatActivity(R.layout.activity_channel_2) {
2219

2320
override fun onCreate(savedInstanceState: Bundle?) {
2421
super.onCreate(savedInstanceState)
25-
val cid = checkNotNull(intent.getStringExtra(CID_KEY)) {"Specifying a channel id is required when starting ChannelActivity"}
22+
val cid = checkNotNull(intent.getStringExtra(CID_KEY)) {
23+
"Specifying a channel id is required when starting ChannelActivity"
24+
}
2625

2726
// step 1 - we create 3 separate ViewModels for the views so it's easy to customize one of the components
28-
val viewModelProvider = ViewModelProvider(this, ChannelViewModelFactory(cid))
29-
val channelHeaderViewModel = viewModelProvider.get(ChannelHeaderViewModel::class.java)
30-
val messageListViewModel = viewModelProvider.get(MessageListViewModel::class.java)
31-
val messageInputViewModel = viewModelProvider.get(MessageInputViewModel::class.java)
27+
val factory = ChannelViewModelFactory(cid)
28+
val channelHeaderViewModel: ChannelHeaderViewModel by viewModels { factory }
29+
val messageListViewModel: MessageListViewModel by viewModels { factory }
30+
val messageInputViewModel: MessageInputViewModel by viewModels { factory }
3231

3332
// set custom AttachmentViewHolderFactory
3433
messageListView.setAttachmentViewHolderFactory(MyAttachmentViewHolderFactory())
@@ -60,27 +59,19 @@ class ChannelActivity2 : AppCompatActivity(R.layout.activity_channel_2) {
6059
}
6160

6261
// step 6 - handle back button behaviour correctly when you're in a thread
63-
val backButtonHandler = {
62+
channelHeaderView.onBackClick = {
6463
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
6564
}
66-
channelHeaderView.onBackClick = { backButtonHandler() }
6765

68-
onBackPressedDispatcher.addCallback(
69-
this,
70-
object : OnBackPressedCallback(true) {
71-
override fun handleOnBackPressed() {
72-
backButtonHandler()
73-
}
74-
}
75-
)
66+
onBackPressedDispatcher.addCallback(this) {
67+
channelHeaderView.onBackClick()
68+
}
7669
}
7770

7871
companion object {
7972
private const val CID_KEY = "key:cid"
8073

81-
fun newIntent(context: Context, channel: Channel) =
82-
Intent(context, ChannelActivity2::class.java).apply {
83-
putExtra(CID_KEY, channel.cid)
84-
}
74+
fun newIntent(context: Context, channel: Channel): Intent =
75+
Intent(context, ChannelActivity2::class.java).putExtra(CID_KEY, channel.cid)
8576
}
8677
}

0 commit comments

Comments
 (0)