Dowload attachments: auto confirm

pull/215/head
M66B 7 months ago
parent 7d1a791876
commit 7b6d61479f

@ -22,6 +22,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -32,6 +33,8 @@ import java.util.List;
public class FragmentDialogDownloadAttachments extends FragmentDialogBase { public class FragmentDialogDownloadAttachments extends FragmentDialogBase {
private List<Long> remaining; private List<Long> remaining;
private static final long AUTO_CONFIRM_DELAY = 2 * 1000L;
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
@ -51,12 +54,14 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase {
NumberFormat NF = NumberFormat.getNumberInstance(); NumberFormat NF = NumberFormat.getNumberInstance();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean attachments_auto_confirm = prefs.getBoolean("attachments_auto_confirm", false);
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_download_attachments, null); View dview = LayoutInflater.from(context).inflate(R.layout.dialog_download_attachments, null);
TextView tvRemark = dview.findViewById(R.id.tvRemark); TextView tvRemark = dview.findViewById(R.id.tvRemark);
Button btnDownload = dview.findViewById(R.id.btnDownload); Button btnDownload = dview.findViewById(R.id.btnDownload);
ProgressBar pbDownloaded = dview.findViewById(R.id.pbDownloaded); ProgressBar pbDownloaded = dview.findViewById(R.id.pbDownloaded);
TextView tvRemaining = dview.findViewById(R.id.tvRemaining); TextView tvRemaining = dview.findViewById(R.id.tvRemaining);
CheckBox cbAutoConfirm = dview.findViewById(R.id.cbAutoConfirm);
CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain); CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain);
btnDownload.setOnClickListener(new View.OnClickListener() { btnDownload.setOnClickListener(new View.OnClickListener() {
@ -96,7 +101,8 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase {
long[] download = args.getLongArray("download"); long[] download = args.getLongArray("download");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(), new Observer<List<EntityAttachment>>() { LiveData<List<EntityAttachment>> live = db.attachment().liveAttachments(id);
live.observe(getViewLifecycleOwner(), new Observer<List<EntityAttachment>>() {
@Override @Override
public void onChanged(List<EntityAttachment> attachments) { public void onChanged(List<EntityAttachment> attachments) {
if (attachments != null) if (attachments != null)
@ -112,6 +118,27 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase {
tvRemaining.setText(getString(R.string.title_attachments_download, of)); tvRemaining.setText(getString(R.string.title_attachments_download, of));
updateButton(); updateButton();
if (remaining.isEmpty()) {
live.removeObserver(this);
boolean attachments_auto_confirm = prefs.getBoolean("attachments_auto_confirm", false);
if (attachments_auto_confirm)
dview.postDelayed(new Runnable() {
@Override
public void run() {
try {
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
startActivity(intent);
dismiss();
} catch (Throwable ex) {
Log.e(ex);
}
}
}, AUTO_CONFIRM_DELAY);
}
} }
}); });
} }
@ -124,6 +151,14 @@ public class FragmentDialogDownloadAttachments extends FragmentDialogBase {
} }
}); });
cbAutoConfirm.setChecked(attachments_auto_confirm);
cbAutoConfirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("attachments_auto_confirm", isChecked).apply();
}
});
cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

@ -66,16 +66,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/pbDownloaded" /> app:layout_constraintTop_toBottomOf="@id/pbDownloaded" />
<CheckBox
android:id="@+id/cbNotAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_no_ask_again"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRemaining" />
<TextView <TextView
android:id="@+id/tvNoInternet" android:id="@+id/tvNoInternet"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -89,6 +79,25 @@
android:textColor="?attr/colorWarning" android:textColor="?attr/colorWarning"
app:drawableTint="?attr/colorWarning" app:drawableTint="?attr/colorWarning"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbNotAgain" /> app:layout_constraintTop_toBottomOf="@id/tvRemaining" />
<CheckBox
android:id="@+id/cbAutoConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/title_auto_confirm"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvNoInternet" />
<CheckBox
android:id="@+id/cbNotAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_no_ask_again"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbAutoConfirm" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView> </ScrollView>

Loading…
Cancel
Save