Skip to content

Instantly share code, notes, and snippets.

@javlonrahimov
Last active November 14, 2022 06:08
Show Gist options
  • Save javlonrahimov/23a6ba9eb0dc81dbe151bdf93bc74dd3 to your computer and use it in GitHub Desktop.
Save javlonrahimov/23a6ba9eb0dc81dbe151bdf93bc74dd3 to your computer and use it in GitHub Desktop.
@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment() {
private lateinit var binding: ModalSearchAddressBinding
private val viewModel: MyAddressesViewModel by viewModels({ requireParentFragment() })
private val adapter = SearchAddressAdapter()
var onResult: ((searchAddress: GeocoderAddress?) -> Unit)? = null
override fun getTheme() = R.style.CustomBottomSheetDialogTheme
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.setOnShowListener {
(it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
?.let { view ->
val layoutParams = view.layoutParams
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
view.layoutParams = layoutParams
}
}
return dialog
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = ModalSearchAddressBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.recycler.adapter = adapter
binding.recycler.addDivider(requireContext())
adapter.onAddressClickListener = OnActionListener {
onResult?.invoke(it)
viewModel.setSelectedAddress(it)
findNavController().popBackStack()
}
binding.mapText.setOnClickListener {
findNavController().popBackStack()
}
binding.searchInput.addTextChangedListener {
if (it != null && it.toString().length > 2) {
viewModel.addressToGeocoder(it.toString(), 5)
}
}
}
override fun onResume() {
super.onResume()
viewModel.getGeocodeAddressesState.observe(viewLifecycleOwner, searchResultObserver)
}
private val searchResultObserver = EventObserver<ResultState<List<GeocoderAddress>>> {
when (it) {
is ResultState.Success -> {
adapter.submitList(it.data)
}
else -> {
}
}
}
companion object {
const val TAG = "search_address_dialog"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment