download dialog
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v4.app.DialogFragment;
|
||||||
|
|
||||||
|
import net.headlezz.jokesbackend.myApi.model.Joke;
|
||||||
|
|
||||||
|
public class DownloadDialogFragment extends DialogFragment {
|
||||||
|
|
||||||
|
public static final String TAG = DownloadDialogFragment.class.getSimpleName();
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
ProgressDialog dialog = new ProgressDialog(getContext());
|
||||||
|
dialog.setCancelable(false);
|
||||||
|
dialog.setCanceledOnTouchOutside(false);
|
||||||
|
dialog.setIndeterminate(true);
|
||||||
|
dialog.setMessage(getContext().getString(R.string.dialog_loading));
|
||||||
|
return dialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
MyBus.getInstance().register(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
MyBus.getInstance().unregister(this);
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onEvent(Joke joke) {
|
||||||
|
dismiss();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onEvent(JokeLoaderTask.JokeDownloadException e) {
|
||||||
|
dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,19 +44,26 @@ public abstract class MainActivityFragment extends Fragment implements View.OnCl
|
|||||||
* cancels old downloads if theres already a running one
|
* cancels old downloads if theres already a running one
|
||||||
*/
|
*/
|
||||||
private void downloadNewJoke() {
|
private void downloadNewJoke() {
|
||||||
if(mJokeLoaderTask != null) {
|
|
||||||
mJokeLoaderTask.cancel(true);
|
|
||||||
}
|
|
||||||
mJokeLoaderTask = new JokeLoaderTask();
|
mJokeLoaderTask = new JokeLoaderTask();
|
||||||
mJokeLoaderTask.execute();
|
mJokeLoaderTask.execute();
|
||||||
|
DownloadDialogFragment frag = new DownloadDialogFragment();
|
||||||
|
frag.show(getFragmentManager(), DownloadDialogFragment.TAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows the joke activity with the given joke
|
||||||
|
* @param joke Joke to show
|
||||||
|
*/
|
||||||
public void onEvent(Joke joke) {
|
public void onEvent(Joke joke) {
|
||||||
Intent i = new Intent(getContext(), JokePresenterActivity.class);
|
Intent i = new Intent(getContext(), JokePresenterActivity.class);
|
||||||
i.putExtra(JokePresenterActivity.BUNDLE_ARG_JOKE, joke.getJoke());
|
i.putExtra(JokePresenterActivity.BUNDLE_ARG_JOKE, joke.getJoke());
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints an error message on the screen if we have a problem with the download
|
||||||
|
* @param e Exception
|
||||||
|
*/
|
||||||
public void onEvent(JokeLoaderTask.JokeDownloadException e) {
|
public void onEvent(JokeLoaderTask.JokeDownloadException e) {
|
||||||
if (getActivity() != null)
|
if (getActivity() != null)
|
||||||
Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.joke_download_error, Snackbar.LENGTH_LONG)
|
Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.joke_download_error, Snackbar.LENGTH_LONG)
|
||||||
|
|||||||
@@ -6,4 +6,5 @@
|
|||||||
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
|
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
|
||||||
<string name="joke_download_error">Failed to load joke</string>
|
<string name="joke_download_error">Failed to load joke</string>
|
||||||
<string name="retry">Retry</string>
|
<string name="retry">Retry</string>
|
||||||
|
<string name="dialog_loading">Loading ...</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user