mirror of https://github.com/FongMi/TV.git
parent
21acc81562
commit
faac37e66b
@ -0,0 +1,76 @@ |
||||
package com.fongmi.android.tv.ui.adapter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
import com.fongmi.android.tv.databinding.AdapterBackupBinding; |
||||
import com.fongmi.android.tv.db.AppDatabase; |
||||
import com.github.catvod.utils.Path; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
public class BackupAdapter extends RecyclerView.Adapter<BackupAdapter.ViewHolder> { |
||||
|
||||
private final OnClickListener mListener; |
||||
private List<String> mItems; |
||||
|
||||
public BackupAdapter(OnClickListener listener) { |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
public interface OnClickListener { |
||||
|
||||
void onTextClick(String item); |
||||
|
||||
void onDeleteClick(String item); |
||||
} |
||||
|
||||
public BackupAdapter addAll() { |
||||
mItems = new ArrayList<>(); |
||||
for(File file : Path.list(Path.tv())) if (file.getAbsolutePath().endsWith(AppDatabase.BACKUP_SUFFIX)) mItems.add(file.getName().replace("." + AppDatabase.BACKUP_SUFFIX, "")); |
||||
Collections.sort(mItems); |
||||
return this; |
||||
} |
||||
|
||||
public int remove(String item) { |
||||
File file = new File(Path.tv(), item + "." + AppDatabase.BACKUP_SUFFIX); |
||||
if (file.exists()) file.delete(); |
||||
mItems.remove(item); |
||||
notifyDataSetChanged(); |
||||
return getItemCount(); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterBackupBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
||||
String item = mItems.get(position); |
||||
holder.binding.text.setText(item); |
||||
holder.binding.text.setOnClickListener(v -> mListener.onTextClick(item)); |
||||
holder.binding.delete.setOnClickListener(v -> mListener.onDeleteClick(item)); |
||||
} |
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder { |
||||
|
||||
private final AdapterBackupBinding binding; |
||||
|
||||
ViewHolder(@NonNull AdapterBackupBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,69 @@ |
||||
package com.fongmi.android.tv.ui.dialog; |
||||
|
||||
import android.app.Activity; |
||||
import android.view.LayoutInflater; |
||||
import android.view.WindowManager; |
||||
|
||||
import androidx.appcompat.app.AlertDialog; |
||||
import com.fongmi.android.tv.databinding.DialogHistoryBinding; |
||||
import com.fongmi.android.tv.db.AppDatabase; |
||||
import com.fongmi.android.tv.impl.BackupCallback; |
||||
import com.fongmi.android.tv.ui.adapter.BackupAdapter; |
||||
import com.fongmi.android.tv.ui.custom.SpaceItemDecoration; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.github.catvod.utils.Path; |
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
||||
import java.io.File; |
||||
|
||||
public class BackupDialog implements BackupAdapter.OnClickListener { |
||||
|
||||
private final DialogHistoryBinding binding; |
||||
private final BackupCallback callback; |
||||
private final BackupAdapter adapter; |
||||
private final AlertDialog dialog; |
||||
|
||||
public static BackupDialog create(Activity activity) { |
||||
return new BackupDialog(activity); |
||||
} |
||||
|
||||
|
||||
public BackupDialog(Activity activity) { |
||||
this.callback = (BackupCallback) activity; |
||||
this.binding = DialogHistoryBinding.inflate(LayoutInflater.from(activity)); |
||||
this.dialog = new MaterialAlertDialogBuilder(activity).setView(binding.getRoot()).create(); |
||||
this.adapter = new BackupAdapter(this); |
||||
} |
||||
|
||||
public void show() { |
||||
setRecyclerView(); |
||||
setDialog(); |
||||
binding.recycler.requestFocus(); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
binding.recycler.setHasFixedSize(true); |
||||
binding.recycler.setAdapter(adapter.addAll()); |
||||
binding.recycler.addItemDecoration(new SpaceItemDecoration(1, 16)); |
||||
} |
||||
|
||||
private void setDialog() { |
||||
if (adapter.getItemCount() == 0) return; |
||||
WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); |
||||
params.width = (int) (ResUtil.getScreenWidth() * 0.4f); |
||||
dialog.getWindow().setAttributes(params); |
||||
dialog.getWindow().setDimAmount(0); |
||||
dialog.show(); |
||||
} |
||||
|
||||
@Override |
||||
public void onTextClick(String item) { |
||||
callback.restore(new File(Path.tv(), item + "." + AppDatabase.BACKUP_SUFFIX)); |
||||
dialog.dismiss(); |
||||
} |
||||
|
||||
@Override |
||||
public void onDeleteClick(String item) { |
||||
if (adapter.remove(item) == 0) dialog.dismiss(); |
||||
} |
||||
} |
||||
@ -1,40 +0,0 @@ |
||||
package com.fongmi.android.tv.ui.dialog; |
||||
|
||||
import android.app.Activity; |
||||
import com.fongmi.android.tv.impl.RestoreCallback; |
||||
import com.github.catvod.utils.Path; |
||||
import com.obsez.android.lib.filechooser.ChooserDialog; |
||||
|
||||
import java.io.File; |
||||
|
||||
public class RestoreDialog { |
||||
|
||||
private ChooserDialog dialog; |
||||
|
||||
private RestoreCallback callback; |
||||
|
||||
public static RestoreDialog create() { |
||||
return new RestoreDialog(); |
||||
} |
||||
|
||||
public RestoreDialog callback(RestoreCallback callback) { |
||||
this.callback = callback; |
||||
return this; |
||||
} |
||||
|
||||
public void show(Activity activity) { |
||||
dialog = new ChooserDialog(activity); |
||||
dialog.withFilter(false, false, "tv", "backup"); |
||||
dialog.withStartFile(Path.tv().getAbsolutePath()); |
||||
dialog.withChosenListener(this::onChoosePath); |
||||
dialog.withOnBackPressedListener(d -> dialog.goBack()); |
||||
dialog.withOnLastBackPressedListener(d -> dialog.dismiss()); |
||||
dialog.build().show(); |
||||
} |
||||
|
||||
|
||||
private void onChoosePath(String path, File pathFile) { |
||||
callback.onRestore(pathFile); |
||||
if (dialog != null) dialog.dismiss(); |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text" |
||||
android:layout_width="0dp" |
||||
android:layout_height="40dp" |
||||
android:layout_weight="1" |
||||
android:background="@drawable/selector_text" |
||||
android:ellipsize="middle" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center" |
||||
android:singleLine="true" |
||||
android:textColor="@color/white" |
||||
android:textSize="18sp" |
||||
tools:text="202201010808" /> |
||||
|
||||
<ImageView |
||||
android:id="@+id/delete" |
||||
android:layout_width="40dp" |
||||
android:layout_height="40dp" |
||||
android:layout_marginStart="12dp" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:padding="8dp" |
||||
android:scaleType="fitCenter" |
||||
android:src="@drawable/ic_setting_delete" /> |
||||
|
||||
</LinearLayout> |
||||
@ -0,0 +1,9 @@ |
||||
package com.fongmi.android.tv.impl; |
||||
|
||||
|
||||
import java.io.File; |
||||
|
||||
public interface BackupCallback { |
||||
|
||||
void restore(File file); |
||||
} |
||||
@ -1,8 +0,0 @@ |
||||
package com.fongmi.android.tv.impl; |
||||
|
||||
import java.io.File; |
||||
|
||||
public interface RestoreCallback { |
||||
|
||||
void onRestore(File file); |
||||
} |
||||
@ -0,0 +1,77 @@ |
||||
package com.fongmi.android.tv.ui.adapter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
import com.fongmi.android.tv.databinding.AdapterBackupBinding; |
||||
import com.fongmi.android.tv.db.AppDatabase; |
||||
import com.github.catvod.utils.Path; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
public class BackupAdapter extends RecyclerView.Adapter<BackupAdapter.ViewHolder> { |
||||
|
||||
private final OnClickListener mListener; |
||||
private List<String> mItems; |
||||
|
||||
public BackupAdapter(OnClickListener listener) { |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
public interface OnClickListener { |
||||
|
||||
void onTextClick(String item); |
||||
|
||||
void onDeleteClick(String item); |
||||
} |
||||
|
||||
public BackupAdapter addAll() { |
||||
mItems = new ArrayList<>(); |
||||
for(File file : Path.list(Path.tv())) if (file.getAbsolutePath().endsWith(AppDatabase.BACKUP_SUFFIX)) mItems.add(file.getName().replace("." + AppDatabase.BACKUP_SUFFIX, "")); |
||||
Collections.sort(mItems); |
||||
return this; |
||||
} |
||||
|
||||
public int remove(String item) { |
||||
File file = new File(Path.tv(), item + "." + AppDatabase.BACKUP_SUFFIX); |
||||
if (file.exists()) file.delete(); |
||||
mItems.remove(item); |
||||
notifyDataSetChanged(); |
||||
return getItemCount(); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterBackupBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
||||
String item = mItems.get(position); |
||||
holder.binding.text.setText(item); |
||||
holder.binding.text.setOnClickListener(v -> mListener.onTextClick(item)); |
||||
holder.binding.delete.setOnClickListener(v -> mListener.onDeleteClick(item)); |
||||
} |
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder { |
||||
|
||||
private final AdapterBackupBinding binding; |
||||
|
||||
ViewHolder(@NonNull AdapterBackupBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,64 @@ |
||||
package com.fongmi.android.tv.ui.dialog; |
||||
|
||||
import android.view.LayoutInflater; |
||||
|
||||
import androidx.appcompat.app.AlertDialog; |
||||
import androidx.fragment.app.Fragment; |
||||
|
||||
import com.fongmi.android.tv.databinding.DialogHistoryBinding; |
||||
import com.fongmi.android.tv.db.AppDatabase; |
||||
import com.fongmi.android.tv.impl.BackupCallback; |
||||
import com.fongmi.android.tv.ui.adapter.BackupAdapter; |
||||
import com.fongmi.android.tv.ui.custom.SpaceItemDecoration; |
||||
import com.github.catvod.utils.Path; |
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
||||
import java.io.File; |
||||
|
||||
public class BackupDialog implements BackupAdapter.OnClickListener { |
||||
|
||||
private final DialogHistoryBinding binding; |
||||
private final BackupCallback callback; |
||||
private final BackupAdapter adapter; |
||||
private final AlertDialog dialog; |
||||
|
||||
public static BackupDialog create(Fragment fragment) { |
||||
return new BackupDialog(fragment); |
||||
} |
||||
|
||||
|
||||
public BackupDialog(Fragment fragment) { |
||||
this.callback = (BackupCallback) fragment; |
||||
this.binding = DialogHistoryBinding.inflate(LayoutInflater.from(fragment.getContext())); |
||||
this.dialog = new MaterialAlertDialogBuilder(fragment.getActivity()).setView(binding.getRoot()).create(); |
||||
this.adapter = new BackupAdapter(this); |
||||
} |
||||
|
||||
public void show() { |
||||
setRecyclerView(); |
||||
setDialog(); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
binding.recycler.setHasFixedSize(true); |
||||
binding.recycler.addItemDecoration(new SpaceItemDecoration(1, 8)); |
||||
binding.recycler.setAdapter(adapter.addAll()); |
||||
} |
||||
|
||||
private void setDialog() { |
||||
if (adapter.getItemCount() == 0) return; |
||||
dialog.getWindow().setDimAmount(0); |
||||
dialog.show(); |
||||
} |
||||
|
||||
@Override |
||||
public void onTextClick(String item) { |
||||
callback.restore(new File(Path.tv(), item + "." + AppDatabase.BACKUP_SUFFIX)); |
||||
dialog.dismiss(); |
||||
} |
||||
|
||||
@Override |
||||
public void onDeleteClick(String item) { |
||||
if (adapter.remove(item) == 0) dialog.dismiss(); |
||||
} |
||||
} |
||||
@ -0,0 +1,30 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:gravity="center_vertical" |
||||
android:orientation="horizontal"> |
||||
|
||||
<com.google.android.material.button.MaterialButton |
||||
android:id="@+id/text" |
||||
style="?attr/materialButtonOutlinedStyle" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:ellipsize="middle" |
||||
android:singleLine="true" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="14sp" |
||||
tools:text="202201010808" /> |
||||
|
||||
<ImageView |
||||
android:id="@+id/delete" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginStart="16dp" |
||||
android:background="?attr/selectableItemBackgroundBorderless" |
||||
android:scaleType="fitCenter" |
||||
android:src="@drawable/ic_setting_delete" /> |
||||
|
||||
</LinearLayout> |
||||
Loading…
Reference in new issue