Skip to content

Commit 0f5552d

Browse files
committed
[Sync] Added Loading dialog
1 parent a264dd3 commit 0f5552d

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.davis.passwordmanager.dialog;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
7+
import androidx.annotation.NonNull;
8+
9+
import de.davis.passwordmanager.databinding.LoadingLayoutBinding;
10+
11+
public class LoadingDialog extends BaseDialogBuilder<LoadingDialog> {
12+
13+
private LoadingLayoutBinding binding;
14+
15+
public LoadingDialog(@NonNull Context context) {
16+
super(context);
17+
setCancelable(false);
18+
}
19+
20+
public void updateProgress(int current, int max){
21+
double progress = (current * 100d / max);
22+
binding.progress.setIndeterminate(false);
23+
binding.progress.setProgressCompat((int) progress, true);
24+
}
25+
26+
@Override
27+
public View onCreateView(LayoutInflater inflater) {
28+
binding = LoadingLayoutBinding.inflate(inflater);
29+
return binding.getRoot();
30+
}
31+
}

app/src/main/java/de/davis/passwordmanager/sync/DataTransfer.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import androidx.annotation.IntDef;
1313
import androidx.annotation.WorkerThread;
14+
import androidx.appcompat.app.AlertDialog;
1415
import androidx.core.os.HandlerCompat;
1516

1617
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@@ -21,6 +22,7 @@
2122
import javax.crypto.AEADBadTagException;
2223

2324
import de.davis.passwordmanager.R;
25+
import de.davis.passwordmanager.dialog.LoadingDialog;
2426

2527
public abstract class DataTransfer {
2628

@@ -32,6 +34,10 @@ public abstract class DataTransfer {
3234
@IntDef({TYPE_EXPORT, TYPE_IMPORT})
3335
public @interface Type{}
3436

37+
private LoadingDialog loadingDialog;
38+
39+
private final Handler handler = HandlerCompat.createAsync(Looper.getMainLooper());
40+
3541
public DataTransfer(Context context) {
3642
this.context = context;
3743
}
@@ -40,7 +46,7 @@ public Context getContext() {
4046
return context;
4147
}
4248

43-
protected void error(Handler handler, Exception exception){
49+
protected void error(Exception exception){
4450
handler.post(() -> {
4551
String msg = exception.getMessage();
4652
if(exception instanceof AEADBadTagException)
@@ -55,7 +61,7 @@ protected void error(Handler handler, Exception exception){
5561
});
5662
}
5763

58-
protected void handleResult(Handler handler, Result result){
64+
protected void handleResult(Result result){
5965
handler.post(() -> {
6066
if(result instanceof Result.Error error)
6167
new MaterialAlertDialogBuilder(getContext())
@@ -79,9 +85,18 @@ public void start(@Type int type, Uri uri){
7985
start(type, uri, null);
8086
}
8187

88+
protected void notifyUpdate(int current, int max){
89+
handler.post(() -> loadingDialog.updateProgress(current, max));
90+
}
91+
8292
protected void start(@Type int type, Uri uri, String password) {
8393
ContentResolver resolver = getContext().getContentResolver();
84-
Handler handler = HandlerCompat.createAsync(Looper.getMainLooper());
94+
95+
loadingDialog = new LoadingDialog(getContext())
96+
.setTitle(type == TYPE_EXPORT ? R.string.export : R.string.import_str)
97+
.setMessage(R.string.wait_text);
98+
AlertDialog alertDialog = loadingDialog.show();
99+
85100
doInBackground(() -> {
86101
Result result = null;
87102
try{
@@ -93,12 +108,14 @@ protected void start(@Type int type, Uri uri, String password) {
93108
if(result == null)
94109
return;
95110

96-
handleResult(handler, result);
111+
handleResult(result);
97112
}catch (Exception e){
98113
if(e instanceof NullPointerException)
99114
return;
100115

101-
error(handler, e);
116+
error(e);
117+
}finally {
118+
alertDialog.dismiss();
102119
}
103120
});
104121
}

app/src/main/java/de/davis/passwordmanager/sync/keygo/KeyGoTransfer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,19 @@ protected Result importElements(InputStream inputStream, String password) throws
100100
.blockingGet();
101101

102102
int existed = 0;
103-
for (SecureElement element : list) {
103+
int length = list.size();
104+
for (int i = 0; i < length; i++) {
105+
SecureElement element = list.get(i);
104106
if(elements.stream().anyMatch(e -> e.getTitle().equals(element.getTitle())
105107
&& e.getDetail().equals(element.getDetail()))) {
106108
existed++;
109+
110+
notifyUpdate(i+1, length);
107111
continue;
108112
}
109113

110114
SecureElementManager.getInstance().createElement(element);
115+
notifyUpdate(i+1, length);
111116
}
112117

113118
if(existed != 0)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
android:padding="24dp">
7+
8+
<com.google.android.material.progressindicator.LinearProgressIndicator
9+
android:id="@+id/progress"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:indeterminate="true"
13+
android:max="100" />
14+
</LinearLayout>

app/src/main/res/values-de/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,6 @@
143143
</plurals>
144144
<string name="invalid_file_length">Ungültige Datei Länge</string>
145145
<string name="invalid_file">Ungültige Datei</string>
146+
147+
<string name="wait_text">Das könnte ein wenig Zeit in Anspruch nehmen. Bitte haben Sie Geduld</string>
146148
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@
186186
</plurals>
187187
<string name="invalid_file_length">Invalid file length</string>
188188
<string name="invalid_file">Invalid file</string>
189+
190+
<string name="wait_text">This might take a little time. Please be patient</string>
189191
</resources>

0 commit comments

Comments
 (0)