-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Basic Chat Support #224
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ed25cc3
Adding chat initial
KevinSchildhorn f3b6834
adding chat view
KevinSchildhorn f2aadec
Merge branch 'ks/FirebaseAuth' into ks/ChatSupport
KevinSchildhorn a76182f
Moving Chat Content
KevinSchildhorn 08821a6
Fixing Channel issues
KevinSchildhorn 4a54dd1
Update ChatViewAndroid.kt
KevinSchildhorn dc27294
Update ChatManager.kt
KevinSchildhorn 0e4f224
Update README.md
KevinSchildhorn 3aafd9e
hiding stream api key and moving Channel View to Compose
KevinSchildhorn c586eab
Updating SwiftUI for Stream
KevinSchildhorn 1fa4ecc
Update CUSTOMIZING.md
KevinSchildhorn 14ed2c8
Update ComposeRootController.kt
KevinSchildhorn f89d8d4
Adding Compose Support
KevinSchildhorn 4616e31
Getting key from plist
KevinSchildhorn d0901c9
Adding joining default channels
KevinSchildhorn 8aa3dd9
Update ChatManager.swift
KevinSchildhorn a4c3025
updating documentation
KevinSchildhorn 58aced5
Update ChatManager.swift
KevinSchildhorn edb20b0
Update ChatManager.swift
KevinSchildhorn cbe1e0d
Updating to used suggested Activity
KevinSchildhorn 413ad46
Update ChannelActivity.kt
KevinSchildhorn 8c78a0b
Update build.gradle.kts
KevinSchildhorn 5c220cd
formatting
KevinSchildhorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
android/src/main/java/co/touchlab/droidcon/android/chat/ChannelActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package co.touchlab.droidcon.android.chat | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import io.getstream.chat.android.compose.ui.messages.MessagesScreen | ||
import io.getstream.chat.android.compose.ui.theme.ChatTheme | ||
import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFactory | ||
|
||
class ChannelActivity : ComponentActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val channelId = intent.getStringExtra(KEY_CHANNEL_ID)!! | ||
actionBar?.hide() | ||
|
||
setContent { | ||
ChatTheme { | ||
MessagesScreen( | ||
viewModelFactory = MessagesViewModelFactory( | ||
context = this, | ||
channelId = channelId, | ||
messageLimit = 30 | ||
), | ||
onBackPressed = { finish() } | ||
) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val KEY_CHANNEL_ID = "channelId" | ||
|
||
fun getIntent(context: Context, channelId: String): Intent { | ||
return Intent(context, ChannelActivity::class.java).apply { | ||
putExtra(KEY_CHANNEL_ID, channelId) | ||
} | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
android/src/main/java/co/touchlab/droidcon/android/chat/ChatManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package co.touchlab.droidcon.android.chat | ||
|
||
import android.content.Context | ||
import co.touchlab.droidcon.BuildConfig | ||
import co.touchlab.droidcon.UserData | ||
import co.touchlab.kermit.Logger | ||
import io.getstream.chat.android.client.ChatClient | ||
import io.getstream.chat.android.client.logger.ChatLogLevel | ||
import io.getstream.chat.android.models.User | ||
import io.getstream.chat.android.offline.plugin.factory.StreamOfflinePluginFactory | ||
import io.getstream.chat.android.state.plugin.config.StatePluginConfig | ||
import io.getstream.chat.android.state.plugin.factory.StreamStatePluginFactory | ||
import io.getstream.result.call.enqueue | ||
|
||
object ChatManager { | ||
private val chatLogger: Logger = Logger.withTag("ChatManager") | ||
|
||
var isConnected: Boolean = false | ||
private set | ||
|
||
fun initChat( | ||
applicationContext: Context, | ||
userData: UserData, | ||
) { | ||
if (!isConnected) { | ||
chatLogger.i { "Initializing Chat" } | ||
val offlinePluginFactory = StreamOfflinePluginFactory(appContext = applicationContext) | ||
val statePluginFactory = | ||
StreamStatePluginFactory( | ||
config = StatePluginConfig(), | ||
appContext = applicationContext | ||
) | ||
|
||
val client = ChatClient.Builder(BuildConfig.STREAM_API_KEY, applicationContext) | ||
.withPlugins(offlinePluginFactory, statePluginFactory) | ||
.logLevel(ChatLogLevel.ALL) // TODO: Set to NOTHING in prod | ||
.build() | ||
|
||
chatLogger.v { "Built the client, adding user" } | ||
val user = User( | ||
id = userData.id, | ||
name = userData.name ?: "Unknown Name", | ||
image = userData.pictureUrl ?: "", | ||
) | ||
val token = client.devToken(user.id) // TODO: Replace with Token from backend | ||
|
||
chatLogger.v { "Connecting User" } | ||
client.connectUser(user = user, token = token).enqueue( | ||
onSuccess = { | ||
chatLogger.v { "Successfully Connected!" } | ||
isConnected = true | ||
joinDefaultChannels(user.id) | ||
}, | ||
onError = { | ||
chatLogger.e { "Error Connecting! $it" } | ||
isConnected = false | ||
} | ||
) | ||
} | ||
} | ||
|
||
private fun joinDefaultChannels(id: String) { | ||
chatLogger.i { "Joining the General Channel" } | ||
|
||
// TODO: Make the Auto-Join Channel(s) configurable | ||
val channelClient = ChatClient.instance().channel("messaging", "general") | ||
channelClient.addMembers(listOf(id)).enqueue( | ||
onSuccess = { | ||
chatLogger.v { "Successfully Joined The General channel" } | ||
}, | ||
onError = { | ||
chatLogger.e { "Error Joining the General channel $it" } | ||
}) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
android/src/main/java/co/touchlab/droidcon/android/chat/ChatView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package co.touchlab.droidcon.android.chat | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.core.content.ContextCompat.startActivity | ||
import co.touchlab.droidcon.R | ||
import io.getstream.chat.android.client.ChatClient | ||
import io.getstream.chat.android.compose.ui.channels.ChannelsScreen | ||
import io.getstream.chat.android.compose.ui.theme.ChatTheme | ||
import io.getstream.chat.android.models.InitializationState | ||
|
||
@Composable | ||
internal fun ChatView() { | ||
val context: Context = LocalContext.current | ||
|
||
Column(modifier = Modifier.fillMaxSize()) { | ||
ChatTheme { | ||
val client = ChatClient.instance() | ||
val clientInitialisationState by client.clientState.initializationState.collectAsState() | ||
|
||
when (clientInitialisationState) { | ||
InitializationState.COMPLETE -> { | ||
ChannelsScreen( | ||
title = stringResource(id = R.string.app_name), | ||
isShowingHeader = false, | ||
isShowingSearch = true, | ||
onItemClick = { | ||
startActivity( | ||
context, | ||
ChannelActivity.getIntent(context, it.cid), | ||
Bundle(), | ||
) | ||
}, | ||
onBackPressed = { } | ||
) | ||
} | ||
|
||
InitializationState.INITIALIZING -> { | ||
Text(text = "Initialising...") | ||
} | ||
|
||
InitializationState.NOT_INITIALIZED -> { | ||
Text(text = "Not initialized...") | ||
} | ||
Comment on lines
+51
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add an "action" to initialize or retry on failure? |
||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the chat only enabled for authenticated people? If yes, should we create a placeholder screen for the chat tab?
Reason: if I'm not authenticated, the app crashes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic does not update the current user when switching accounts.