diff options
author | David-Development <david-dev@live.de> | 2021-11-07 17:31:36 +0300 |
---|---|---|
committer | David Luhmer <david-dev@live.de> | 2021-11-07 17:31:36 +0300 |
commit | 2b98fcc9e651fd6d5375dcf197eea4b2923427d3 (patch) | |
tree | dfab1bb83a864d750509dbb7ca69c402a37689dd /News-Android-App/src/main/java | |
parent | 9d7dac8825f739d43e4739a1b51f1ad062e45ad5 (diff) |
improve OMPL import dialog
Signed-off-by: David Luhmer <david-dev@live.de>
Diffstat (limited to 'News-Android-App/src/main/java')
2 files changed, 124 insertions, 20 deletions
diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewFeedActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewFeedActivity.java index d158c947..8887da72 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewFeedActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewFeedActivity.java @@ -5,10 +5,8 @@ import static java.util.Objects.requireNonNull; import android.Manifest; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; -import android.app.ProgressDialog; import android.content.ContentResolver; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; @@ -29,6 +27,8 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentTransaction; import org.json.JSONException; import org.json.JSONObject; @@ -249,7 +249,7 @@ public class NewFeedActivity extends AppCompatActivity { private final String mUrlToFile; private HashMap<String, String> extractedUrls; - private ProgressDialog pd; + private NewsReaderOPMLImportDialogFragment pd; private final Context mContext; ImportOpmlSubscriptionsTask(String urlToFile, Context context) { @@ -259,21 +259,30 @@ public class NewFeedActivity extends AppCompatActivity { @Override protected void onPreExecute() { - pd = new ProgressDialog(mContext); - pd.setTitle(getString(R.string.parsing_opml)); - pd.setMessage(getString(R.string.please_wait)); - pd.setCancelable(false); - pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - pd.show(); + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + Fragment prev = getSupportFragmentManager().findFragmentByTag("news_reader_opml_import_dialog"); + if (prev != null) { + ft.remove(prev); + } + ft.addToBackStack(null); + pd = NewsReaderOPMLImportDialogFragment.newInstance(false); + pd.show(ft, "news_reader_opml_import_dialog"); super.onPreExecute(); } @Override protected Boolean doInBackground(Void... params) { + try { + // wait for NewsReaderOPMLImportDialogFragment to be visible + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + String opmlContent; try { - if(mUrlToFile.startsWith("http")) {//http[s] + if (mUrlToFile.startsWith("http")) {//http[s] opmlContent = URLConnectionReader.getText(mUrlToFile); } else { opmlContent = getStringFromFile(mUrlToFile); @@ -287,7 +296,7 @@ public class NewFeedActivity extends AppCompatActivity { extractedUrls = OpmlXmlParser.ReadFeed(parser); List<String> result = new ArrayList<>(); - publishProgress(result); + publishProgress(new ArrayList<>(result)); final HashMap<String, Long> existingFolders = new HashMap<>(); @@ -321,14 +330,15 @@ public class NewFeedActivity extends AppCompatActivity { Log.d(TAG, "Successfully imported feed: " + feedUrl + " - Feed-ID: " + feed.getId()); } else if (response.code() == 409) { // already exists - result.add("โ " + " - " + feedUrl); + result.add("โค " + feedUrl); } else { result.add("โ " + response.code() + " - " + feedUrl); Log.e(TAG, "Failed to import feed: " + feedUrl + " - Status-Code: " + response.code()); Log.e(TAG, response.errorBody().string()); } - publishProgress(result); + // make list immutable and report it as progress + publishProgress(new ArrayList<>(result)); } } catch (Exception e) { e.printStackTrace(); @@ -339,28 +349,28 @@ public class NewFeedActivity extends AppCompatActivity { @Override protected void onProgressUpdate(List<String>... values) { - StringBuilder text = new StringBuilder("This might take a few minutes.. please wait:\n"); + // StringBuilder text = new StringBuilder("This might take a few minutes.. please wait..\n"); + StringBuilder text = new StringBuilder(); List<String> log = values[0]; for (String line : log) { text.append("\n").append(line); } - pd.setMax(extractedUrls.size()); - pd.setProgress(log.size()); - pd.setMessage(text.toString()); + pd.updateProgress(log.size(), extractedUrls.size()); + pd.setMessage(text.toString().trim()); super.onProgressUpdate(values); } @Override protected void onPostExecute(Boolean result) { - pd.setButton(DialogInterface.BUTTON_POSITIVE, getString(android.R.string.yes), (dialog, which) -> pd.dismiss()); + pd.setVisibilityOkButton(true); if(!result) { Toast.makeText(mContext, "Failed to parse OPML file", Toast.LENGTH_SHORT).show(); } else { - Toast.makeText(mContext, "Imported done!", Toast.LENGTH_LONG).show(); + Toast.makeText(mContext, "Import done!", Toast.LENGTH_LONG).show(); } super.onPostExecute(result); @@ -368,7 +378,6 @@ public class NewFeedActivity extends AppCompatActivity { } - /** * Attempts to sign in or register the account specified by the login form. * If there are form errors (invalid email, missing fields, etc.), the diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderOPMLImportDialogFragment.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderOPMLImportDialogFragment.java new file mode 100644 index 00000000..3ae7bdf1 --- /dev/null +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderOPMLImportDialogFragment.java @@ -0,0 +1,95 @@ +package de.luhmer.owncloudnewsreader; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.FragmentTransaction; + +import javax.inject.Inject; + +import de.luhmer.owncloudnewsreader.databinding.FragmentDialogOpmlImportBinding; +import de.luhmer.owncloudnewsreader.di.ApiProvider; +import io.reactivex.annotations.NonNull; + + +public class NewsReaderOPMLImportDialogFragment extends DialogFragment { + + private static final String TAG = NewsReaderOPMLImportDialogFragment.class.getCanonicalName(); + protected @Inject + ApiProvider mApi; + + protected FragmentDialogOpmlImportBinding binding; + + + static NewsReaderOPMLImportDialogFragment newInstance(boolean showOkButton) { + var f = new NewsReaderOPMLImportDialogFragment(); + + Bundle args = new Bundle(); + args.putBoolean("show_ok_button", showOkButton); + + f.setArguments(args); + return f; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FloatingDialog); + } + + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + binding = FragmentDialogOpmlImportBinding.inflate(inflater, container, false); + + final Bundle args = requireArguments(); + boolean showOkButton = args.getBoolean("show_ok_button", true); + setVisibilityOkButton(showOkButton); + + binding.okButton.setOnClickListener(v -> { + FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); + ft.remove(this); + ft.commit(); + }); + + return binding.getRoot(); + } + + public void updateProgress(final int current, final int max) { + if (binding != null) { + binding.opmlImportProgress.setMax(max); + binding.opmlImportProgress.setProgress(current); + + int percentage = Math.round((float) current / (float) max * 100f); + Log.d(TAG, current + "-" + max + "- " + percentage); + binding.tvPercentage.setText(String.format("%d%%", percentage)); + binding.tvAbsoluteProgress.setText(String.format("%d / %d", current, max)); + } else { + Log.e(TAG, "Binding is not ready yet"); + } + } + + public void setMessage(final String message) { + if (binding != null) { + binding.tvMessage.setText(message); + + binding.messageScrollview.post(() -> binding.messageScrollview.fullScroll(View.FOCUS_DOWN)); + } else { + Log.e(TAG, "Binding is not ready yet"); + } + } + + public void setVisibilityOkButton(final boolean show) { + if (binding != null) { + binding.okButton.setVisibility(show ? View.VISIBLE : View.GONE); + } else { + Log.e(TAG, "Binding is not ready yet"); + } + } +} |