Skip to content

Commit

Permalink
Add reasons to comment deletion dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsalarm committed Sep 2, 2018
1 parent f03ae14 commit 2f4ebe6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 74 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ class ItemUserAdminDialog : BaseDialogFragment("ItemUserAdminDialog") {

private val reasonListView: ListView by bindView(R.id.reason)
private val customReasonText: EditText by bindView(R.id.custom_reason)
private val blockUser: CheckBox by bindView(R.id.block_user)
private val blockUserForDays: EditText by bindView(R.id.block_user_days)
private val blockTreeup: CheckBox by bindView(R.id.block_treeup)

private val blockUser: CheckBox? by bindOptionalView(R.id.block_user)
private val blockUserForDays: EditText? by bindOptionalView(R.id.block_user_days)
private val blockTreeup: CheckBox? by bindOptionalView(R.id.block_treeup)

private val deleteSoft: CheckBox? by bindOptionalView(R.id.soft_delete)

// one of those must be set.
private val user: String? by lazy { arguments?.getString(KEY_USER) }
private val item: FeedItem? by lazy { arguments?.getFreezable(KEY_FEED_ITEM, FeedItem) }
private val comment: Long? by lazy { arguments?.getLong(KEY_COMMENT)?.takeIf { it > 0 } }

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val layout = if (user != null) R.layout.admin_ban_user else R.layout.admin_delete_item
val layout = when {
user != null -> R.layout.admin_ban_user
item != null -> R.layout.admin_delete_item
comment != null -> R.layout.admin_delete_comment
else -> throw IllegalArgumentException()
}

return dialog(requireContext()) {
layout(layout)
Expand All @@ -51,13 +60,15 @@ class ItemUserAdminDialog : BaseDialogFragment("ItemUserAdminDialog") {
reasonListView.adapter = ArrayAdapter(dialog.context,
android.R.layout.simple_list_item_1, REASONS)

if (user != null) {
blockUser.isChecked = true
blockUser.isEnabled = false
}
blockUser?.let { blockUser ->
if (user != null) {
blockUser.isChecked = true
blockUser.isEnabled = false
}

blockUser.checkedChanges().subscribe { checked ->
blockTreeup.isEnabled = checked
blockUser.checkedChanges().subscribe { checked ->
blockTreeup?.isEnabled = checked
}
}

reasonListView.itemClicks().subscribe { index ->
Expand All @@ -74,6 +85,7 @@ class ItemUserAdminDialog : BaseDialogFragment("ItemUserAdminDialog") {
val completable = null
?: item?.let { deleteItem(it, reason) }
?: user?.let { blockUser(it, reason) }
?: comment?.let { deleteComment(it, reason) }
?: throw IllegalStateException("Either item or user must be set.")

completable
Expand All @@ -84,20 +96,26 @@ class ItemUserAdminDialog : BaseDialogFragment("ItemUserAdminDialog") {
}

private fun deleteItem(item: FeedItem, reason: String): Completable {
val ban = blockUser.isChecked
val banUserDays = if (ban) blockUserForDays.text.toString().toFloatOrNull() else null
val ban = blockUser?.isChecked ?: false
val banUserDays = if (ban) blockUserForDays?.text?.toString()?.toFloatOrNull() else null
return adminService.deleteItem(item, reason, banUserDays)
}

private fun blockUser(user: String, reason: String): Completable {
val treeup = blockTreeup.isChecked
val banUserDays = blockUserForDays.text.toString().toFloatOrNull() ?: 0f
val treeup = blockTreeup?.isChecked ?: false
val banUserDays = blockUserForDays?.text?.toString()?.toFloatOrNull() ?: 0f
return adminService.banUser(user, reason, banUserDays, treeup)
}

private fun deleteComment(commentId: Long, reason: String): Completable {
val deleteHard = !(deleteSoft?.isChecked ?: false)
return adminService.deleteComment(deleteHard, commentId, reason)
}

companion object {
private const val KEY_USER = "userId"
private const val KEY_FEED_ITEM = "feedItem"
private const val KEY_COMMENT = "commentId"

val REASONS = listOf(
"Repost",
Expand Down Expand Up @@ -128,5 +146,9 @@ class ItemUserAdminDialog : BaseDialogFragment("ItemUserAdminDialog") {
fun forUser(name: String) = ItemUserAdminDialog().arguments {
putString(KEY_USER, name)
}

fun forComment(commentId: Long) = ItemUserAdminDialog().arguments {
putLong(KEY_COMMENT, commentId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.pr0gramm.app.ui.*
import com.pr0gramm.app.ui.ScrollHideToolbarListener.ToolbarActivity
import com.pr0gramm.app.ui.back.BackAwareFragment
import com.pr0gramm.app.ui.base.BaseFragment
import com.pr0gramm.app.ui.dialogs.DeleteCommentDialog
import com.pr0gramm.app.ui.dialogs.ErrorDialogFragment.Companion.showErrorString
import com.pr0gramm.app.ui.dialogs.NewTagDialogFragment
import com.pr0gramm.app.ui.dialogs.ignoreError
Expand Down Expand Up @@ -1216,7 +1215,7 @@ class PostFragment : BaseFragment("PostFragment"), NewTagDialogFragment.OnAddNew
}

override fun onDeleteCommentClicked(comment: Api.Comment): Boolean {
val dialog = DeleteCommentDialog.newInstance(comment.id)
val dialog = ItemUserAdminDialog.forComment(comment.id)
dialog.show(fragmentManager, null)
return true
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/admin_ban_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
tools:ignore="HardcodedText"/>
</LinearLayout>


<CheckBox
android:id="@+id/block_treeup"
android:layout_width="wrap_content"
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/res/layout/admin_delete_comment.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:orientation="vertical">

<ListView
android:id="@+id/reason"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"/>

<EditText
android:id="@+id/custom_reason"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:hint="@string/reason"/>

<CheckBox
android:id="@+id/soft_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="@string/delete_comment_soft"/>

<EditText
android:id="@+id/reason"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reason"/>
</LinearLayout>

0 comments on commit 2f4ebe6

Please sign in to comment.