1
1
package org .wikimedia .commons .media ;
2
2
3
3
import android .content .Intent ;
4
+ import android .database .DataSetObserver ;
4
5
import android .graphics .*;
5
6
import android .os .*;
6
7
import android .text .*;
@@ -62,6 +63,7 @@ public static MediaDetailFragment forMedia(int index, boolean editable) {
62
63
private boolean categoriesPresent = false ;
63
64
private ViewTreeObserver .OnGlobalLayoutListener layoutListener ; // for layout stuff, only used once!
64
65
private ViewTreeObserver .OnScrollChangedListener scrollListener ;
66
+ DataSetObserver dataObserver ;
65
67
private AsyncTask <Void ,Void ,Boolean > detailFetchTask ;
66
68
private LicenseList licenseList ;
67
69
@@ -93,7 +95,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
93
95
index = getArguments ().getInt ("index" );
94
96
initialListTop = 0 ;
95
97
}
96
- final Media media = detailProvider .getMediaAtPosition (index );
97
98
categoryNames = new ArrayList <String >();
98
99
categoryNames .add (getString (R .string .detail_panel_cats_loading ));
99
100
@@ -113,18 +114,60 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
113
114
114
115
licenseList = new LicenseList (getActivity ());
115
116
116
- // Enable or disable editing on the title
117
- /*
118
- title.setClickable(editable);
119
- title.setFocusable(editable);
120
- title.setCursorVisible(editable);
121
- title.setFocusableInTouchMode(editable);
122
- if(!editable) {
123
- title.setBackgroundDrawable(null);
117
+ Media media = detailProvider .getMediaAtPosition (index );
118
+ if (media == null ) {
119
+ // Ask the detail provider to ping us when we're ready
120
+ Log .d ("Commons" , "MediaDetailFragment not yet ready to display details; registering observer" );
121
+ dataObserver = new DataSetObserver () {
122
+ public void onChanged () {
123
+ Log .d ("Commons" , "MediaDetailFragment ready to display delayed details!" );
124
+ detailProvider .unregisterDataSetObserver (dataObserver );
125
+ dataObserver = null ;
126
+ displayMediaDetails (detailProvider .getMediaAtPosition (index ));
127
+ }
128
+ };
129
+ detailProvider .registerDataSetObserver (dataObserver );
130
+ } else {
131
+ Log .d ("Commons" , "MediaDetailFragment ready to display details" );
132
+ displayMediaDetails (media );
124
133
}
125
- */
126
134
135
+ // Progressively darken the image in the background when we scroll detail pane up
136
+ scrollListener = new ViewTreeObserver .OnScrollChangedListener () {
137
+ public void onScrollChanged () {
138
+ updateTheDarkness ();
139
+ }
140
+ };
141
+ view .getViewTreeObserver ().addOnScrollChangedListener (scrollListener );
142
+
143
+ // Layout layoutListener to size the spacer item relative to the available space.
144
+ // There may be a .... better way to do this.
145
+ layoutListener = new ViewTreeObserver .OnGlobalLayoutListener () {
146
+ private int currentHeight = -1 ;
147
+
148
+ public void onGlobalLayout () {
149
+ int viewHeight = view .getHeight ();
150
+ //int textHeight = title.getLineHeight();
151
+ int paddingDp = 112 ;
152
+ float paddingPx = TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , paddingDp , getResources ().getDisplayMetrics ());
153
+ int newHeight = viewHeight - Math .round (paddingPx );
154
+
155
+ if (newHeight != currentHeight ) {
156
+ currentHeight = newHeight ;
157
+ ViewGroup .LayoutParams params = spacer .getLayoutParams ();
158
+ params .height = newHeight ;
159
+ spacer .setLayoutParams (params );
160
+
161
+ scrollView .scrollTo (0 , initialListTop );
162
+ }
163
+
164
+ }
165
+ };
166
+ view .getViewTreeObserver ().addOnGlobalLayoutListener (layoutListener );
167
+ return view ;
168
+ }
127
169
170
+ private void displayMediaDetails (final Media media ) {
128
171
String actualUrl = (media .getLocalUri () != null && TextUtils .isEmpty (media .getLocalUri ().toString ())) ? media .getLocalUri ().toString () : media .getThumbnailUrl (640 );
129
172
if (actualUrl .startsWith ("http" )) {
130
173
ImageLoader loader = ((CommonsApplication )getActivity ().getApplicationContext ()).getImageLoader ();
@@ -212,58 +255,6 @@ public void onLoadingCancelled(String s, View view) {
212
255
title .setText (media .getDisplayTitle ());
213
256
desc .setText ("" ); // fill in from network...
214
257
license .setText ("" ); // fill in from network...
215
-
216
- /*
217
- title.addTextChangedListener(new TextWatcher() {
218
- public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
219
-
220
- }
221
-
222
- public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
223
- detailProvider.getMediaAtPosition(index).setFilename(title.getText().toString());
224
- detailProvider.getMediaAtPosition(index).setTag("isDirty", true);
225
- detailProvider.notifyDatasetChanged();
226
- }
227
-
228
- public void afterTextChanged(Editable editable) {
229
-
230
- }
231
- });
232
- */
233
-
234
- // Progressively darken the image in the background when we scroll detail pane up
235
- scrollListener = new ViewTreeObserver .OnScrollChangedListener () {
236
- public void onScrollChanged () {
237
- updateTheDarkness ();
238
- }
239
- };
240
- view .getViewTreeObserver ().addOnScrollChangedListener (scrollListener );
241
-
242
- // Layout layoutListener to size the spacer item relative to the available space.
243
- // There may be a .... better way to do this.
244
- layoutListener = new ViewTreeObserver .OnGlobalLayoutListener () {
245
- private int currentHeight = -1 ;
246
-
247
- public void onGlobalLayout () {
248
- int viewHeight = view .getHeight ();
249
- //int textHeight = title.getLineHeight();
250
- int paddingDp = 112 ;
251
- float paddingPx = TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , paddingDp , getResources ().getDisplayMetrics ());
252
- int newHeight = viewHeight - Math .round (paddingPx );
253
-
254
- if (newHeight != currentHeight ) {
255
- currentHeight = newHeight ;
256
- ViewGroup .LayoutParams params = spacer .getLayoutParams ();
257
- params .height = newHeight ;
258
- spacer .setLayoutParams (params );
259
-
260
- scrollView .scrollTo (0 , initialListTop );
261
- }
262
-
263
- }
264
- };
265
- view .getViewTreeObserver ().addOnGlobalLayoutListener (layoutListener );
266
- return view ;
267
258
}
268
259
269
260
@ Override
0 commit comments