Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.3.4 버그 수정 #287

Merged
merged 13 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package boostcamp.and07.mindsync.data.model

import boostcamp.and07.mindsync.ui.util.Dp
import kotlinx.serialization.Serializable

@Serializable
sealed class Node(
open val id: String,
open val parentId: String?,
open val path: NodePath,
open val description: String,
open val children: List<String>,
)
) : java.io.Serializable

data class CircleNode(
override val id: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package boostcamp.and07.mindsync.data.model

import boostcamp.and07.mindsync.ui.util.Dp
import kotlinx.serialization.Serializable

@Serializable
sealed class NodePath(open val centerX: Dp, open val centerY: Dp)

data class RectanglePath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.navArgs
import androidx.navigation.navGraphViewModels
import boostcamp.and07.mindsync.R
import boostcamp.and07.mindsync.data.NodeGenerator
import boostcamp.and07.mindsync.data.crdt.OperationType
import boostcamp.and07.mindsync.data.model.CircleNode
import boostcamp.and07.mindsync.data.model.RectangleNode
import boostcamp.and07.mindsync.databinding.DialogEditDescriptionBinding
import boostcamp.and07.mindsync.ui.mindmap.MindMapViewModel

class EditDescriptionDialog : DialogFragment() {
private var _binding: DialogEditDescriptionBinding? = null
private val binding get() = _binding!!
private lateinit var editDialogInterface: EditDialogInterface

fun setListener(listener: EditDialogInterface) {
editDialogInterface = listener
}
private val mindMapViewModel: MindMapViewModel by navGraphViewModels(R.id.nav_graph)
private val args: EditDescriptionDialogArgs by navArgs()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
Expand All @@ -36,16 +41,46 @@ class EditDescriptionDialog : DialogFragment() {
savedInstanceState: Bundle?,
): View {
_binding = DialogEditDescriptionBinding.inflate(inflater, container, false)
binding.run {
btnCancel.setOnClickListener {
dismiss()
}
btnSubmit.setOnClickListener {
editDialogInterface.onSubmitClick(binding.etNodeDescription.text.toString())
dismiss()
return binding.root
}

override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)
setupCancelBtn()
setupSubmitBtn()
}

private fun setupSubmitBtn() {
binding.btnSubmit.setOnClickListener {
val node = args.node
val description = binding.etNodeDescription.text.toString()
when (args.operation) {
OperationType.ADD -> {
mindMapViewModel.addNode(node, NodeGenerator.makeNode(description, node.id))
}

OperationType.UPDATE -> {
val newNode =
when (node) {
is CircleNode -> node.copy(description = description)
is RectangleNode -> node.copy(description = description)
}
mindMapViewModel.updateNode(newNode)
}

else -> {}
}
dismiss()
}
}

private fun setupCancelBtn() {
binding.btnCancel.setOnClickListener {
dismiss()
}
return binding.root
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MainActivity :
super.onStart()
mainViewModel.fetchProfile()
mainViewModel.getSpaces()
setTitle()
}

override fun init() {
Expand All @@ -52,7 +53,6 @@ class MainActivity :
setSideBarNavigation()
setBinding()
observeEvent()
setTitle()
}

override fun getViewModel(): BaseActivityViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ class MainViewModel

fun getSpaces() {
viewModelScope.launch(coroutineExceptionHandler) {
profileSpaceRepository.getSpaces().collectLatest { spaces ->
profileSpaceRepository.getSpaces().collectLatest { responseSpaces ->
val newSpaces =
responseSpaces.map { space ->
if (space.id == _uiState.value.nowSpace?.id) {
space.copy(isSelected = true)
} else {
space
}
}
_uiState.update { uiState ->
uiState.copy(
spaces = spaces,
spaces = newSpaces,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package boostcamp.and07.mindsync.ui.mindmap

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.navigation.navGraphViewModels
import boostcamp.and07.mindsync.R
import boostcamp.and07.mindsync.data.NodeGenerator
import boostcamp.and07.mindsync.data.model.CircleNode
import boostcamp.and07.mindsync.data.crdt.OperationType
import boostcamp.and07.mindsync.data.model.Node
import boostcamp.and07.mindsync.data.model.RectangleNode
import boostcamp.and07.mindsync.data.model.Tree
import boostcamp.and07.mindsync.data.network.SocketState
import boostcamp.and07.mindsync.databinding.FragmentMindMapBinding
import boostcamp.and07.mindsync.ui.base.BaseFragment
import boostcamp.and07.mindsync.ui.dialog.EditDescriptionDialog
import boostcamp.and07.mindsync.ui.dialog.EditDialogInterface
import boostcamp.and07.mindsync.ui.util.Dp
import boostcamp.and07.mindsync.ui.util.Px
import boostcamp.and07.mindsync.ui.util.ThrottleDuration
Expand All @@ -36,7 +33,9 @@ class MindMapFragment :
NodeClickListener,
TreeUpdateListener,
NodeMoveListener {
private val mindMapViewModel: MindMapViewModel by viewModels()
private val mindMapViewModel: MindMapViewModel by navGraphViewModels(R.id.nav_graph) {
MindMapViewModelFactory()
}
private lateinit var mindMapContainer: MindMapContainer
private val args: MindMapFragmentArgs by navArgs()

Expand Down Expand Up @@ -111,42 +110,39 @@ class MindMapFragment :
}

private fun showDialog(
selectNode: Node,
action: (Node, String) -> Unit,
operationType: OperationType,
selectedNode: Node,
) {
val dialog = EditDescriptionDialog()
dialog.setListener(
object : EditDialogInterface {
override fun onSubmitClick(description: String) {
action.invoke(selectNode, description)
}
},
findNavController().navigate(
MindMapFragmentDirections.actionMindMapFragmentToEditDescriptionDialog(
operationType,
selectedNode,
),
)
dialog.show(requireActivity().supportFragmentManager, "EditDescriptionDialog")
}

private fun setClickEventThrottle() {
with(binding) {
imgbtnMindMapAdd.setClickEvent(lifecycleScope, ThrottleDuration.SHORT_DURATION.duration) {
imgbtnMindMapAdd.setClickEvent(
lifecycleScope,
ThrottleDuration.SHORT_DURATION.duration,
) {
mindMapViewModel.selectedNode.value?.let { selectNode ->
showDialog(selectNode) { parent, description ->
mindMapViewModel.addNode(parent, NodeGenerator.makeNode(description, parent.id))
}
showDialog(OperationType.ADD, selectNode)
}
}
imgbtnMindMapEdit.setClickEvent(lifecycleScope, ThrottleDuration.SHORT_DURATION.duration) {
imgbtnMindMapEdit.setClickEvent(
lifecycleScope,
ThrottleDuration.SHORT_DURATION.duration,
) {
mindMapViewModel.selectedNode.value?.let { selectNode ->
showDialog(selectNode) { node, description ->
val newNode =
when (node) {
is CircleNode -> node.copy(description = description)
is RectangleNode -> node.copy(description = description)
}
mindMapViewModel.updateNode(newNode)
}
showDialog(OperationType.UPDATE, selectNode)
}
}
imgbtnMindMapRemove.setClickEvent(lifecycleScope, ThrottleDuration.SHORT_DURATION.duration) {
imgbtnMindMapRemove.setClickEvent(
lifecycleScope,
ThrottleDuration.SHORT_DURATION.duration,
) {
mindMapViewModel.selectedNode.value?.let { selectNode ->
mindMapViewModel.removeNode(selectNode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import boostcamp.and07.mindsync.data.network.SocketEventType
import boostcamp.and07.mindsync.data.network.SocketState
import boostcamp.and07.mindsync.data.network.response.mindmap.SerializedCrdtTree
import boostcamp.and07.mindsync.data.network.response.mindmap.SerializedOperation
import boostcamp.and07.mindsync.data.repository.mindmap.MindMapRepository
import boostcamp.and07.mindsync.ui.util.Dp
import boostcamp.and07.mindsync.ui.util.ExceptionMessage
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -32,9 +31,7 @@ import javax.inject.Inject
@HiltViewModel
class MindMapViewModel
@Inject
constructor(
private val mindMapRepository: MindMapRepository,
) : ViewModel() {
constructor() : ViewModel() {
private var boardId: String = ""
val crdtTree = CrdtTree(id = IdGenerator.makeRandomNodeId(), tree = Tree())
private var _selectedNode = MutableStateFlow<Node?>(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package boostcamp.and07.mindsync.ui.mindmap

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class MindMapViewModelFactory :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MindMapViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return MindMapViewModel() as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package boostcamp.and07.mindsync.ui.recyclebin

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import boostcamp.and07.mindsync.data.model.Board
import boostcamp.and07.mindsync.databinding.ItemRecycleBoardBinding

class RecycleBinAdapter :
ListAdapter<Board, RecycleBinAdapter.RecycleBinViewHolder>(DIFF_CALLBACK) {
private var clickListener: RecycleBinClickListener? = null

fun setRecycleBinClickListener(listener: RecycleBinClickListener) {
this.clickListener = listener
}

class RecycleBinViewHolder(
private val binding: ItemRecycleBoardBinding,
private val clickListener: RecycleBinClickListener?,
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: Board) {
with(binding) {
board = item
cbBoard.isChecked = item.isChecked
cbBoard.setOnClickListener {
item.isChecked = !item.isChecked
clickListener?.onCheckBoxClick(item)
}
}
}
}

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): RecycleBinViewHolder {
val binding =
ItemRecycleBoardBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return RecycleBinViewHolder(binding, clickListener)
}

override fun onBindViewHolder(
holder: RecycleBinViewHolder,
position: Int,
) {
holder.bind(getItem(position))
}

companion object {
val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<Board>() {
override fun areItemsTheSame(
oldItem: Board,
newItem: Board,
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: Board,
newItem: Board,
): Boolean {
return oldItem == newItem
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package boostcamp.and07.mindsync.ui.recyclebin

import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import boostcamp.and07.mindsync.data.model.Board

@BindingAdapter("app:restoreBoards")
fun RecyclerView.bindRestoreBoards(boards: List<Board>) {
if (this.adapter != null) {
(this.adapter as RecycleBinAdapter).submitList(boards.toMutableList())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package boostcamp.and07.mindsync.ui.recyclebin

import boostcamp.and07.mindsync.data.model.Board

interface RecycleBinClickListener {
fun onCheckBoxClick(board: Board)
}
Loading