Skip to content

Commit

Permalink
create a group with two addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 26, 2024
1 parent ac8a2d5 commit 4fc84ea
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.xmtp.android.example.conversation
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
Expand All @@ -14,12 +15,14 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.launch
import org.xmtp.android.example.R
import org.xmtp.android.example.databinding.BottomSheetNewConversationBinding
import org.xmtp.android.example.databinding.BottomSheetNewGroupBinding
import java.util.regex.Pattern

class NewGroupBottomSheet : BottomSheetDialogFragment() {

private val viewModel: NewConversationViewModel by viewModels()
private var _binding: BottomSheetNewConversationBinding? = null
private var _binding: BottomSheetNewGroupBinding? = null
private val addresses: MutableList<String> = mutableListOf()
private val binding get() = _binding!!

companion object {
Expand All @@ -37,7 +40,7 @@ class NewGroupBottomSheet : BottomSheetDialogFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = BottomSheetNewConversationBinding.inflate(inflater, container, false)
_binding = BottomSheetNewGroupBinding.inflate(inflater, container, false)
return binding.root
}

Expand All @@ -50,12 +53,23 @@ class NewGroupBottomSheet : BottomSheetDialogFragment() {
}
}

binding.addressInput.addTextChangedListener {
binding.addressInput1.addTextChangedListener {
if (viewModel.uiState.value is NewConversationViewModel.UiState.Loading) return@addTextChangedListener
val input = binding.addressInput.text.trim()
val input = binding.addressInput1.text.trim()
val matcher = ADDRESS_PATTERN.matcher(input)
if (matcher.matches()) {
viewModel.createGroup(listOf(input.toString()))
addresses.add(input.toString())
binding.addressInput2.visibility = VISIBLE
}
}

binding.addressInput2.addTextChangedListener {
if (viewModel.uiState.value is NewConversationViewModel.UiState.Loading) return@addTextChangedListener
val input = binding.addressInput1.text.trim()
val matcher = ADDRESS_PATTERN.matcher(input)
if (matcher.matches()) {
addresses.add(input.toString())
viewModel.createGroup(addresses)
}
}
}
Expand All @@ -68,12 +82,14 @@ class NewGroupBottomSheet : BottomSheetDialogFragment() {
private fun ensureUiState(uiState: NewConversationViewModel.UiState) {
when (uiState) {
is NewConversationViewModel.UiState.Error -> {
binding.addressInput.isEnabled = true
binding.addressInput1.isEnabled = true
binding.addressInput2.isEnabled = true
binding.progress.visibility = View.GONE
showError(uiState.message)
}
NewConversationViewModel.UiState.Loading -> {
binding.addressInput.isEnabled = false
binding.addressInput1.isEnabled = false
binding.addressInput2.isEnabled = false
binding.progress.visibility = View.VISIBLE
}
is NewConversationViewModel.UiState.Success -> {
Expand Down
56 changes: 56 additions & 0 deletions example/src/main/res/layout/bottom_sheet_new_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/padding"
android:paddingBottom="48dp">

<ProgressBar
android:id="@+id/progress"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="48dp"
android:layout_height="48dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/addressInput1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:autofillHints="no"
android:hint="@string/enter_address"
android:inputType="text"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/addressInput2"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/addressInput2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:autofillHints="no"
android:hint="@string/enter_address"
android:inputType="text"
android:maxLines="1"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/addressInput1" />

</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

0 comments on commit 4fc84ea

Please sign in to comment.