Skip to content

Commit 09da996

Browse files
Merge pull request #21 from GetStream/fix/issues
Fix back button handling
2 parents 97b0b91 + 0034afa commit 09da996

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ class ChannelActivity : AppCompatActivity(R.layout.activity_channel) {
4444
is MessageListViewModel.Mode.Normal -> messageInputViewModel.resetThread()
4545
}
4646
}
47-
// step 4 - let the message input know when we are editing a message
47+
48+
// step 4 - handle navigate up state
49+
messageListViewModel.state.observe(this) {
50+
when (it) {
51+
is MessageListViewModel.State.NavigateUp -> finish()
52+
else -> Unit
53+
}
54+
}
55+
56+
// step 5 - let the message input know when we are editing a message
4857
messageListView.setOnMessageEditHandler {
4958
messageInputViewModel.editMessage.postValue(it)
5059
}
5160

52-
// step 5 - handle back button behaviour correctly when you're in a thread
61+
// step 6 - handle back button behaviour correctly when you're in a thread
5362
val backButtonHandler = {
5463
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
5564
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,21 @@ class ChannelActivity2 : AppCompatActivity(R.layout.activity_channel_2) {
4545
is MessageListViewModel.Mode.Normal -> messageInputViewModel.resetThread()
4646
}
4747
}
48-
// step 4 - let the message input know when we are editing a message
48+
49+
// step 4 - handle navigate up state
50+
messageListViewModel.state.observe(this) {
51+
when (it) {
52+
is MessageListViewModel.State.NavigateUp -> finish()
53+
else -> Unit
54+
}
55+
}
56+
57+
// step 5 - let the message input know when we are editing a message
4958
messageListView.setOnMessageEditHandler {
5059
messageInputViewModel.editMessage.postValue(it)
5160
}
5261

