package ru.ltst.u2020mvp.data.api.mock; import android.app.Application; import android.content.res.AssetManager; import android.graphics.BitmapFactory; import javax.inject.Inject; import ru.ltst.u2020mvp.ApplicationScope; import ru.ltst.u2020mvp.data.api.model.response.Image; @ApplicationScope public final class MockImageLoader { private final AssetManager assetManager; @Inject MockImageLoader(Application application) { assetManager = application.getAssets(); } /** * A filename like {@code abc123.jpg} inside the {@code mock/images/} asset folder. */ private ImageBuilder newImage(String filename) { String path = "mock/images/" + filename; int width; int height; try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(assetManager.open(path), null, options); width = options.outWidth; height = options.outHeight; } catch (Exception e) { throw new RuntimeException("Unable to load " + filename, e); } String id = filename.substring(0, filename.lastIndexOf('.')); String link = "mock:///" + path; return new ImageBuilder(id, link, id /* default title == id */, id /* default description == id */, width, height); } public Image newImage(MockImage mockImage) { return newImage(mockImage.image) .title(mockImage.title) .views(mockImage.views) .build(); } public static class ImageBuilder { private final String id; private final String link; private final int width; private final int height; private String title; private String description; private long datetime = System.currentTimeMillis(); private int views; private boolean isAlbum; private ImageBuilder(String id, String link, String title, String description, int width, int height) { this.id = id; this.link = link; this.title = title; this.description = description; this.width = width; this.height = height; } public ImageBuilder title(String title) { this.title = title; return this; } public ImageBuilder description(String description) { this.description = description; return this; } public ImageBuilder datetime(int datetime) { this.datetime = datetime; return this; } public ImageBuilder views(int views) { this.views = views; return this; } public ImageBuilder isAlbum(boolean isAlbum) { this.isAlbum = isAlbum; return this; } public Image build() { return new Image(id, link, title, description, width, height, datetime, views, isAlbum); } } }