Merge pull request #7333 from nextcloud/shimmer

Add shimmer to preview image fragment
This commit is contained in:
Andy Scherzinger 2020-12-01 12:43:47 +01:00 committed by GitHub
commit 183efcbe89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 29 deletions

View file

@ -42,7 +42,9 @@ import android.os.AsyncTask;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.Display; import android.view.Display;
import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.client.network.ConnectivityService;
@ -253,21 +255,27 @@ public final class ThumbnailsCacheManager {
private FileDataStorageManager storageManager; private FileDataStorageManager storageManager;
private Account account; private Account account;
private WeakReference<ImageView> imageViewReference; private WeakReference<ImageView> imageViewReference;
private WeakReference<FrameLayout> frameLayoutReference;
private OCFile file; private OCFile file;
private ConnectivityService connectivityService; private ConnectivityService connectivityService;
private int backgroundColor;
public ResizedImageGenerationTask(FileFragment fileFragment, public ResizedImageGenerationTask(FileFragment fileFragment,
ImageView imageView, ImageView imageView,
FrameLayout emptyListProgress,
FileDataStorageManager storageManager, FileDataStorageManager storageManager,
ConnectivityService connectivityService, ConnectivityService connectivityService,
Account account) Account account,
int backgroundColor)
throws IllegalArgumentException { throws IllegalArgumentException {
this.fileFragment = fileFragment; this.fileFragment = fileFragment;
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
frameLayoutReference = new WeakReference<>(emptyListProgress);
this.storageManager = storageManager; this.storageManager = storageManager;
this.connectivityService = connectivityService; this.connectivityService = connectivityService;
this.account = account; this.account = account;
this.backgroundColor = backgroundColor;
} }
@Override @Override
@ -375,6 +383,7 @@ public final class ThumbnailsCacheManager {
protected void onPostExecute(Bitmap bitmap) { protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null) { if (imageViewReference != null) {
final ImageView imageView = imageViewReference.get(); final ImageView imageView = imageViewReference.get();
final FrameLayout frameLayout = frameLayoutReference.get();
if (bitmap != null) { if (bitmap != null) {
final ResizedImageGenerationTask bitmapWorkerTask = getResizedImageGenerationWorkerTask(imageView); final ResizedImageGenerationTask bitmapWorkerTask = getResizedImageGenerationWorkerTask(imageView);
@ -383,7 +392,13 @@ public final class ThumbnailsCacheManager {
String tagId = String.valueOf(file.getFileId()); String tagId = String.valueOf(file.getFileId());
if (String.valueOf(imageView.getTag()).equals(tagId)) { if (String.valueOf(imageView.getTag()).equals(tagId)) {
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(bitmap); imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(backgroundColor);
if (frameLayout != null) {
frameLayout.setVisibility(View.GONE);
}
} }
} }
} else { } else {

View file

@ -270,4 +270,8 @@ public abstract class ToolbarActivity extends BaseActivity {
public ImageView getPreviewImageView() { public ImageView getPreviewImageView() {
return mPreviewImage; return mPreviewImage;
} }
public FrameLayout getPreviewImageContainer() {
return mPreviewImageContainer;
}
} }

View file

@ -128,7 +128,11 @@ public class DiskLruImageCache {
options.inMutable = false; options.inMutable = false;
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(buffIn, null, options); BitmapFactory.decodeStream(buffIn, null, options);
snapshot = mDiskCache.get(validKey);
inputStream = snapshot.getInputStream(0);
buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE);
// Calculate inSampleSize // Calculate inSampleSize
options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height); options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height);

View file

@ -581,9 +581,12 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
final ThumbnailsCacheManager.ResizedImageGenerationTask task = final ThumbnailsCacheManager.ResizedImageGenerationTask task =
new ThumbnailsCacheManager.ResizedImageGenerationTask(this, new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
toolbarActivity.getPreviewImageView(), toolbarActivity.getPreviewImageView(),
toolbarActivity.getPreviewImageContainer(),
containerActivity.getStorageManager(), containerActivity.getStorageManager(),
connectivityService, connectivityService,
containerActivity.getStorageManager().getAccount()); containerActivity.getStorageManager().getAccount(),
getResources().getColor(R.color.background_color_inverse)
);
if (resizedImage == null) { if (resizedImage == null) {
resizedImage = thumbnail; resizedImage = thumbnail;

View file

@ -89,6 +89,8 @@ import androidx.fragment.app.FragmentStatePagerAdapter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifDrawable;
import static com.owncloud.android.datamodel.ThumbnailsCacheManager.PREFIX_THUMBNAIL;
/** /**
* This fragment shows a preview of a downloaded image. * This fragment shows a preview of a downloaded image.
@ -237,34 +239,44 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
int width = screenSize.x; int width = screenSize.x;
int height = screenSize.y; int height = screenSize.y;
// show thumbnail while loading image
binding.image.setVisibility(View.GONE);
binding.emptyListProgress.setVisibility(View.VISIBLE);
Bitmap thumbnail = getThumbnailBitmap(getFile());
if (thumbnail != null) {
binding.shimmer.setVisibility(View.VISIBLE);
binding.shimmerThumbnail.setImageBitmap(thumbnail);
binding.image.setVisibility(View.GONE);
bitmap = thumbnail;
} else {
thumbnail = ThumbnailsCacheManager.mDefaultImg;
}
if (showResizedImage) { if (showResizedImage) {
Bitmap resizedImage = getResizedBitmap(getFile(), width, height); Bitmap resizedImage = getResizedBitmap(getFile(), width, height);
if (resizedImage != null && !getFile().isUpdateThumbnailNeeded()) { if (resizedImage != null && !getFile().isUpdateThumbnailNeeded()) {
binding.image.setImageBitmap(resizedImage); binding.image.setImageBitmap(resizedImage);
binding.image.setVisibility(View.VISIBLE); binding.image.setVisibility(View.VISIBLE);
binding.emptyListView.setVisibility(View.GONE);
binding.emptyListProgress.setVisibility(View.GONE);
binding.image.setBackgroundColor(getResources().getColor(R.color.background_color_inverse));
bitmap = resizedImage; bitmap = resizedImage;
} else { } else {
// show thumbnail while loading resized image
Bitmap thumbnail = getResizedBitmap(getFile(), width, height);
if (thumbnail != null) {
binding.image.setImageBitmap(thumbnail);
binding.image.setVisibility(View.VISIBLE);
bitmap = thumbnail;
} else {
thumbnail = ThumbnailsCacheManager.mDefaultImg;
}
// generate new resized image // generate new resized image
if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), binding.image) && if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), binding.image) &&
containerActivity.getStorageManager() != null) { containerActivity.getStorageManager() != null) {
final ThumbnailsCacheManager.ResizedImageGenerationTask task = final ThumbnailsCacheManager.ResizedImageGenerationTask task =
new ThumbnailsCacheManager.ResizedImageGenerationTask(this, new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
binding.image, binding.image,
binding.emptyListProgress,
containerActivity.getStorageManager(), containerActivity.getStorageManager(),
connectivityService, connectivityService,
containerActivity.getStorageManager().getAccount()); containerActivity.getStorageManager().getAccount(),
getResources().getColor(R.color.background_color_inverse)
);
if (resizedImage == null) { if (resizedImage == null) {
resizedImage = thumbnail; resizedImage = thumbnail;
} }
@ -278,11 +290,6 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
task.execute(getFile()); task.execute(getFile());
} }
} }
binding.emptyListView.setVisibility(View.GONE);
binding.emptyListProgress.setVisibility(View.GONE);
binding.image.setBackgroundColor(getResources().getColor(R.color.background_color_inverse));
binding.image.setVisibility(View.VISIBLE);
} else { } else {
loadBitmapTask = new LoadBitmapTask(binding.image, binding.emptyListView, binding.emptyListProgress); loadBitmapTask = new LoadBitmapTask(binding.image, binding.emptyListView, binding.emptyListProgress);
binding.image.setVisibility(View.GONE); binding.image.setVisibility(View.GONE);
@ -316,6 +323,11 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
return cachedImage; return cachedImage;
} }
private @Nullable
Bitmap getThumbnailBitmap(OCFile file) {
return ThumbnailsCacheManager.getBitmapFromDiskCache(PREFIX_THUMBNAIL + file.getRemoteId());
}
@Override @Override
public void onStop() { public void onStop() {
Log_OC.d(TAG, "onStop starts"); Log_OC.d(TAG, "onStop starts");

View file

@ -89,19 +89,20 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<com.elyeproj.loaderviewlibrary.LoaderImageView <com.elyeproj.loaderviewlibrary.LoaderImageView
android:layout_width="@dimen/empty_list_icon_layout_width" android:id="@+id/shimmer"
android:layout_height="@dimen/empty_list_icon_layout_width" android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center" android:layout_gravity="center"
android:contentDescription="@null" android:contentDescription="@null"
app:corners="24" /> app:corners="24" />
<ImageView <ImageView
android:id="@+id/shimmerThumbnail"
android:layout_width="@dimen/empty_list_icon_layout_width" android:layout_width="@dimen/empty_list_icon_layout_width"
android:layout_height="@dimen/empty_list_icon_layout_height" android:layout_height="@dimen/empty_list_icon_layout_height"
android:layout_gravity="center" android:layout_gravity="center"
android:contentDescription="@null" android:contentDescription="@null"
android:src="@drawable/ic_image_outline" android:src="@drawable/ic_image_outline" />
app:tint="@color/bg_default" />
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>

View file

@ -91,6 +91,8 @@
android:layout_height="@dimen/nav_drawer_header_height" android:layout_height="@dimen/nav_drawer_header_height"
android:visibility="gone"> android:visibility="gone">
<!-- TODO add shimmer -->
<ImageView <ImageView
android:id="@+id/preview_image" android:id="@+id/preview_image"
android:layout_width="match_parent" android:layout_width="match_parent"