-
Notifications
You must be signed in to change notification settings - Fork 20
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
Litun
wants to merge
1
commit into
yamblz-native:master
Choose a base branch
from
Litun:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw #20
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/ru/yandex/yamblz/loader/SimpleCollageStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
app/src/main/java/ru/yandex/yamblz/loader/StubCollageLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вот за это плюс)