From cf87981d5f170ad3ca7388c385e95fb8287e0868 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 22 Mar 2017 22:05:17 +0100 Subject: [PATCH] add Loading/Error fragment --- .../chaosflix/activities/EventsActivity.java | 2 +- .../fragments/BrowseErrorFragment.java | 79 +++++++++++++++++++ .../fragments/ConferencesBrowseFragment.java | 13 +++ .../fragments/EventsBrowseFragment.java | 11 ++- .../layout/activity_conferences_browse.xml | 9 ++- ...ty_main.xml => activity_events_browse.xml} | 2 +- app/src/main/res/values/dimens.xml | 2 + 7 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/de/nicidienase/chaosflix/fragments/BrowseErrorFragment.java rename app/src/main/res/layout/{activity_main.xml => activity_events_browse.xml} (90%) diff --git a/app/src/main/java/de/nicidienase/chaosflix/activities/EventsActivity.java b/app/src/main/java/de/nicidienase/chaosflix/activities/EventsActivity.java index 2fbd15e0..68658f14 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/activities/EventsActivity.java +++ b/app/src/main/java/de/nicidienase/chaosflix/activities/EventsActivity.java @@ -13,6 +13,6 @@ public class EventsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + setContentView(R.layout.activity_events_browse); } } diff --git a/app/src/main/java/de/nicidienase/chaosflix/fragments/BrowseErrorFragment.java b/app/src/main/java/de/nicidienase/chaosflix/fragments/BrowseErrorFragment.java new file mode 100644 index 00000000..0856b301 --- /dev/null +++ b/app/src/main/java/de/nicidienase/chaosflix/fragments/BrowseErrorFragment.java @@ -0,0 +1,79 @@ +package de.nicidienase.chaosflix.fragments; + +import android.app.Fragment; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v17.leanback.app.ErrorFragment; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ProgressBar; + +import de.nicidienase.chaosflix.R; + +/** + * Created by felix on 22.03.17. + */ + +public class BrowseErrorFragment extends ErrorFragment{ + + private static final boolean TRANSLUCENT = true; + private SpinnerFragment mSpinnerFragment; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mSpinnerFragment = new SpinnerFragment(); + getFragmentManager().beginTransaction().add(R.id.browse_fragment, mSpinnerFragment).commit(); + } + + @Override + public void onStop() { + super.onStop(); + getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit(); + } + + public void setErrorContent(int resourceId){ + setErrorContent(getResources().getString(resourceId)); + } + + public void setErrorContent(String message) { + setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null)); + setMessage(message); + setDefaultBackground(TRANSLUCENT); + + setButtonText(getResources().getString(R.string.dismiss_error)); + setButtonClickListener(new View.OnClickListener() { + @Override + public void onClick(View arg0) { + dismiss(); + } + }); + } + + public void dismiss() { + getFragmentManager().beginTransaction().remove(BrowseErrorFragment.this).commit(); + getFragmentManager().popBackStack(); + } + + + public static class SpinnerFragment extends Fragment { + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ProgressBar progressBar = new ProgressBar(container.getContext()); + if (container instanceof FrameLayout) { + Resources res = getResources(); + int width = res.getDimensionPixelSize(R.dimen.spinner_width); + int height = res.getDimensionPixelSize(R.dimen.spinner_height); + FrameLayout.LayoutParams layoutParams = + new FrameLayout.LayoutParams(width, height, Gravity.CENTER); + progressBar.setLayoutParams(layoutParams); + } + return progressBar; + } + } +} diff --git a/app/src/main/java/de/nicidienase/chaosflix/fragments/ConferencesBrowseFragment.java b/app/src/main/java/de/nicidienase/chaosflix/fragments/ConferencesBrowseFragment.java index 70e6e3d2..123425fe 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/fragments/ConferencesBrowseFragment.java +++ b/app/src/main/java/de/nicidienase/chaosflix/fragments/ConferencesBrowseFragment.java @@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.fragments; import android.os.Bundle; import android.support.v17.leanback.app.BrowseFragment; +import android.support.v17.leanback.app.ErrorFragment; import android.support.v17.leanback.widget.ArrayObjectAdapter; import android.support.v17.leanback.widget.HeaderItem; import android.support.v17.leanback.widget.ListRow; @@ -14,6 +15,7 @@ import java.util.Map; import java.util.Set; import de.nicidienase.chaosflix.CardPresenter; +import de.nicidienase.chaosflix.R; import de.nicidienase.chaosflix.entities.Conference; import de.nicidienase.chaosflix.entities.Conferences; import de.nicidienase.chaosflix.network.MediaCCCClient; @@ -31,12 +33,14 @@ public class ConferencesBrowseFragment extends BrowseFragment { private MediaCCCClient mMediaCCCClient; private ArrayObjectAdapter mRowsAdapter; private Map> mConferences; + private BrowseErrorFragment mErrorFragment; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMediaCCCClient = new MediaCCCClient(); + final BrowseErrorFragment errorFragment = showErrorFragment(); mMediaCCCClient.listConferences().enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { @@ -55,12 +59,14 @@ public class ConferencesBrowseFragment extends BrowseFragment { addRow(mConferences, cardPresenter, tag); } } + errorFragment.dismiss(); setAdapter(mRowsAdapter); } @Override public void onFailure(Call call, Throwable t) { Log.d(TAG,"Error loading conferences",t); + errorFragment.setErrorContent(t.getMessage()); t.printStackTrace(); } }); @@ -75,6 +81,13 @@ public class ConferencesBrowseFragment extends BrowseFragment { mRowsAdapter.add(new ListRow(header, listRowAdapter)); } + private BrowseErrorFragment showErrorFragment(){ + BrowseErrorFragment errorFragment = new BrowseErrorFragment(); + getFragmentManager().beginTransaction().replace(R.id.browse_fragment, errorFragment) + .addToBackStack(null).commit(); + return errorFragment; + } + private String getStringForTag(String tag) { switch (tag){ case "congress": diff --git a/app/src/main/java/de/nicidienase/chaosflix/fragments/EventsBrowseFragment.java b/app/src/main/java/de/nicidienase/chaosflix/fragments/EventsBrowseFragment.java index eeb8dcb1..728896dd 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/fragments/EventsBrowseFragment.java +++ b/app/src/main/java/de/nicidienase/chaosflix/fragments/EventsBrowseFragment.java @@ -86,7 +86,7 @@ public class EventsBrowseFragment extends BrowseFragment { public void onActivityCreated(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); super.onActivityCreated(savedInstanceState); - + final BrowseErrorFragment errorFragment = showErrorFragment(); conferenceId = this.getActivity().getIntent().getIntExtra(EventsActivity.CONFERENCE_ID, 0); conference = this.getActivity().getIntent().getParcelableExtra(EventsActivity.CONFERENCE); client.getConference(conference.getApiID()).enqueue(new Callback() { @@ -95,11 +95,13 @@ public class EventsBrowseFragment extends BrowseFragment { conference = response.body(); setupUIElements(); loadRows(); + errorFragment.dismiss(); } @Override public void onFailure(Call call, Throwable t) { Log.d(TAG,"Error loading conferences",t); + errorFragment.setErrorContent(t.getMessage()); t.printStackTrace(); } }); @@ -116,6 +118,13 @@ public class EventsBrowseFragment extends BrowseFragment { } } + private BrowseErrorFragment showErrorFragment(){ + BrowseErrorFragment errorFragment = new BrowseErrorFragment(); + getFragmentManager().beginTransaction().replace(R.id.browse_fragment, errorFragment) + .addToBackStack(null).commit(); + return errorFragment; + } + private void loadRows() { HashMap> eventsByTags = conference.getEventsByTags(); diff --git a/app/src/main/res/layout/activity_conferences_browse.xml b/app/src/main/res/layout/activity_conferences_browse.xml index 97aa69a4..5a3b47b9 100644 --- a/app/src/main/res/layout/activity_conferences_browse.xml +++ b/app/src/main/res/layout/activity_conferences_browse.xml @@ -1,6 +1,7 @@ \ No newline at end of file + android:name="de.nicidienase.chaosflix.fragments.ConferencesBrowseFragment" + android:id="@+id/browse_fragment" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"/> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_events_browse.xml similarity index 90% rename from app/src/main/res/layout/activity_main.xml rename to app/src/main/res/layout/activity_events_browse.xml index 33f4337b..ff5efda1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_events_browse.xml @@ -1,5 +1,5 @@ - 274dp 274dp + 50dp + 50dp \ No newline at end of file