Skip to content

Update Tutorial to SDK 5.2.0 #65

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

Merged
merged 8 commits into from
May 22, 2022
Merged

Update Tutorial to SDK 5.2.0 #65

merged 8 commits into from
May 22, 2022

Conversation

MarinTolic
Copy link
Contributor

@MarinTolic MarinTolic commented May 12, 2022

  • Update tutorial to SDK 5.2.0
  • Update Java tutorial to use OP (part of the 5.2.0 update)
  • Refactor typing updates logic
  • Remove a recursive call in the custom imgur viewholder code

Testing

You can use the following patch to make sure typing updates work in ChannelActivity3 and "ChannelActivity4" in both Kotlin and Java apps.

The following patch will change the user to jc, it shares the channel Glaring Bear with user tutorialdroid

Code in a collapsible block
Index: samplekotlin/src/main/java/com/example/chattutorial/MainActivity.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/samplekotlin/src/main/java/com/example/chattutorial/MainActivity.kt b/samplekotlin/src/main/java/com/example/chattutorial/MainActivity.kt
--- a/samplekotlin/src/main/java/com/example/chattutorial/MainActivity.kt	(revision b306a5c8c93c2c03512f3930ef25195c0a0237d3)
+++ b/samplekotlin/src/main/java/com/example/chattutorial/MainActivity.kt	(date 1652355528185)
@@ -45,13 +45,13 @@
 
         // Step 3 - Authenticate and connect the user
         val user = User(
-            id = "tutorial-droid",
-            name = "Tutorial Droid",
+            id = "jc",
+            name = "jc",
             image = "https://bit.ly/2TIt8NR"
         )
         client.connectUser(
             user = user,
-            token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoidHV0b3JpYWwtZHJvaWQifQ.NhEr0hP9W9nwqV7ZkdShxvi02C5PR7SJE7Cs4y7kyqg"
+            token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiamMifQ.NgHlRrxJhvpfLAY3Q1q23oSayLJKTlXUadhD2l0kKWI"
         ).enqueue()
 
         // Step 4 - Set the channel list filter and order

binding.typingHeaderView.setText(nobodyTyping);

// Observe typing events and update typing header depending on its state.
Flow<ChannelState> channelStateFlow = ChatClientExtensions.watchChannelAsState(ChatClient.instance(), cid, 30);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very clunky, but we currently don't expose any of our state classes (ChannelState, GlobalState, ...) as LiveData.

It was either this or suggesting that Java users use Coroutines, which I feel is a bigger unknown in the Java world + the syntax would still be clunky since Flow operators are written as extensions.

// Helper method that transforms typing updates into a string
// containing typing member's names
@NonNull
private String joinTypingUpdatesToUserNames(@NonNull TypingEvent typingEvent) {
Copy link
Contributor Author

@MarinTolic MarinTolic May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to do this manually because String.join() is not compatible with Android API 21 without desugaring.

// Set view factory manager for Imgur attachments
ImgurAttachmentFactory imgurAttachmentFactory = new ImgurAttachmentFactory();

List<ImgurAttachmentFactory> imgurAttachmentViewFactories = new ArrayList<ImgurAttachmentFactory>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, List.of() not supported by API 21 without desugaring, making this feel clunky.

override fun createViewHolder(
message: Message,
listeners: MessageListListenerContainer?,
parent: ViewGroup
): InnerAttachmentViewHolder {
val imgurAttachment = message.attachments.firstOrNull { it.isImgurAttachment() }
?: return createViewHolder(message, listeners, parent)
val imgurAttachment = message.attachments.first() { it.isImgurAttachment() }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be safe because canHandle has already made sure that this attachment exists.

Also previously this was a recursive call.

@@ -95,7 +96,10 @@ class ChannelActivity3 : AppCompatActivity() {

// Observe typing events and update typing header depending on its state.
lifecycleScope.launchWhenStarted {
ChatClient.instance().globalState.typingUpdates.collect {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GlobalState should not be used here. It would provide typing updates not for a particular channel but for all active channels.

In order to get updates for a particular active channel we have to watch it first and collect the resulting ChannelState.

@MarinTolic MarinTolic requested review from skydoves and filbabic May 12, 2022 11:03
…e imports for Stream resources because shortened imports confuse the IDE when copy pasting
@@ -47,12 +47,12 @@ class ImgurAttachmentFactory : AttachmentFactory {
binding.ivMediaThumb.apply {
shapeAppearanceModel = shapeAppearanceModel
.toBuilder()
.setAllCornerSizes(resources.getDimension(R.dimen.stream_ui_selected_attachment_corner_radius))
.setAllCornerSizes(resources.getDimension(io.getstream.chat.android.ui.R.dimen.stream_ui_selected_attachment_corner_radius))
Copy link
Contributor Author

@MarinTolic MarinTolic May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using fully classified names seems to throw the IDE off when copy pasting from our tutorial or even another IDE where the import is working.

Classifying it fully fixes that and should result in the users a smoother experience.

Copy link
Contributor

@skydoves skydoves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 💯

@filbabic filbabic removed their request for review May 17, 2022 09:16
@skydoves skydoves marked this pull request as ready for review May 22, 2022 23:14
@skydoves skydoves merged commit 7f1049e into main May 22, 2022
@skydoves skydoves deleted the update/5.2.0 branch May 22, 2022 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants