Skip to content

Commit 6685e4d

Browse files
committed
Load Gist in background task when missing from store
The pager will only be configured to displays the Gist's files once the Gist has been refreshed.
1 parent ca9d4d7 commit 6685e4d

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

app/src/main/java/com/github/mobile/ui/gist/GistFilesViewActivity.java

+59-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@
2222
import android.content.Intent;
2323
import android.os.Bundle;
2424
import android.support.v4.view.ViewPager;
25+
import android.widget.ProgressBar;
2526

2627
import com.actionbarsherlock.app.ActionBar;
2728
import com.actionbarsherlock.view.MenuItem;
2829
import com.github.mobile.Intents.Builder;
2930
import com.github.mobile.R.id;
3031
import com.github.mobile.R.layout;
3132
import com.github.mobile.R.string;
33+
import com.github.mobile.core.gist.FullGist;
3234
import com.github.mobile.core.gist.GistStore;
35+
import com.github.mobile.core.gist.RefreshGistTask;
3336
import com.github.mobile.util.AvatarLoader;
37+
import com.github.mobile.util.HttpImageGetter;
38+
import com.github.mobile.util.ViewUtils;
3439
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
3540
import com.google.inject.Inject;
3641
import com.viewpagerindicator.TitlePageIndicator;
@@ -39,6 +44,7 @@
3944
import org.eclipse.egit.github.core.User;
4045

4146
import roboguice.inject.InjectExtra;
47+
import roboguice.inject.InjectView;
4248

4349
/**
4450
* Activity to page through the content of all the files in a Gist
@@ -63,6 +69,15 @@ public static Intent createIntent(Gist gist, int position) {
6369
@InjectExtra(EXTRA_POSITION)
6470
private int initialPosition;
6571

72+
@InjectView(id.vp_pages)
73+
private ViewPager pager;
74+
75+
@InjectView(id.pb_loading)
76+
private ProgressBar loadingBar;
77+
78+
@InjectView(id.tpi_header)
79+
private TitlePageIndicator indicator;
80+
6681
private Gist gist;
6782

6883
@Inject
@@ -71,39 +86,74 @@ public static Intent createIntent(Gist gist, int position) {
7186
@Inject
7287
private AvatarLoader avatars;
7388

89+
@Inject
90+
private HttpImageGetter imageGetter;
91+
7492
@Override
7593
protected void onCreate(Bundle savedInstanceState) {
7694
super.onCreate(savedInstanceState);
7795

7896
setContentView(layout.pager_with_title);
7997

98+
if (initialPosition < 0)
99+
initialPosition = 0;
100+
101+
getSupportActionBar().setTitle(getString(string.gist_title) + gistId);
102+
80103
gist = store.getGist(gistId);
104+
if (gist != null)
105+
configurePager();
106+
else {
107+
ViewUtils.setGone(loadingBar, false);
108+
ViewUtils.setGone(pager, true);
109+
ViewUtils.setGone(indicator, true);
110+
new RefreshGistTask(this, gistId, imageGetter) {
111+
112+
@Override
113+
protected void onSuccess(FullGist gist) throws Exception {
114+
super.onSuccess(gist);
115+
116+
GistFilesViewActivity.this.gist = gist.getGist();
117+
configurePager();
118+
}
119+
120+
}.execute();
121+
}
122+
}
81123

124+
private void configurePager() {
82125
ActionBar actionBar = getSupportActionBar();
83126
actionBar.setDisplayHomeAsUpEnabled(true);
84-
actionBar.setTitle(getString(string.gist_title) + gistId);
85127
User author = gist.getUser();
86128
if (author != null) {
87129
actionBar.setSubtitle(author.getLogin());
88130
avatars.bind(actionBar, author);
89131
} else
90132
actionBar.setSubtitle(string.anonymous);
91133

92-
ViewPager pager = (ViewPager) findViewById(id.vp_pages);
93-
pager.setAdapter(new GistFilesPagerAdapter(getSupportFragmentManager(),
94-
gist));
95-
((TitlePageIndicator) findViewById(id.tpi_header)).setViewPager(pager);
134+
ViewUtils.setGone(loadingBar, true);
135+
ViewUtils.setGone(pager, false);
136+
ViewUtils.setGone(indicator, false);
137+
138+
GistFilesPagerAdapter pagerAdapter = new GistFilesPagerAdapter(
139+
getSupportFragmentManager(), gist);
140+
pager.setAdapter(pagerAdapter);
141+
indicator.setViewPager(pager);
96142

97-
pager.setCurrentItem(initialPosition);
143+
if (initialPosition < pagerAdapter.getCount())
144+
pager.setCurrentItem(initialPosition);
98145
}
99146

100147
@Override
101148
public boolean onOptionsItemSelected(MenuItem item) {
102149
switch (item.getItemId()) {
103150
case android.R.id.home:
104-
Intent intent = GistsViewActivity.createIntent(gist);
105-
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
106-
startActivity(intent);
151+
if (gist != null) {
152+
Intent intent = GistsViewActivity.createIntent(gist);
153+
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP
154+
| FLAG_ACTIVITY_SINGLE_TOP);
155+
startActivity(intent);
156+
}
107157
return true;
108158
default:
109159
return super.onOptionsItemSelected(item);

0 commit comments

Comments
 (0)