WIP comentarios

This commit is contained in:
Erik Cavazos 2023-09-02 18:08:05 -06:00
parent 4fc72786af
commit 435d669399
6 changed files with 142 additions and 80 deletions

View File

@ -23,7 +23,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".home.HomeActivity" android:theme="@style/Theme.Isolaatti" android:windowSoftInputMode="adjustResize" />
<activity android:name=".home.HomeActivity" android:theme="@style/Theme.Isolaatti" />
<activity android:name=".login.LogInActivity" android:theme="@style/Theme.Isolaatti" />
<activity android:name=".profile.ui.ProfileActivity"
android:theme="@style/Theme.Isolaatti"

View File

@ -13,4 +13,13 @@ object Dialogs {
.setNegativeButton(R.string.cancel) { _, _ -> 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) }
}
}

View File

@ -26,6 +26,8 @@ class CommentsViewModel @Inject constructor(private val getComments: GetComments
val comments: LiveData<Pair<List<Comment>, UpdateEvent>> get() = _comments
val commentPosted: MutableLiveData<Boolean?> = MutableLiveData()
val noMoreContent: MutableLiveData<Boolean?> = MutableLiveData()
val commentToEdit: MutableLiveData<Comment> = MutableLiveData()
val finishedEditingComment: MutableLiveData<Boolean?> = 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)
}
}

View File

@ -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<OptionClicked?> = Observer { optionClicked ->
private val optionsObserver: Observer<OptionClicked?> = 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<Boolean?> = Observer {
private val commentPostedObserver: Observer<Boolean?> = 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<Pair<List<Comment>, 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<Boolean?> = Observer {
if(it == true) {
adapter.blockInfiniteScroll = true
viewModel.noMoreContent.postValue(null)
}
optionsViewModel.optionClicked.observe(viewLifecycleOwner, optionsObserver)
}
private val finishedEditingComment: Observer<Boolean?> = Observer {
if(it == true) {
switchEditionModeUi(false)
}
}
private val commentToEditObserver: Observer<Comment> = 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)
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
@ -8,11 +8,7 @@
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/topAppBar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/recycler_comments">
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
@ -29,63 +25,73 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_comments"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintBottom_toTopOf="@id/new_comment_area"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topAppBar_layout" />
android:layout_height="match_parent"
android:layout_marginBottom="184dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
<androidx.constraintlayout.widget.ConstraintLayout
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/new_comment_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
android:layout_height="120dp"
android:layout_gravity="bottom">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/newCommentTextField"
style="?attr/textInputOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
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:startIconDrawable="@drawable/baseline_keyboard_voice_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/submitCommentButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/comment_actions"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:orientation="horizontal">
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/newCommentTextField"
style="?attr/textInputOutlinedStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"/>
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"
</com.google.android.material.textfield.TextInputLayout>
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/submitCommentButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/comment_actions">
<com.google.android.material.button.MaterialButton
android:id="@+id/submitCommentButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:enabled="false"
app:icon="@drawable/baseline_send_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/submitCommentButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:enabled="false"
app:icon="@drawable/baseline_send_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/comment_actions" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -67,8 +67,11 @@
<string name="people">People</string>
<string name="about">About</string>
<string name="post_will_dropped">This discussion and all related content will be dropped. Continue?</string>
<string name="comment_will_be_dropped">This comment will be removed. Continue?</string>
<string name="yes_continue">Yes, delete</string>
<string name="cancel">Cancel</string>
<string name="save">Save</string>
<string name="save_snapshot">Save snapshot</string>
<string name="comment_posted">Comment posted!</string>
<string name="comment_failed_to_post">Comment failed to post</string>
</resources>