use material library inbuilts

This commit is contained in:
Bnyro 2022-09-24 10:47:17 +02:00
parent 6f3834fcef
commit 31fee99282
4 changed files with 6 additions and 85 deletions

View File

@ -1,38 +0,0 @@
package com.github.libretube.ui.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.BackupRowBinding
class BackupOptionsAdapter(
private val options: List<Int>,
private val onChange: (position: Int, isChecked: Boolean) -> Unit
) : RecyclerView.Adapter<BackupOptionsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BackupOptionsViewHolder {
val binding = BackupRowBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return BackupOptionsViewHolder(binding)
}
override fun getItemCount(): Int {
return options.size
}
override fun onBindViewHolder(holder: BackupOptionsViewHolder, position: Int) {
holder.binding.apply {
title.text = root.context?.getString(options[position])
switchWidget.setOnCheckedChangeListener { _, isChecked ->
onChange.invoke(position, isChecked)
}
}
}
}
class BackupOptionsViewHolder(
val binding: BackupRowBinding
) : RecyclerView.ViewHolder(binding.root)

View File

@ -3,25 +3,20 @@ package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.databinding.DialogBackupBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.obj.BackupFile
import com.github.libretube.obj.PreferenceItem
import com.github.libretube.ui.adapters.BackupOptionsAdapter
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class BackupDialog(
private val createBackupFile: (BackupFile) -> Unit
) : DialogFragment() {
private lateinit var binding: DialogBackupBinding
private val backupFile = BackupFile()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val backupOptions = listOf(
val backupOptionNames = listOf(
R.string.watch_history,
R.string.watch_positions,
R.string.search_history,
@ -30,18 +25,15 @@ class BackupDialog(
R.string.preferences
)
val selected = MutableList(backupOptions.size) { false }
val backupItems = backupOptionNames.map { context?.getString(it)!! }.toTypedArray()
binding = DialogBackupBinding.inflate(layoutInflater)
binding.backupOptionsRecycler.layoutManager = LinearLayoutManager(context)
binding.backupOptionsRecycler.adapter =
BackupOptionsAdapter(backupOptions) { position, isChecked ->
selected[position] = isChecked
}
val selected = BooleanArray(backupOptionNames.size) { false }
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.backup)
.setView(binding.root)
.setMultiChoiceItems(backupItems, selected) { _, index, newValue ->
selected[index] = newValue
}
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.backup) { _, _ ->
val thread = Thread {

View File

@ -1,22 +0,0 @@
<?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="horizontal"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp">
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:textSize="16sp" />
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -1,11 +0,0 @@
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/backupOptionsRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>