Skip to content

Commit 9f63337

Browse files
committed
ProgressDialogFragment implements ProgressMonitor.
1 parent 9fa7837 commit 9f63337

File tree

12 files changed

+320
-138
lines changed

12 files changed

+320
-138
lines changed

.idea/.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/NavHostActivity.xml renamed to .idea/runConfigurations/mobile.xml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mobile/src/androidTest/java/io/syslogic/github/TestRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
* @author Martin Zeitler
2020
*/
21+
@Deprecated
2122
@RunWith(AndroidJUnit4.class)
2223
public class TestRepository extends TestSuite {
2324

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.syslogic.github.dialog;
2+
3+
import android.app.Dialog;
4+
import android.content.SharedPreferences;
5+
import android.os.Bundle;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import androidx.core.content.ContextCompat;
13+
import androidx.databinding.ViewDataBinding;
14+
import androidx.fragment.app.DialogFragment;
15+
import androidx.preference.PreferenceManager;
16+
17+
import io.syslogic.github.BuildConfig;
18+
import io.syslogic.github.R;
19+
20+
/**
21+
* Base {@link DialogFragment}
22+
*
23+
* @author Martin Zeitler
24+
*/
25+
abstract public class BaseDialogFragment extends DialogFragment {
26+
27+
/** Debug Output */
28+
protected static final boolean mDebug = BuildConfig.DEBUG;
29+
30+
@Nullable protected ViewDataBinding mDataBinding = null;
31+
protected SharedPreferences prefs;
32+
@Nullable protected Dialog dialog;
33+
34+
/** By default the dialog can be closed by touching outside of it */
35+
protected boolean cancelOnTouchOutSide = true;
36+
37+
public BaseDialogFragment() {}
38+
39+
/**
40+
* onCreate()
41+
* @param savedInstanceState reference to the Bundle object, which is being passed into the method.
42+
**/
43+
@Override
44+
public void onCreate(@Nullable Bundle savedInstanceState) {
45+
super.onCreate(savedInstanceState);
46+
this.prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity());
47+
}
48+
49+
/** classes which extend this class must implement method onCreateView, due to data-binding: */
50+
@Nullable
51+
abstract public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);
52+
53+
/**
54+
* onCreateDialog()
55+
* @param savedInstanceState reference to the Bundle object, which is being passed into the method.
56+
**/
57+
@Override
58+
@NonNull
59+
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
60+
super.onCreateDialog(savedInstanceState);
61+
this.dialog = new Dialog(this.requireContext());
62+
this.dialog.setCanceledOnTouchOutside(this.cancelOnTouchOutSide);
63+
if (this.dialog.getWindow() != null) {
64+
this.dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
65+
this.dialog.getWindow().setNavigationBarColor(ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark));
66+
}
67+
return this.dialog;
68+
}
69+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.syslogic.github.dialog;
2+
3+
import android.annotation.SuppressLint;
4+
import android.os.Bundle;
5+
import android.util.Log;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
13+
import org.eclipse.jgit.lib.ProgressMonitor;
14+
15+
import io.syslogic.github.databinding.DialogProgressBinding;
16+
import io.syslogic.github.R;
17+
18+
/**
19+
* Progress {@link BaseDialogFragment}
20+
*
21+
* @author Martin Zeitler
22+
*/
23+
public class ProgressDialogFragment extends BaseDialogFragment implements ProgressMonitor {
24+
25+
/** Log Tag */
26+
public static final String LOG_TAG = ProgressDialogFragment.class.getSimpleName();
27+
28+
/** layout resId kept for reference */
29+
@SuppressWarnings("unused") private final int resId = R.layout.dialog_progress;
30+
31+
/** The Data-Binding */
32+
DialogProgressBinding mDataBinding;
33+
34+
private String repoName = null;
35+
36+
private boolean taskCancelled = false;
37+
private int totalTasks = 0;
38+
private int currentTask = 0;
39+
private int totalWork = 0;
40+
private int currentWork = 0;
41+
public ProgressDialogFragment() {}
42+
43+
/** Inflate the data-binding */
44+
@NonNull
45+
@Override
46+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
47+
this.mDataBinding = DialogProgressBinding.inflate(inflater, container, false);
48+
this.mDataBinding.repositoryName.setText(this.repoName);
49+
this.mDataBinding.buttonCancel.setOnClickListener((view) -> this.taskCancelled = true);
50+
return this.mDataBinding.getRoot();
51+
}
52+
53+
/** Being called early on... */
54+
public void setRepositoryName(String repoName) {
55+
this.repoName = repoName;
56+
}
57+
58+
/** ProgressMonitor: Advise the monitor of the total number of subtasks. */
59+
@Override
60+
public void start(int totalTasks) {
61+
this.totalTasks = totalTasks;
62+
}
63+
64+
/** ProgressMonitor: Begin processing a single task. */
65+
@SuppressLint("SetTextI18n")
66+
@Override
67+
public void beginTask(String title, int totalWork) {
68+
this.currentTask++;
69+
this.totalWork = totalWork;
70+
this.currentWork = 0;
71+
if (mDebug) {
72+
Log.d(LOG_TAG, "beginTask " + this.currentTask + ": " + title + ": " + totalWork + " items");
73+
}
74+
requireActivity().runOnUiThread(() ->
75+
this.mDataBinding.textCurrentTaskTitle.setText(title));
76+
}
77+
78+
/** ProgressMonitor: Denote that some work units have been completed. */
79+
@SuppressLint("SetTextI18n")
80+
@Override
81+
public void update(int completed) {
82+
this.currentWork += completed;
83+
String message = this.currentWork + " / " + this.totalWork;
84+
if (mDebug) {Log.d(LOG_TAG, "items complete: " + message);}
85+
requireActivity().runOnUiThread(() ->
86+
this.mDataBinding.textDownloadStatus.setText(message)
87+
);
88+
}
89+
90+
/** ProgressMonitor: Finish the current task, so the next can begin. */
91+
@Override
92+
public void endTask() {
93+
if (mDebug) {Log.d(LOG_TAG, "endTask " + this.currentTask);}
94+
if (this.currentTask == this.totalTasks) {this.dismiss();}
95+
}
96+
97+
/** Interface: ProgressMonitor. Check for user task cancellation. */
98+
@Override
99+
public boolean isCancelled() {
100+
if (this.taskCancelled) {this.dismiss();}
101+
return this.taskCancelled;
102+
}
103+
}

