Restore dialog to add filter

This commit is contained in:
tarsin committed 2023-06-07 13:43:57 +08:00
1 parent a05fc8447d
commit b86cb676d6
2 files changed
+99 -1

No files matched your search

@@ -1,7 +1,9 @@
package com.hippo.ehviewer.ui.settings
import android.content.DialogInterface
import android.view.View
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
@@ -34,6 +36,7 @@ import androidx.compose.ui.unit.dp
import com.hippo.ehviewer.R
import com.hippo.ehviewer.client.EhFilter
import com.hippo.ehviewer.dao.Filter
import com.hippo.ehviewer.databinding.DialogAddFilterBinding
import com.hippo.ehviewer.ui.LocalNavController
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
import kotlinx.coroutines.Dispatchers
@@ -45,6 +48,41 @@ fun FilterScreen() {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope { Dispatchers.IO }
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
val scaffoldRecomposeScope = currentRecomposeScope
class AddFilterDialogHelper(private val dialog: AlertDialog) : View.OnClickListener {
private val mArray: Array<String> = dialog.context.resources.getStringArray(R.array.filter_entries)
private val binding = DialogAddFilterBinding.bind(dialog.findViewById(R.id.base)!!)
init { dialog.getButton(DialogInterface.BUTTON_POSITIVE)!!.setOnClickListener(this) }
override fun onClick(v: View) {
val text1 = binding.spinner.editText!!.text.toString()
if (text1.isEmpty()) {
binding.spinner.error = dialog.context.getString(R.string.text_is_empty)
return
} else {
binding.spinner.error = null
}
val text = binding.textInputLayout.editText!!.text.toString().trim { it <= ' ' }
if (text.isBlank()) {
binding.textInputLayout.error = dialog.context.getString(R.string.text_is_empty)
return
} else {
binding.textInputLayout.error = null
}
val mode = mArray.indexOf(text1)
val filter = Filter()
filter.mode = mode
filter.text = text
if (!EhFilter.addFilter(filter)) {
binding.textInputLayout.error = dialog.context.getString(R.string.label_text_exist)
return
} else {
binding.textInputLayout.error = null
}
scaffoldRecomposeScope.invalidate()
dialog.dismiss()
}
}
Scaffold(
topBar = {
TopAppBar(
@@ -63,7 +101,7 @@ fun FilterScreen() {
)
},
floatingActionButton = {
FloatingActionButton(onClick = { }) {
FloatingActionButton(onClick = { AddFilterDialogHelper(BaseDialogBuilder(context).setTitle(R.string.add_filter).setView(R.layout.dialog_add_filter).setPositiveButton(R.string.add, null).setNegativeButton(android.R.string.cancel, null).show()) }) {
Icon(imageVector = Icons.Default.Add, contentDescription = null)
}
},
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2016 Hippo Seven
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="?attr/dialogPreferredPadding"
android:paddingTop="@dimen/abc_dialog_padding_top_material">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/spinner"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/filter_label">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
app:simpleItems="@array/filter_entries" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:hint="@string/filter_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="1"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>