add Loading/Error fragment

This commit is contained in:
Felix 2017-03-22 22:05:17 +01:00
parent c4d561e79a
commit d9d7bebe54
7 changed files with 111 additions and 7 deletions

View file

@ -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);
}
}

View file

@ -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;
}
}
}

View file

@ -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<String, List<Conference>> 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<Conferences>() {
@Override
public void onResponse(Call<Conferences> call, Response<Conferences> response) {
@ -55,12 +59,14 @@ public class ConferencesBrowseFragment extends BrowseFragment {
addRow(mConferences, cardPresenter, tag);
}
}
errorFragment.dismiss();
setAdapter(mRowsAdapter);
}
@Override
public void onFailure(Call<Conferences> 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":

View file

@ -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<Conference>() {
@ -95,11 +95,13 @@ public class EventsBrowseFragment extends BrowseFragment {
conference = response.body();
setupUIElements();
loadRows();
errorFragment.dismiss();
}
@Override
public void onFailure(Call<Conference> 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<String, List<Event>> eventsByTags = conference.getEventsByTags();

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="de.nicidienase.chaosflix.fragments.ConferencesBrowseFragment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
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"/>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment android:id="@+id/main_browse_fragment"
<fragment android:id="@+id/browse_fragment"
android:name="de.nicidienase.chaosflix.fragments.EventsBrowseFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

View file

@ -2,4 +2,6 @@
<resources>
<dimen name="detail_thumb_width">274dp</dimen>
<dimen name="detail_thumb_height">274dp</dimen>
<dimen name="spinner_width">50dp</dimen>
<dimen name="spinner_height">50dp</dimen>
</resources>