22
22
import android .content .Intent ;
23
23
import android .os .Bundle ;
24
24
import android .support .v4 .view .ViewPager ;
25
+ import android .widget .ProgressBar ;
25
26
26
27
import com .actionbarsherlock .app .ActionBar ;
27
28
import com .actionbarsherlock .view .MenuItem ;
28
29
import com .github .mobile .Intents .Builder ;
29
30
import com .github .mobile .R .id ;
30
31
import com .github .mobile .R .layout ;
31
32
import com .github .mobile .R .string ;
33
+ import com .github .mobile .core .gist .FullGist ;
32
34
import com .github .mobile .core .gist .GistStore ;
35
+ import com .github .mobile .core .gist .RefreshGistTask ;
33
36
import com .github .mobile .util .AvatarLoader ;
37
+ import com .github .mobile .util .HttpImageGetter ;
38
+ import com .github .mobile .util .ViewUtils ;
34
39
import com .github .rtyley .android .sherlock .roboguice .activity .RoboSherlockFragmentActivity ;
35
40
import com .google .inject .Inject ;
36
41
import com .viewpagerindicator .TitlePageIndicator ;
39
44
import org .eclipse .egit .github .core .User ;
40
45
41
46
import roboguice .inject .InjectExtra ;
47
+ import roboguice .inject .InjectView ;
42
48
43
49
/**
44
50
* 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) {
63
69
@ InjectExtra (EXTRA_POSITION )
64
70
private int initialPosition ;
65
71
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
+
66
81
private Gist gist ;
67
82
68
83
@ Inject
@@ -71,39 +86,74 @@ public static Intent createIntent(Gist gist, int position) {
71
86
@ Inject
72
87
private AvatarLoader avatars ;
73
88
89
+ @ Inject
90
+ private HttpImageGetter imageGetter ;
91
+
74
92
@ Override
75
93
protected void onCreate (Bundle savedInstanceState ) {
76
94
super .onCreate (savedInstanceState );
77
95
78
96
setContentView (layout .pager_with_title );
79
97
98
+ if (initialPosition < 0 )
99
+ initialPosition = 0 ;
100
+
101
+ getSupportActionBar ().setTitle (getString (string .gist_title ) + gistId );
102
+
80
103
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
+ }
81
123
124
+ private void configurePager () {
82
125
ActionBar actionBar = getSupportActionBar ();
83
126
actionBar .setDisplayHomeAsUpEnabled (true );
84
- actionBar .setTitle (getString (string .gist_title ) + gistId );
85
127
User author = gist .getUser ();
86
128
if (author != null ) {
87
129
actionBar .setSubtitle (author .getLogin ());
88
130
avatars .bind (actionBar , author );
89
131
} else
90
132
actionBar .setSubtitle (string .anonymous );
91
133
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 );
96
142
97
- pager .setCurrentItem (initialPosition );
143
+ if (initialPosition < pagerAdapter .getCount ())
144
+ pager .setCurrentItem (initialPosition );
98
145
}
99
146
100
147
@ Override
101
148
public boolean onOptionsItemSelected (MenuItem item ) {
102
149
switch (item .getItemId ()) {
103
150
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
+ }
107
157
return true ;
108
158
default :
109
159
return super .onOptionsItemSelected (item );
0 commit comments