53-
// step 5 - handle back button behaviour correctly when you're in a thread
62+
// step 6 - handle back button behaviour correctly when you're in a thread
5463
val backButtonHandler = {
5564
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
5665
}
@@ -70,7 +79,7 @@ class ChannelActivity2 : AppCompatActivity(R.layout.activity_channel_2) {
7079
private const val CID_KEY = "key:cid"
7180

7281
fun newIntent(context: Context, channel: Channel) =
73-
Intent(context, ChannelActivity::class.java).apply {
82+
Intent(context, ChannelActivity2::class.java).apply {
7483
putExtra(CID_KEY, channel.cid)
7584
}
7685
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ class ChannelActivity3 : AppCompatActivity(R.layout.activity_channel_3) {
4949
is MessageListViewModel.Mode.Normal -> messageInputViewModel.resetThread()
5050
}
5151
}
52-
// step 4 - let the message input know when we are editing a message
52+
53+
// step 4 - handle navigate up state
54+
messageListViewModel.state.observe(this) {
55+
when (it) {
56+
is MessageListViewModel.State.NavigateUp -> finish()
57+
else -> Unit
58+
}
59+
}
60+
61+
// step 5 - let the message input know when we are editing a message
5362
messageListView.setOnMessageEditHandler {
5463
messageInputViewModel.editMessage.postValue(it)
5564
}
@@ -65,7 +74,7 @@ class ChannelActivity3 : AppCompatActivity(R.layout.activity_channel_3) {
6574
}
6675
channelController.typing.observe(this, typingObserver)
6776

68-
// step 5 - handle back button behaviour correctly when you're in a thread
77+
// step 6 - handle back button behaviour correctly when you're in a thread
6978
val backButtonHandler = {
7079
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
7180
}
@@ -85,7 +94,7 @@ class ChannelActivity3 : AppCompatActivity(R.layout.activity_channel_3) {
8594
private const val CID_KEY = "key:cid"
8695

8796
fun newIntent(context: Context, channel: Channel) =
88-
Intent(context, ChannelActivity::class.java).apply {
97+
Intent(context, ChannelActivity3::class.java).apply {
8998
putExtra(CID_KEY, channel.cid)
9099
}
91100
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ class ChannelActivity4 : AppCompatActivity(R.layout.activity_channel_4) {
5151
is MessageListViewModel.Mode.Normal -> messageInputViewModel.resetThread()
5252
}
5353
}
54-
// step 4 - let the message input know when we are editing a message
54+
55+
// step 4 - handle navigate up state
56+
messageListViewModel.state.observe(this) {
57+
when (it) {
58+
is MessageListViewModel.State.NavigateUp -> finish()
59+
else -> Unit
60+
}
61+
}
62+
63+
// step 5 - let the message input know when we are editing a message
5564
messageListView.setOnMessageEditHandler {
5665
messageInputViewModel.editMessage.postValue(it)
5766
}
@@ -85,7 +94,7 @@ class ChannelActivity4 : AppCompatActivity(R.layout.activity_channel_4) {
8594
}
8695
currentlyTyping.observe(this, typingObserver)
8796

88-
// step 5 - handle back button behaviour correctly when you're in a thread
97+
// step 6 - handle back button behaviour correctly when you're in a thread
8998
val backButtonHandler = {
9099
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed)
91100
}
@@ -105,7 +114,7 @@ class ChannelActivity4 : AppCompatActivity(R.layout.activity_channel_4) {
105114
private const val CID_KEY = "key:cid"
106115

107116
fun newIntent(context: Context, channel: Channel) =
108-
Intent(context, ChannelActivity::class.java).apply {
117+
Intent(context, ChannelActivity4::class.java).apply {
109118
putExtra(CID_KEY, channel.cid)
110119
}
111120
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.getstream.sdk.chat.viewmodel.channels.ChannelsViewModelImpl
99
import com.getstream.sdk.chat.viewmodel.channels.bindView
1010
import com.getstream.sdk.chat.viewmodel.factory.ChannelsViewModelFactory
1111
import io.getstream.chat.android.client.logger.ChatLogLevel
12-
import io.getstream.chat.android.client.logger.ChatLogger
1312
import io.getstream.chat.android.client.models.Filters
1413
import io.getstream.chat.android.client.models.User
1514
import kotlinx.android.synthetic.main.activity_main.*
@@ -19,8 +18,9 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
1918
super.onCreate(savedInstanceState)
2019

2120
// step 1 - setup the Chat Client
22-
Chat.Builder(apiKey = "b67pax5b2wdq", context = applicationContext).build()
23-
// TODO: set log level to all
21+
Chat.Builder(apiKey = "b67pax5b2wdq", context = applicationContext)
22+
.apply { chatLogLevel = ChatLogLevel.ALL }
23+
.build()
2424

2525
val user = User("summer-brook-2").apply {
2626
extraData["name"] = "Paranoid Android"
@@ -38,13 +38,14 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
3838
Filters.`in`("members", listOf(user.id))
3939
)
4040
val viewModelFactory = ChannelsViewModelFactory(filter, ChannelsViewModel.DEFAULT_SORT)
41-
val viewModel = ViewModelProvider(this, viewModelFactory).get(ChannelsViewModelImpl::class.java)
41+
val viewModel =
42+
ViewModelProvider(this, viewModelFactory).get(ChannelsViewModelImpl::class.java)
4243

4344
// step 4 - connect the ChannelsViewModel to the channelsView, loose coupling make it easy to customize
4445
viewModel.bindView(channelsView, this)
4546
channelsView.setOnChannelClickListener { channel ->
4647
// open the channel activity
47-
startActivity(ChannelActivity3.newIntent(this, channel))
48+
startActivity(ChannelActivity4.newIntent(this, channel))
4849
}
4950
}
5051
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
include ':samplejava', ':samplekotlin'
2-
rootProject.name='doc-sample-android-chat-client-kotlin'
2+
rootProject.name='android-chat-tutorial'
33

44

55
/*

0 commit comments

Comments
 (0)