-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LH-112] Add add question fragment #16
- Loading branch information
1 parent
f83d451
commit 718a124
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
feature/board/src/main/java/com/lighthouse/board/view/AddFragment.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,90 @@ | ||
package com.lighthouse.board.view | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.chip.Chip | ||
import com.google.android.material.chip.ChipGroup | ||
import com.lighthouse.android.common_ui.constant.toast | ||
import com.lighthouse.board.R | ||
import com.lighthouse.board.databinding.FragmentAddBinding | ||
import com.lighthouse.board.viewmodel.BoardViewModel | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.launch | ||
|
||
@AndroidEntryPoint | ||
class AddFragment : Fragment() { | ||
private val viewModel: BoardViewModel by viewModels() | ||
private lateinit var binding: FragmentAddBinding | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_add, container, false) | ||
initBack() | ||
initAddButton() | ||
|
||
val interestList = listOf( | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.cooking), | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.game), | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.movie), | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.music), | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.travel), | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.study), | ||
) | ||
|
||
addChipToGroup(binding.chipInterest, interestList) | ||
|
||
return binding.root | ||
} | ||
|
||
private fun initBack() { | ||
binding.btnBack.setOnClickListener { | ||
requireActivity().supportFragmentManager.popBackStack() | ||
} | ||
} | ||
|
||
private fun addChipToGroup(chipGroup: ChipGroup, interestList: List<String>) { | ||
val inflater = LayoutInflater.from(context) | ||
interestList.forEach { | ||
val chip = inflater.inflate( | ||
com.lighthouse.android.common_ui.R.layout.chip, binding.chipInterest, false | ||
) as Chip | ||
|
||
chip.text = it | ||
chip.isCloseIconVisible = false | ||
if (it == interestList[0]) { | ||
chip.isChecked = true | ||
} | ||
|
||
chipGroup.isSingleSelection = true | ||
chipGroup.isSelectionRequired = true | ||
chipGroup.addView(chip) | ||
} | ||
} | ||
|
||
private fun initAddButton() { | ||
binding.btnAdd.setOnClickListener { | ||
val text = binding.etQuestion.text.toString() | ||
if (text.length <= 10 || text.length >= 200) { | ||
val msg = | ||
requireContext().resources.getString(com.lighthouse.android.common_ui.R.string.question_size_error) | ||
context.toast(msg) | ||
} else { | ||
val categoryId = binding.chipInterest.checkedChipId - 2 | ||
viewLifecycleOwner.lifecycleScope.launch { | ||
viewModel.uploadQuestion(1, categoryId, text).collect { | ||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |