Skip to content

Commit

Permalink
Replace SearchFragment with SearchDialogFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
alialbaali committed Aug 2, 2021
1 parent 61ca211 commit 8bac650
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,33 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.chatychaty.app.R
import com.chatychaty.app.databinding.FragmentSearchBinding
import com.chatychaty.app.databinding.FragmentDialogSearchBinding
import com.chatychaty.app.util.BaseBottomSheetDialogFragment
import com.chatychaty.app.util.UiState
import com.chatychaty.app.util.snackbar
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
* A simple [Fragment] subclass.
*/
class SearchFragment : Fragment() {
class SearchDialogFragment : BaseBottomSheetDialogFragment() {

private lateinit var binding: FragmentSearchBinding
private lateinit var binding: FragmentDialogSearchBinding

private val viewModel by viewModel<SearchViewModel>()

private val imm by lazy {
requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
private val imm by lazy { requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentSearchBinding.inflate(inflater, container, false).also {
binding = FragmentDialogSearchBinding.inflate(inflater, container, false).also {
it.lifecycleOwner = this
it.viewModel = viewModel
}

binding.tb.navigationIcon?.setTint(resources.getColor(R.color.colorPrimary))

setListeners()
collectState()

Expand All @@ -47,45 +40,36 @@ class SearchFragment : Fragment() {

private fun setListeners() {

binding.tb.setNavigationOnClickListener {
imm.hideSoftInputFromWindow(binding.et.windowToken, 0)
findNavController().navigateUp()
}

binding.et.requestFocus()
binding.etUsername.requestFocus()
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)

binding.et.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
viewModel.createChat()
imm.hideSoftInputFromWindow(v.windowToken, 0)
findNavController().navigate(SearchFragmentDirections.actionSearchFragmentToProgressDialogFragment())
true
} else {
false
}
binding.btnSearch.setOnClickListener {
imm.hideSoftInputFromWindow(binding.etUsername.windowToken, 0)
findNavController().navigate(SearchDialogFragmentDirections.actionSearchDialogFragmentToProgressDialogFragment())
viewModel.createChat()
}
}

private fun collectState() {
val parentFragmentView = requireParentFragment().requireView()
val parentFragmentAnchorView = parentFragmentView.findViewById<FloatingActionButton>(R.id.fab)
viewModel.state
.onEach { state ->
when (state) {

is UiState.Failure -> {
findNavController().navigateUp()
binding.root.snackbar(state.exception.message.toString())
findNavController().navigateUp()
parentFragmentView.snackbar(state.exception.message.toString(), anchorView = parentFragmentAnchorView)
}
is UiState.Loading -> {
}
is UiState.Success -> {
findNavController().navigateUp()
findNavController().navigateUp()
binding.root.snackbar(state.value)
parentFragmentView.snackbar(state.value, anchorView = parentFragmentAnchorView)
}
}
}
.launchIn(lifecycleScope)

}
}
58 changes: 58 additions & 0 deletions app/src/main/res/layout/fragment_dialog_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="viewModel"
type="com.chatychaty.app.search.SearchViewModel" />

</data>

<LinearLayout
android:id="@+id/ll"
style="@style/Dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<View
android:id="@+id/v_head"
style="@style/HeadView"
android:layout_width="25dp"
android:layout_height="3dp"
android:layout_margin="@dimen/spacing_normal" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_name"
style="@style/TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:hint="@string/type_a_username"
app:counterEnabled="true"
app:counterMaxLength="32"
app:endIconMode="clear_text"
app:errorEnabled="true">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_username"
style="@style/TextInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textAutoComplete"
android:text="@={viewModel.username}" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_search"
style="@style/PrimaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:padding="@dimen/spacing_normal"
android:text="@string/search" />

</LinearLayout>
</layout>
62 changes: 0 additions & 62 deletions app/src/main/res/layout/fragment_search.xml

This file was deleted.

16 changes: 8 additions & 8 deletions app/src/main/res/navigation/navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
app:popEnterAnim="@anim/slide_in_down"
app:popExitAnim="@anim/slide_out_up" />
<action
android:id="@+id/action_chatListFragment_to_searchFragment"
app:destination="@id/searchFragment"
android:id="@+id/action_chatListFragment_to_searchDialogFragment"
app:destination="@id/searchDialogFragment"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:launchSingleTop="false"
Expand Down Expand Up @@ -131,15 +131,15 @@
android:name="chatId"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/searchFragment"
android:name="com.chatychaty.app.search.SearchFragment"
<dialog
android:id="@+id/searchDialogFragment"
android:name="com.chatychaty.app.search.SearchDialogFragment"
android:label="SearchFragment"
tools:layout="@layout/fragment_search" >
tools:layout="@layout/fragment_dialog_search" >
<action
android:id="@+id/action_searchFragment_to_progressDialogFragment"
android:id="@+id/action_searchDialogFragment_to_progressDialogFragment"
app:destination="@id/progressDialogFragment" />
</fragment>
</dialog>
<dialog
android:id="@+id/passwordDialogFragment"
android:name="com.chatychaty.app.user.PasswordDialogFragment"
Expand Down

0 comments on commit 8bac650

Please sign in to comment.