mobile/src/main/java/io/syslogic/github/fragment/RepositoryFragment.java

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.webkit.WebViewClient;
2121
import android.widget.AdapterView;
2222
import android.widget.Toast;
23-
import android.widget.ViewFlipper;
2423

2524
import com.google.gson.JsonObject;
2625
import com.google.gson.JsonParser;
@@ -32,7 +31,6 @@
3231

3332
import androidx.annotation.NonNull;
3433
import androidx.annotation.Nullable;
35-
import androidx.annotation.RequiresApi;
3634
import androidx.appcompat.widget.AppCompatSpinner;
3735
import androidx.databinding.ViewDataBinding;
3836

@@ -46,6 +44,7 @@
4644
import io.syslogic.github.R;
4745
import io.syslogic.github.Constants;
4846
import io.syslogic.github.databinding.FragmentRepositoryBinding;
47+
import io.syslogic.github.dialog.ProgressDialogFragment;
4948
import io.syslogic.github.model.User;
5049
import io.syslogic.github.network.TokenCallback;
5150
import io.syslogic.github.retrofit.GithubClient;
@@ -64,7 +63,7 @@
6463
*
6564
* @author Martin Zeitler
6665
*/
67-
public class RepositoryFragment extends BaseFragment implements TokenCallback, ProgressMonitor {
66+
public class RepositoryFragment extends BaseFragment implements TokenCallback {
6867

6968
/** Log Tag */
7069
@SuppressWarnings("unused")
@@ -74,8 +73,8 @@ public class RepositoryFragment extends BaseFragment implements TokenCallback, P
7473

7574
/** Data Binding */
7675
FragmentRepositoryBinding mDataBinding;
77-
78-
private Long itemId = 0L;
76+
ProgressDialogFragment currentDialog;
77+
Long itemId = 0L;
7978

8079
/** Constructor */
8180
public RepositoryFragment() {}
@@ -90,7 +89,6 @@ public static RepositoryFragment newInstance(long itemId) {
9089
}
9190

9291
@Override
93-
@RequiresApi(api = Build.VERSION_CODES.N)
9492
public void onCreate(@Nullable Bundle savedInstanceState) {
9593
super.onCreate(savedInstanceState);
9694
this.registerBroadcastReceiver();
@@ -169,16 +167,17 @@ public void onNothingSelected(AdapterView<?> parent) {}
169167
// directory should be empty.
170168
if (destination.exists()) {
171169
if (! destination.delete()) {
172-
Log.e(LOG_TAG, "destination not deleted");
170+
Log.e(LOG_TAG, "destination directory not deleted");
173171
return;
174172
}
175173
}
176174
if (! destination.exists()) {
177175
if (! destination.mkdir()) {
178-
Log.e(LOG_TAG, "destination not created");
176+
Log.e(LOG_TAG, "destination directory not created");
177+
return;
179178
}
180179
}
181-
if (destination.exists()) {
180+
if (destination.exists() && destination.isDirectory()) {
182181
String branch = getDataBinding().toolbarDownload.spinnerBranch.getSelectedItem().toString();
183182
gitClone(destination, branch);
184183
}
@@ -190,11 +189,17 @@ public void onNothingSelected(AdapterView<?> parent) {}
190189

191190
/** git clone. */
192191
private void gitClone(@NonNull File destination, @Nullable String branch) {
192+
193+
/* Attempting to display the checkout progress. */
194+
this.currentDialog = new ProgressDialogFragment();
195+
this.currentDialog.show(getChildFragmentManager(), ProgressDialogFragment.LOG_TAG);
196+
this.currentDialog.setRepositoryName(getRepoName());
197+
193198
Thread thread = new Thread(() -> {
194199
CloneCommand cmd = Git.cloneRepository()
195200
.setURI(getRepoUrl())
196201
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(getPersonalAccessToken(), ""))
197-
.setProgressMonitor(RepositoryFragment.this)
202+
.setProgressMonitor((ProgressMonitor) currentDialog)
198203
.setDirectory(destination)
199204
.setRemote("github");
200205

@@ -204,19 +209,16 @@ private void gitClone(@NonNull File destination, @Nullable String branch) {
204209
cmd.setBranch(branch);
205210
}
206211

212+
if (mDebug) {Log.d(LOG_TAG, "Cloning into " + getRepoName() + "...");}
207213
try {
208-
if (mDebug) {Log.d(LOG_TAG, "Cloning into " + getRepoName() + "...");}
209214
cmd.call();
210215
} catch (GitAPIException | JGitInternalException | NoSuchMethodError e) {
211216
String message = e.getMessage();
212217
if (mDebug) {Log.e(LOG_TAG, e.getMessage(), e);}
213-
requireActivity().runOnUiThread(() ->
214-
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show());
215-
} finally {
216-
String message = "Cloned.";
217-
if (mDebug) {Log.d(LOG_TAG, message);}
218-
requireActivity().runOnUiThread(() ->
219-
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show());
218+
requireActivity().runOnUiThread(() -> {
219+
this.currentDialog.dismiss();
220+
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show();
221+
});
220222
}
221223
});
222224
thread.start();
@@ -538,47 +540,6 @@ public void showDownloads() {
538540
startActivity(intent);
539541
}
540542

541-
@SuppressWarnings("unused")
542-
private void switchToolbarView(@NonNull Integer childIndex) {
543-
ViewFlipper view = this.mDataBinding.toolbarDownload.viewflipperDownload;
544-
int index = view.getDisplayedChild();
545-
switch(childIndex) {
546-
case 0: if (index != childIndex) {view.showPrevious();} break;
547-
case 1: if (index != childIndex) {view.showNext();} break;
548-
}
549-
}
550-
551543
@Override
552544
public void onLogin(@NonNull User item) {}
553-
554-
/** Interface: ProgressMonitor */
555-
@Override
556-
public void start(int totalTasks) {
557-
if (mDebug) {Log.d(LOG_TAG, "totalTasks: " + totalTasks);}
558-
}
559-
560-
/** Interface: ProgressMonitor */
561-
@Override
562-
public void beginTask(String title, int totalWork) {
563-
if (mDebug) {Log.d(LOG_TAG, "beginTask " + title + ": " + totalWork);}
564-
}
565-
566-
/** Interface: ProgressMonitor */
567-
@Override
568-
public void update(int completed) {
569-
if (mDebug) {Log.d(LOG_TAG, "completed: +" + completed);}
570-
}
571-
572-
/** Interface: ProgressMonitor */
573-
@Override
574-
public void endTask() {
575-
if (mDebug) {Log.d(LOG_TAG, "endTask");}
576-
}
577-
578-
/** Interface: ProgressMonitor */
579-
@Override
580-
public boolean isCancelled() {
581-
// if (mDebug) {Log.d(LOG_TAG, "isCancelled?");}
582-
return false;
583-
}
584545
}

0 commit comments

Comments
 (0)