parent
bb7df5b804
commit
2b6430a81f
@ -1,55 +0,0 @@ |
||||
package io.legado.app.ui.book.group |
||||
|
||||
import android.content.Context |
||||
import android.view.LayoutInflater |
||||
import io.legado.app.R |
||||
import io.legado.app.data.appDb |
||||
import io.legado.app.data.entities.BookGroup |
||||
import io.legado.app.databinding.DialogEditTextBinding |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.lib.dialogs.alert |
||||
import io.legado.app.utils.requestInputMethod |
||||
|
||||
object GroupEdit { |
||||
|
||||
fun show(context: Context, layoutInflater: LayoutInflater, bookGroup: BookGroup) = context.run { |
||||
alert(title = getString(R.string.group_edit)) { |
||||
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { |
||||
textInputLayout.setHint(R.string.group_name) |
||||
editView.setText(bookGroup.groupName) |
||||
} |
||||
if (bookGroup.groupId >= 0) { |
||||
neutralButton(R.string.delete) { |
||||
deleteGroup(context, bookGroup) |
||||
} |
||||
} |
||||
customView { alertBinding.root } |
||||
yesButton { |
||||
alertBinding.editView.text?.toString()?.let { |
||||
bookGroup.groupName = it |
||||
Coroutine.async { |
||||
appDb.bookGroupDao.update(bookGroup) |
||||
} |
||||
} |
||||
} |
||||
noButton() |
||||
}.show().requestInputMethod() |
||||
} |
||||
|
||||
private fun deleteGroup(context: Context, bookGroup: BookGroup) = context.run { |
||||
alert(R.string.delete, R.string.sure_del) { |
||||
okButton { |
||||
Coroutine.async { |
||||
appDb.bookGroupDao.delete(bookGroup) |
||||
val books = appDb.bookDao.getBooksByGroup(bookGroup.groupId) |
||||
books.forEach { |
||||
it.group = it.group - bookGroup.groupId |
||||
} |
||||
appDb.bookDao.update(*books.toTypedArray()) |
||||
} |
||||
} |
||||
noButton() |
||||
}.show() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,110 @@ |
||||
package io.legado.app.ui.book.group |
||||
|
||||
import android.os.Bundle |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.fragment.app.FragmentManager |
||||
import androidx.fragment.app.viewModels |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseDialogFragment |
||||
import io.legado.app.data.entities.BookGroup |
||||
import io.legado.app.databinding.DialogBookGroupEditBinding |
||||
import io.legado.app.lib.dialogs.alert |
||||
import io.legado.app.lib.theme.primaryColor |
||||
import io.legado.app.utils.gone |
||||
import io.legado.app.utils.toastOnUi |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
import io.legado.app.utils.windowSize |
||||
import splitties.views.onClick |
||||
|
||||
class GroupEditDialog : BaseDialogFragment() { |
||||
|
||||
companion object { |
||||
|
||||
fun start(fragmentManager: FragmentManager, bookGroup: BookGroup? = null) { |
||||
GroupEditDialog().apply { |
||||
arguments = Bundle().apply { |
||||
putParcelable("group", bookGroup) |
||||
} |
||||
}.show(fragmentManager, "bookGroupEdit") |
||||
} |
||||
|
||||
} |
||||
|
||||
private val binding by viewBinding(DialogBookGroupEditBinding::bind) |
||||
private val viewModel by viewModels<GroupViewModel>() |
||||
private var bookGroup: BookGroup? = null |
||||
|
||||
override fun onStart() { |
||||
super.onStart() |
||||
val dm = requireActivity().windowSize |
||||
dialog?.window?.setLayout( |
||||
(dm.widthPixels * 0.9).toInt(), |
||||
ViewGroup.LayoutParams.WRAP_CONTENT |
||||
) |
||||
} |
||||
|
||||
override fun onCreateView( |
||||
inflater: LayoutInflater, |
||||
container: ViewGroup?, |
||||
savedInstanceState: Bundle? |
||||
): View? { |
||||
return inflater.inflate(R.layout.dialog_book_group_edit, container) |
||||
} |
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
||||
binding.toolBar.setBackgroundColor(primaryColor) |
||||
bookGroup = arguments?.getParcelable("group") |
||||
bookGroup?.let { |
||||
binding.tieGroupName.setText(it.groupName) |
||||
binding.ivCover.load(it.cover) |
||||
} ?: let { |
||||
binding.toolBar.title = getString(R.string.add_group) |
||||
binding.btnDelete.gone() |
||||
} |
||||
binding.run { |
||||
btnCancel.onClick { |
||||
dismiss() |
||||
} |
||||
btnOk.onClick { |
||||
val groupName = tieGroupName.text?.toString() |
||||
if (groupName.isNullOrEmpty()) { |
||||
toastOnUi("分组名称不能为空") |
||||
} else { |
||||
bookGroup?.let { |
||||
it.groupName = groupName |
||||
it.cover = binding.ivCover.path |
||||
viewModel.upGroup(it) { |
||||
dismiss() |
||||
} |
||||
} ?: let { |
||||
viewModel.addGroup(groupName, binding.ivCover.path) { |
||||
dismiss() |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
btnDelete.onClick { |
||||
deleteGroup { |
||||
bookGroup?.let { |
||||
viewModel.delGroup(it) { |
||||
dismiss() |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private fun deleteGroup(ok: () -> Unit) { |
||||
alert(R.string.delete, R.string.sure_del) { |
||||
okButton { |
||||
ok.invoke() |
||||
} |
||||
noButton() |
||||
}.show() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,89 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="@color/background_menu"> |
||||
|
||||
<androidx.appcompat.widget.Toolbar |
||||
android:id="@+id/tool_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="@color/background_menu" |
||||
android:elevation="5dp" |
||||
android:theme="?attr/actionBarStyle" |
||||
app:displayHomeAsUp="false" |
||||
app:fitStatusBar="false" |
||||
app:layout_constraintTop_toTopOf="parent" |
||||
app:popupTheme="@style/AppTheme.PopupOverlay" |
||||
app:title="@string/group_edit" |
||||
app:titleTextAppearance="@style/ToolbarTitle" /> |
||||
|
||||
<io.legado.app.ui.widget.image.CoverImageView |
||||
android:id="@+id/iv_cover" |
||||
android:layout_width="90dp" |
||||
android:layout_height="126dp" |
||||
android:layout_margin="6dp" |
||||
android:contentDescription="@string/img_cover" |
||||
android:scaleType="centerCrop" |
||||
android:src="@drawable/image_cover_default" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintLeft_toLeftOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@+id/tool_bar" /> |
||||
|
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:id="@+id/til_group_name" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_margin="6dp" |
||||
android:hint="@string/group_name" |
||||
app:layout_constraintLeft_toRightOf="@+id/iv_cover" |
||||
app:layout_constraintRight_toRightOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@+id/tool_bar" |
||||
app:layout_constraintBottom_toTopOf="@+id/btn_ok"> |
||||
|
||||
<io.legado.app.ui.widget.text.EditText |
||||
android:id="@+id/tie_group_name" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:singleLine="true" |
||||
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" /> |
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
<io.legado.app.ui.widget.text.AccentTextView |
||||
android:id="@+id/btn_delete" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:padding="12dp" |
||||
android:layout_marginLeft="12dp" |
||||
android:text="@string/delete" |
||||
app:layout_constraintBottom_toBottomOf="@id/btn_ok" |
||||
app:layout_constraintLeft_toRightOf="@+id/iv_cover" |
||||
app:layout_constraintTop_toTopOf="@+id/btn_ok" |
||||
tools:ignore="RtlHardcoded" /> |
||||
|
||||
<io.legado.app.ui.widget.text.AccentTextView |
||||
android:id="@+id/btn_ok" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:padding="12dp" |
||||
android:layout_marginRight="12dp" |
||||
android:text="@string/ok" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintRight_toRightOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@+id/til_group_name" |
||||
tools:ignore="RtlHardcoded" /> |
||||
|
||||
<io.legado.app.ui.widget.text.AccentTextView |
||||
android:id="@+id/btn_cancel" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:padding="12dp" |
||||
android:text="@string/cancel" |
||||
app:layout_constraintBottom_toBottomOf="@id/btn_ok" |
||||
app:layout_constraintRight_toLeftOf="@+id/btn_ok" |
||||
app:layout_constraintTop_toTopOf="@+id/btn_ok" /> |
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
Loading…
Reference in new issue