|
1 | 1 | package com.project200.feature.matching.place |
2 | 2 |
|
| 3 | +import android.os.Bundle |
| 4 | +import android.view.LayoutInflater |
3 | 5 | import android.view.View |
| 6 | +import android.view.ViewGroup |
4 | 7 | import android.widget.Toast |
5 | | -import androidx.core.widget.doAfterTextChanged |
| 8 | +import androidx.compose.runtime.getValue |
| 9 | +import androidx.compose.ui.platform.ComposeView |
| 10 | +import androidx.fragment.app.Fragment |
6 | 11 | import androidx.fragment.app.viewModels |
| 12 | +import androidx.lifecycle.Lifecycle |
| 13 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 14 | +import androidx.lifecycle.lifecycleScope |
| 15 | +import androidx.lifecycle.repeatOnLifecycle |
7 | 16 | import androidx.navigation.fragment.findNavController |
8 | 17 | import androidx.navigation.fragment.navArgs |
9 | 18 | import com.project200.domain.model.BaseResult |
10 | | -import com.project200.presentation.base.BindingFragment |
| 19 | +import com.project200.presentation.compose.applyAppTheme |
11 | 20 | import com.project200.undabang.feature.matching.R |
12 | | -import com.project200.undabang.feature.matching.databinding.FragmentExercisePlaceRegisterBinding |
13 | 21 | import dagger.hilt.android.AndroidEntryPoint |
| 22 | +import kotlinx.coroutines.launch |
14 | 23 |
|
15 | | -@AndroidEntryPoint // Hilt 어노테이션 추가 |
16 | | -class ExercisePlaceRegisterFragment : BindingFragment<FragmentExercisePlaceRegisterBinding> (R.layout.fragment_exercise_place_register) { |
| 24 | +@AndroidEntryPoint |
| 25 | +class ExercisePlaceRegisterFragment : Fragment() { |
17 | 26 | private val viewModel: ExercisePlaceRegisterViewModel by viewModels() |
18 | 27 | private val args: ExercisePlaceRegisterFragmentArgs by navArgs() |
19 | 28 |
|
20 | | - override fun getViewBinding(view: View): FragmentExercisePlaceRegisterBinding { |
21 | | - return FragmentExercisePlaceRegisterBinding.bind(view) |
22 | | - } |
23 | | - |
24 | | - override fun setupViews() { |
25 | | - binding.baseToolbar.apply { |
26 | | - setTitle(getString(R.string.exercise_place_register_title)) |
27 | | - showBackButton(true) { findNavController().navigateUp() } |
28 | | - } |
29 | | - binding.placeAddressTv.text = args.address |
30 | | - |
| 29 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 30 | + super.onCreate(savedInstanceState) |
31 | 31 | viewModel.initializePlaceInfo( |
32 | 32 | id = args.placeId, |
33 | 33 | placeName = args.name, |
34 | 34 | placeAddress = args.address, |
35 | 35 | latitude = args.latitude.toDouble(), |
36 | 36 | longitude = args.longitude.toDouble(), |
37 | 37 | ) |
38 | | - binding.placeNameEt.setText(args.name) |
39 | | - setupListeners() |
40 | 38 | } |
41 | 39 |
|
42 | | - private fun setupListeners() { |
43 | | - binding.placeNameEt.doAfterTextChanged { text -> |
44 | | - viewModel.onPlaceNameChanged(text.toString()) |
45 | | - } |
46 | | - |
47 | | - binding.registerBtn.setOnClickListener { |
48 | | - if (binding.placeNameEt.text.toString().isEmpty()) { |
49 | | - Toast.makeText(requireContext(), R.string.empty_place_name, Toast.LENGTH_SHORT).show() |
| 40 | + override fun onCreateView( |
| 41 | + inflater: LayoutInflater, |
| 42 | + container: ViewGroup?, |
| 43 | + savedInstanceState: Bundle?, |
| 44 | + ): View = |
| 45 | + ComposeView(requireContext()).apply { |
| 46 | + applyAppTheme { |
| 47 | + val placeNameInput by viewModel.customPlaceName.collectAsStateWithLifecycle() |
| 48 | + val placeAddress by viewModel.placeAddress.collectAsStateWithLifecycle() |
| 49 | + ExercisePlaceRegisterScreen( |
| 50 | + placeName = if (placeNameInput.isBlank()) getString(R.string.place_name_hint) else placeNameInput, |
| 51 | + placeAddress = placeAddress, |
| 52 | + placeNameInput = placeNameInput, |
| 53 | + onPlaceNameChange = viewModel::onPlaceNameChanged, |
| 54 | + onRegisterClick = { |
| 55 | + if (placeNameInput.isBlank()) { |
| 56 | + Toast.makeText(requireContext(), R.string.empty_place_name, Toast.LENGTH_SHORT).show() |
| 57 | + } |
| 58 | + viewModel.confirmExercisePlace() |
| 59 | + }, |
| 60 | + onBackClick = { findNavController().navigateUp() }, |
| 61 | + ) |
50 | 62 | } |
51 | | - viewModel.confirmExercisePlace() |
52 | 63 | } |
53 | | - } |
54 | 64 |
|
55 | | - override fun setupObservers() { |
56 | | - viewModel.customPlaceName.observe(viewLifecycleOwner) { name -> |
57 | | - binding.placeNameTv.text = if (name.isBlank()) getString(R.string.place_name_hint) else name |
58 | | - } |
59 | | - |
60 | | - viewModel.registrationResult.observe(viewLifecycleOwner) { result -> |
61 | | - when (result) { |
62 | | - is BaseResult.Success -> { |
63 | | - Toast.makeText(requireContext(), getString(R.string.success_register_place), Toast.LENGTH_SHORT).show() |
64 | | - findNavController().navigate(R.id.action_complete_registration_and_go_to_place_list) |
65 | | - } |
66 | | - is BaseResult.Error -> { |
67 | | - Toast.makeText(requireContext(), result.message, Toast.LENGTH_SHORT).show() |
68 | | - } |
69 | | - } |
70 | | - } |
| 65 | + override fun onViewCreated( |
| 66 | + view: View, |
| 67 | + savedInstanceState: Bundle?, |
| 68 | + ) { |
| 69 | + super.onViewCreated(view, savedInstanceState) |
71 | 70 |
|
72 | | - viewModel.editResult.observe(viewLifecycleOwner) { result -> |
73 | | - when (result) { |
74 | | - is BaseResult.Success -> { |
75 | | - Toast.makeText(requireContext(), getString(R.string.success_edit_place), Toast.LENGTH_SHORT).show() |
76 | | - findNavController().navigate(R.id.action_complete_registration_and_go_to_place_list) |
| 71 | + viewLifecycleOwner.lifecycleScope.launch { |
| 72 | + repeatOnLifecycle(Lifecycle.State.STARTED) { |
| 73 | + launch { |
| 74 | + viewModel.registrationResult.collect { result -> |
| 75 | + when (result) { |
| 76 | + is BaseResult.Success -> { |
| 77 | + Toast.makeText(requireContext(), getString(R.string.success_register_place), Toast.LENGTH_SHORT).show() |
| 78 | + findNavController().navigate(R.id.action_complete_registration_and_go_to_place_list) |
| 79 | + } |
| 80 | + is BaseResult.Error -> { |
| 81 | + Toast.makeText(requireContext(), result.message, Toast.LENGTH_SHORT).show() |
| 82 | + } |
| 83 | + } |
| 84 | + } |
77 | 85 | } |
78 | | - is BaseResult.Error -> { |
79 | | - Toast.makeText(requireContext(), R.string.error_fail_to_register_place, Toast.LENGTH_SHORT).show() |
| 86 | + launch { |
| 87 | + viewModel.editResult.collect { result -> |
| 88 | + when (result) { |
| 89 | + is BaseResult.Success -> { |
| 90 | + Toast.makeText(requireContext(), getString(R.string.success_edit_place), Toast.LENGTH_SHORT).show() |
| 91 | + findNavController().navigate(R.id.action_complete_registration_and_go_to_place_list) |
| 92 | + } |
| 93 | + is BaseResult.Error -> { |
| 94 | + Toast.makeText(requireContext(), R.string.error_fail_to_register_place, Toast.LENGTH_SHORT).show() |
| 95 | + } |
| 96 | + } |
| 97 | + } |
80 | 98 | } |
81 | 99 | } |
82 | 100 | } |
|
0 commit comments