Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Hw #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ android {

// Libraries declared in dependencies.gradle in the root of the project
dependencies {

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.8'

compile libraries.dagger
apt libraries.daggerCompiler

Expand Down
64 changes: 64 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/api/ApiManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.yandex.yamblz.api;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.WorkerThread;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.yandex.yamblz.model.Artist;
import rx.Observable;

/**
* Encapsulates api work.
* Created by Litun on 21.04.2016.
*/
public class ApiManager {
private static final String URL = "http://cache-default06d.cdn.yandex.net/download.cdn.yandex.net/mobilization-2016/";
private ApiService service;

public ApiManager() {

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

service = retrofit.create(ApiService.class);
}

public Observable<List<Artist>> requestArtists() {
return Observable.just(true)
.map(this::loadArtists);
}

@WorkerThread
private List<Artist> loadArtists(boolean ignored) {
try {
Response<List<Artist>> response = service.listArtist().execute();
if (response.isSuccessful())
return response.body();
} catch (IOException e) {
}
return null;
}

@WorkerThread
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот за это плюс)

public Bitmap downloadImageSync(String url) {
Call<ResponseBody> call = service.downloadImage(url);
try {
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
return BitmapFactory.decodeStream(response.body().byteStream());
}
} catch (IOException ignored) {
}
return null;
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/api/ApiService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.yandex.yamblz.api;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;
import ru.yandex.yamblz.model.Artist;

/**
* Created by Litun on 07.08.2016.
*/

public interface ApiService {
@GET("artists.json")
Call<List<Artist>> listArtist();

@GET
Call<ResponseBody> downloadImage(@Url String fileUrl);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.yandex.yamblz.loader;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import java.util.List;

/**
* Created by Litun on 07.08.2016.
*/
public class SimpleCollageStrategy implements CollageStrategy {
@Override
public Bitmap create(List<Bitmap> bitmaps) {
if (bitmaps == null || bitmaps.size() == 0)
return null;

int width = 400, height = 400;
Bitmap collageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

Canvas collageCanvas = new Canvas(collageBitmap);

switch (bitmaps.size()) {
case 1:
return bitmaps.get(0);
case 2:
collageCanvas.drawBitmap(bitmaps.get(0), 0f, 0f, null);
collageCanvas.drawBitmap(bitmaps.get(1), width / 2, 0f, null);
break;
case 3:
collageCanvas.drawBitmap(bitmaps.get(0), 0f, 0f, null);
collageCanvas.drawBitmap(bitmaps.get(1), width / 2, 0f, null);
collageCanvas.drawBitmap(bitmaps.get(2), 0, height / 2, null);
break;
default:
case 4:
collageCanvas.drawBitmap(bitmaps.get(0), 0f, 0f, null);
collageCanvas.drawBitmap(bitmaps.get(1), 0f, height / 2, null);
collageCanvas.drawBitmap(bitmaps.get(2), width / 2, 0f, null);
collageCanvas.drawBitmap(bitmaps.get(3), width / 2, height / 2, null);
break;
}

return collageBitmap;

}
}
55 changes: 53 additions & 2 deletions app/src/main/java/ru/yandex/yamblz/loader/StubCollageLoader.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,82 @@
package ru.yandex.yamblz.loader;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.WorkerThread;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.Executors;

import ru.yandex.yamblz.api.ApiManager;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class StubCollageLoader implements CollageLoader {

private LruCache<List<String>, Bitmap> collageCache = new LruCache<>(20);
ApiManager apiManager = new ApiManager();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Модификаторы по настроению?

private CollageStrategy defaultCollageStrategy;

public StubCollageLoader(@NonNull CollageStrategy defaultCollageStrategy) {
this.defaultCollageStrategy = defaultCollageStrategy;
}

public StubCollageLoader() {
this(new SimpleCollageStrategy());
}

@Override
public void loadCollage(List<String> urls, ImageView imageView) {

loadCollage(urls, imageView, defaultCollageStrategy);
}

@Override
public void loadCollage(List<String> urls, ImageTarget imageTarget) {

loadCollage(urls, imageTarget, defaultCollageStrategy);
}

@Override
public void loadCollage(List<String> urls, ImageView imageView,
CollageStrategy collageStrategy) {
final WeakReference<ImageView> imageViewWeakReference = new WeakReference<>(imageView);
loadCollage(urls, bitmap -> {
ImageView weekImageView = imageViewWeakReference.get();
if (weekImageView != null)
weekImageView.setImageBitmap(bitmap);
}, collageStrategy);

}

@Override
public void loadCollage(List<String> urls, ImageTarget imageTarget,
CollageStrategy collageStrategy) {
Bitmap cachedBitmap = collageCache.get(urls);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не вижу отписку старого запроса для текущей таргета, если он есть

if (cachedBitmap == null)
Observable.from(urls)
.take(4)
.subscribeOn(Schedulers.from(Executors.newFixedThreadPool(4)))
.map(this::downloadImage)
.observeOn(Schedulers.io())
.toList()
.map(collageStrategy::create)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(imageTarget::onLoadBitmap);
else
imageTarget.onLoadBitmap(cachedBitmap);
}

@WorkerThread
private Bitmap downloadImage(String url) {
return apiManager.downloadImageSync(url);
}

private static class CollageData {
ImageTarget imageTarget;
CollageStrategy collageStrategy;
Bitmap collage;
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/Artist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ru.yandex.yamblz.model;

import java.util.ArrayList;
import java.util.Collection;

/**
* Created by Litun on 21.04.2016.
*/
public class Artist {

int id;
String name;
Collection<String> genres = new ArrayList<>();
int tracks;
int albums;
String link;
String description;
Cover cover;

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getSmallCover() {
return cover == null ? null : cover.small;
}

public Cover getCover() {
return cover;
}

public String getBigCover() {
return cover == null ? null : cover.big;
}

public Collection<String> getGenres() {
return genres;
}

public void setGenres(Collection<String> genres) {
this.genres = genres;
}

public int getAlbums() {
return albums;
}

public int getTracks() {
return tracks;
}

public String getDescription() {
return description;
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/Cover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.yandex.yamblz.model;


/**
* Created by Litun on 21.04.2016.
*/
public class Cover {
int id;
String small;
String big;
}
30 changes: 30 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/Genre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.yandex.yamblz.model;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Litun on 20.07.2016.
*/
public class Genre {

public Genre(String name, Artist artist){
this.name = name;
this.artist = artist;
}

String name;
private final Artist artist;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Artist getArtist() {
return artist;
}
}
Loading