-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d89f11c
commit ac8a2d5
Showing
12 changed files
with
239 additions
and
44 deletions.
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
97 changes: 97 additions & 0 deletions
97
example/src/main/java/org/xmtp/android/example/conversation/NewGroupBottomSheet.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,97 @@ | ||
package org.xmtp.android.example.conversation | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Toast | ||
import androidx.core.widget.addTextChangedListener | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
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 java.util.regex.Pattern | ||
|
||
class NewGroupBottomSheet : BottomSheetDialogFragment() { | ||
|
||
private val viewModel: NewConversationViewModel by viewModels() | ||
private var _binding: BottomSheetNewConversationBinding? = null | ||
private val binding get() = _binding!! | ||
|
||
companion object { | ||
const val TAG = "NewGroupBottomSheet" | ||
|
||
private val ADDRESS_PATTERN = Pattern.compile("^0x[a-fA-F0-9]{40}\$") | ||
|
||
fun newInstance(): NewGroupBottomSheet { | ||
return NewGroupBottomSheet() | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
_binding = BottomSheetNewConversationBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.uiState.collect(::ensureUiState) | ||
} | ||
} | ||
|
||
binding.addressInput.addTextChangedListener { | ||
if (viewModel.uiState.value is NewConversationViewModel.UiState.Loading) return@addTextChangedListener | ||
val input = binding.addressInput.text.trim() | ||
val matcher = ADDRESS_PATTERN.matcher(input) | ||
if (matcher.matches()) { | ||
viewModel.createGroup(listOf(input.toString())) | ||
} | ||
} | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
private fun ensureUiState(uiState: NewConversationViewModel.UiState) { | ||
when (uiState) { | ||
is NewConversationViewModel.UiState.Error -> { | ||
binding.addressInput.isEnabled = true | ||
binding.progress.visibility = View.GONE | ||
showError(uiState.message) | ||
} | ||
NewConversationViewModel.UiState.Loading -> { | ||
binding.addressInput.isEnabled = false | ||
binding.progress.visibility = View.VISIBLE | ||
} | ||
is NewConversationViewModel.UiState.Success -> { | ||
startActivity( | ||
ConversationDetailActivity.intent( | ||
requireContext(), | ||
topic = uiState.conversation.topic, | ||
peerAddress = uiState.conversation.peerAddress | ||
) | ||
) | ||
dismiss() | ||
} | ||
NewConversationViewModel.UiState.Unknown -> Unit | ||
} | ||
} | ||
|
||
private fun showError(message: String) { | ||
val error = message.ifBlank { resources.getString(R.string.error) } | ||
Toast.makeText(requireContext(), error, Toast.LENGTH_SHORT).show() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M22,9l0,-2l-2,0l0,2l-2,0l0,2l2,0l0,2l2,0l0,-2l2,0l0,-2z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M8,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4S4,5.79 4,8S5.79,12 8,12z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M8,13c-2.67,0 -8,1.34 -8,4v3h16v-3C16,14.34 10.67,13 8,13z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M12.51,4.05C13.43,5.11 14,6.49 14,8s-0.57,2.89 -1.49,3.95C14.47,11.7 16,10.04 16,8S14.47,4.3 12.51,4.05z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M16.53,13.83C17.42,14.66 18,15.7 18,17v3h2v-3C20,15.55 18.41,14.49 16.53,13.83z"/> | ||
</vector> |
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
Oops, something went wrong.