From 435d669399451bd46a9c2deb3e199699fac3fac9 Mon Sep 17 00:00:00 2001 From: Erik Cavazos Date: Sat, 2 Sep 2023 18:08:05 -0600 Subject: [PATCH] WIP comentarios --- app/src/main/AndroidManifest.xml | 2 +- .../main/java/com/isolaatti/common/Dialogs.kt | 9 ++ .../presentation/CommentsViewModel.kt | 6 + .../comments/ui/BottomSheetPostComments.kt | 84 +++++++++---- .../res/layout/bottom_sheet_post_comments.xml | 118 +++++++++--------- app/src/main/res/values/strings.xml | 3 + 6 files changed, 142 insertions(+), 80 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 66ad57c..5e480a9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,7 +23,7 @@ - + onContinue(false)} .setOnCancelListener { onContinue(false) } } + + fun buildDeleteCommentDialog(context: Context, onContinue: (delete: Boolean) -> Unit): MaterialAlertDialogBuilder { + return MaterialAlertDialogBuilder(context) + .setTitle(R.string.delete) + .setMessage(R.string.comment_will_be_dropped) + .setPositiveButton(R.string.yes_continue) {_, _ -> onContinue(true)} + .setNegativeButton(R.string.cancel) { _, _ -> onContinue(false)} + .setOnCancelListener { onContinue(false) } + } } \ No newline at end of file diff --git a/app/src/main/java/com/isolaatti/posting/comments/presentation/CommentsViewModel.kt b/app/src/main/java/com/isolaatti/posting/comments/presentation/CommentsViewModel.kt index ee3dbea..db38cb4 100644 --- a/app/src/main/java/com/isolaatti/posting/comments/presentation/CommentsViewModel.kt +++ b/app/src/main/java/com/isolaatti/posting/comments/presentation/CommentsViewModel.kt @@ -26,6 +26,8 @@ class CommentsViewModel @Inject constructor(private val getComments: GetComments val comments: LiveData, UpdateEvent>> get() = _comments val commentPosted: MutableLiveData = MutableLiveData() val noMoreContent: MutableLiveData = MutableLiveData() + val commentToEdit: MutableLiveData = MutableLiveData() + val finishedEditingComment: MutableLiveData = MutableLiveData() /** * postId to query comments for. First page will be fetched when set. @@ -92,4 +94,8 @@ class CommentsViewModel @Inject constructor(private val getComments: GetComments commentsList.addAll(newList) _comments.postValue(Pair(commentsList, UpdateEvent(UpdateEvent.UpdateType.COMMENT_ADDED_TOP, null))) } + + fun switchToEditMode(comment: Comment) { + commentToEdit.postValue(comment) + } } \ No newline at end of file diff --git a/app/src/main/java/com/isolaatti/posting/comments/ui/BottomSheetPostComments.kt b/app/src/main/java/com/isolaatti/posting/comments/ui/BottomSheetPostComments.kt index cad94e1..2c92d1e 100644 --- a/app/src/main/java/com/isolaatti/posting/comments/ui/BottomSheetPostComments.kt +++ b/app/src/main/java/com/isolaatti/posting/comments/ui/BottomSheetPostComments.kt @@ -1,18 +1,23 @@ package com.isolaatti.posting.comments.ui import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.core.view.doOnLayout import androidx.core.widget.doOnTextChanged import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED +import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HALF_EXPANDED import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import com.isolaatti.R import com.isolaatti.common.Dialogs import com.isolaatti.databinding.BottomSheetPostCommentsBinding import com.isolaatti.posting.comments.domain.model.Comment @@ -43,12 +48,12 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC val optionsViewModel: BottomSheetPostOptionsViewModel by activityViewModels() - val optionsObserver: Observer = Observer { optionClicked -> + private val optionsObserver: Observer = Observer { optionClicked -> if(optionClicked?.callerId == CALLER_ID) { val comment = optionClicked.payload as? Comment ?: return@Observer when(optionClicked.optionId) { Options.Option.OPTION_DELETE -> { - Dialogs.buildDeletePostDialog(requireContext()) { delete -> + Dialogs.buildDeleteCommentDialog(requireContext()) { delete -> optionsViewModel.handle() if(delete) { // remove comment @@ -58,7 +63,7 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC } Options.Option.OPTION_EDIT -> { optionsViewModel.handle() - //editDiscussion.launch(post.id) + viewModel.switchToEditMode(comment) } Options.Option.OPTION_REPORT -> { optionsViewModel.handle() @@ -68,14 +73,14 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC } - val commentPostedObserver: Observer = Observer { + private val commentPostedObserver: Observer = Observer { when(it) { true -> { clearNewCommentUi() - Toast.makeText(requireContext(), "comment posted", Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), R.string.comment_posted, Toast.LENGTH_SHORT).show() } false -> { - Toast.makeText(requireContext(), "comment failed to post", Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), R.string.comment_failed_to_post, Toast.LENGTH_SHORT).show() } null -> return@Observer } @@ -84,25 +89,44 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC } - private fun setObservers() { - viewModel.comments.observe(viewLifecycleOwner) { - val (list, updateEvent) = it - adapter.updateList(list, updateEvent) - if(updateEvent.updateType == UpdateEvent.UpdateType.COMMENT_ADDED_TOP) { - (viewBinding.recyclerComments.layoutManager as LinearLayoutManager).scrollToPosition(0) - } else { - adapter.newContentRequestFinished() - } + private val commentsObserver: Observer, UpdateEvent>> = Observer { + val (list, updateEvent) = it + adapter.updateList(list, updateEvent) + if(updateEvent.updateType == UpdateEvent.UpdateType.COMMENT_ADDED_TOP) { + (viewBinding.recyclerComments.layoutManager as LinearLayoutManager).scrollToPosition(0) + } else { + adapter.newContentRequestFinished() } - viewModel.noMoreContent.observe(viewLifecycleOwner) { - if(it == true) { - adapter.blockInfiniteScroll = true - viewModel.noMoreContent.postValue(null) - } + } + private val noMoreContentObserver: Observer = Observer { + if(it == true) { + adapter.blockInfiniteScroll = true + viewModel.noMoreContent.postValue(null) } - optionsViewModel.optionClicked.observe(viewLifecycleOwner, optionsObserver) + } + + private val finishedEditingComment: Observer = Observer { + if(it == true) { + switchEditionModeUi(false) + } + + } + + private val commentToEditObserver: Observer = Observer { + switchEditionModeUi(true) + + viewBinding.newCommentTextField.editText?.setText(it.textContent) + } + + private fun setObservers() { + viewModel.comments.observe(viewLifecycleOwner, commentsObserver) + viewModel.noMoreContent.observe(viewLifecycleOwner, noMoreContentObserver) viewModel.commentPosted.observe(viewLifecycleOwner, commentPostedObserver) + viewModel.commentToEdit.observe(viewLifecycleOwner, commentToEditObserver) + viewModel.finishedEditingComment.observe(viewLifecycleOwner, finishedEditingComment) + + optionsViewModel.optionClicked.observe(viewLifecycleOwner, optionsObserver) } private fun setListeners() { @@ -120,6 +144,15 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC viewBinding.newCommentTextField.editText?.text?.clear() } + private fun switchEditionModeUi(editionMode: Boolean) { + if(editionMode) { + + } else { + + } + } + + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val postId = arguments?.getLong(ARG_POST_ID) @@ -135,12 +168,17 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC ): View { viewBinding = BottomSheetPostCommentsBinding.inflate(inflater) - (dialog as BottomSheetDialog).behavior.state = BottomSheetBehavior.STATE_EXPANDED + return viewBinding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + (dialog as BottomSheetDialog).also { + it.behavior.state = STATE_EXPANDED + it.behavior.skipCollapsed = true + } + viewBinding.recyclerComments.isNestedScrollingEnabled = true @@ -186,7 +224,7 @@ class BottomSheetPostComments() : BottomSheetDialogFragment(), OnUserInteractedC override fun onOptions(comment: Ownable) { - optionsViewModel.setOptions(Options.POST_OPTIONS, CALLER_ID, comment) + optionsViewModel.setOptions(Options.COMMENT_OPTIONS, CALLER_ID, comment) val fragment = BottomSheetPostOptionsFragment() fragment.show(parentFragmentManager, BottomSheetPostOptionsFragment.TAG) } diff --git a/app/src/main/res/layout/bottom_sheet_post_comments.xml b/app/src/main/res/layout/bottom_sheet_post_comments.xml index 1759840..5279b77 100644 --- a/app/src/main/res/layout/bottom_sheet_post_comments.xml +++ b/app/src/main/res/layout/bottom_sheet_post_comments.xml @@ -1,5 +1,5 @@ - + android:layout_height="wrap_content"> + android:layout_height="match_parent" + android:layout_marginBottom="184dp" + app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" /> - + android:layout_height="120dp" + android:layout_gravity="bottom"> - - - + + + + + + android:layout_margin="8dp" + android:layout_marginStart="4dp" + android:layout_marginTop="4dp" + android:layout_marginEnd="4dp" + android:layout_marginBottom="4dp" + android:hint="@string/new_comment" + app:boxCornerRadiusBottomEnd="20dp" + app:boxCornerRadiusBottomStart="20dp" + app:boxCornerRadiusTopEnd="20dp" + app:boxCornerRadiusTopStart="20dp" - + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@+id/submitCommentButton" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/comment_actions"> - - + + + + + + + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e4a3b09..a0f2c63 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -67,8 +67,11 @@ People About This discussion and all related content will be dropped. Continue? + This comment will be removed. Continue? Yes, delete Cancel Save Save snapshot + Comment posted! + Comment failed to post \ No newline at end of file