-
Notifications
You must be signed in to change notification settings - Fork 20
@artemohanjanyan Артем Оганджанян #2
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
Open
artemohanjanyan
wants to merge
10
commits into
yamblz-native:master
Choose a base branch
from
artemohanjanyan: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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
91430b5
second task done with simple demo
artemohanjanyan a4509a4
bugfix
artemohanjanyan af1d25a
so java
artemohanjanyan 30cf4ae
buggy
artemohanjanyan 7dafeea
bug fixes, default strategy changed
artemohanjanyan 89b918c
Merge branch 'rx'
artemohanjanyan cb1859d
WeakReference
artemohanjanyan f471423
critical section during scroll demo
artemohanjanyan c7b8b80
solidlist
artemohanjanyan bcda1e7
Observable.fromCallable instead of custom one + error handling
artemohanjanyan 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| package ru.yandex.yamblz.data; | ||
|
|
||
| import android.database.Cursor; | ||
| import android.os.Parcel; | ||
| import android.os.Parcelable; | ||
| import android.text.TextUtils; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Stores data about one artist. | ||
| */ | ||
| @SuppressWarnings("WeakerAccess") | ||
| public class Artist implements Parcelable { | ||
| public static final Creator<Artist> CREATOR = new Creator<Artist>() { | ||
| @Override | ||
| public Artist createFromParcel(Parcel in) { | ||
| return new Artist(in); | ||
| } | ||
|
|
||
| @Override | ||
| public Artist[] newArray(int size) { | ||
| return new Artist[size]; | ||
| } | ||
| }; | ||
| public int id; | ||
| public String name; | ||
| public List<String> genres; | ||
| public int tracks, albums; | ||
| public String link; | ||
| public String description; | ||
| public String smallCover, bigCover; | ||
|
|
||
| public Artist() { | ||
| } | ||
|
|
||
| /** | ||
| * Creates an instance from parcel. | ||
| * @param in parcel. | ||
| */ | ||
| protected Artist(Parcel in) { | ||
| id = in.readInt(); | ||
| name = in.readString(); | ||
| genres = in.createStringArrayList(); | ||
| tracks = in.readInt(); | ||
| albums = in.readInt(); | ||
| link = in.readString(); | ||
| description = in.readString(); | ||
| smallCover = in.readString(); | ||
| bigCover = in.readString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns link to small cover at current row of cursor, | ||
| */ | ||
| public static String getSmallCover(Cursor cursor) { | ||
| return cursor.getString(7); | ||
| } | ||
|
|
||
| /** | ||
| * Returns link to big cover at current row of cursor, | ||
| */ | ||
| public static String getBigCover(Cursor cursor) { | ||
| return cursor.getString(8); | ||
| } | ||
|
|
||
| /** | ||
| * Returns genres as a comma-separated string. | ||
| */ | ||
| public String getGenres() { | ||
| return TextUtils.join(", ", genres); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeToParcel(Parcel dest, int flags) { | ||
| dest.writeInt(id); | ||
| dest.writeString(name); | ||
| dest.writeStringList(genres); | ||
| dest.writeInt(tracks); | ||
| dest.writeInt(albums); | ||
| dest.writeString(link); | ||
| dest.writeString(description); | ||
| dest.writeString(smallCover); | ||
| dest.writeString(bigCover); | ||
| } | ||
|
|
||
| @Override | ||
| public int describeContents() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| Artist artist = (Artist) o; | ||
|
|
||
| return id == artist.id; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return id; | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/ru/yandex/yamblz/data/InfoObservable.java
This file contains hidden or 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,106 @@ | ||
| package ru.yandex.yamblz.data; | ||
|
|
||
| import android.util.JsonReader; | ||
| import android.util.Log; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import rx.Observable; | ||
|
|
||
| public class InfoObservable { | ||
|
|
||
| private static final String jsonURL = "http://cache-spb03.cdn.yandex.net/" + | ||
| "download.cdn.yandex.net/mobilization-2016/artists.json"; | ||
| private static final String TAG = InfoObservable.class.getSimpleName(); | ||
|
|
||
| public static Observable<List<Artist>> getObservable() { | ||
| return Observable.fromCallable(InfoObservable::download); | ||
| } | ||
|
|
||
| private static List<Artist> download() throws IOException { | ||
| try (JsonReader reader = new JsonReader( | ||
| new InputStreamReader( | ||
| new URL(jsonURL) | ||
| .openStream(), | ||
| "UTF-8"))) { | ||
| Log.d(TAG, "downloading"); | ||
| List<Artist> artists = new ArrayList<>(); | ||
| reader.beginArray(); | ||
|
|
||
| while (reader.hasNext()) { | ||
| Artist artist = new Artist(); | ||
| reader.beginObject(); | ||
|
|
||
| while (reader.hasNext()) { | ||
| String name = reader.nextName(); | ||
| switch (name) { | ||
| case "id": | ||
| artist.id = reader.nextInt(); | ||
| break; | ||
| case "name": | ||
| artist.name = reader.nextString(); | ||
| break; | ||
| case "genres": | ||
| List<String> genres = new ArrayList<>(); | ||
| reader.beginArray(); | ||
| while (reader.hasNext()) { | ||
| genres.add(reader.nextString()); | ||
| } | ||
| reader.endArray(); | ||
| artist.genres = genres; | ||
| break; | ||
| case "tracks": | ||
| artist.tracks = reader.nextInt(); | ||
| break; | ||
| case "albums": | ||
| artist.albums = reader.nextInt(); | ||
| break; | ||
| case "link": | ||
| artist.link = reader.nextString(); | ||
| break; | ||
| case "description": | ||
| artist.description = reader.nextString(); | ||
| break; | ||
| case "cover": | ||
| reader.beginObject(); | ||
| for (int i = 0; i < 2; ++i) { | ||
| String size = reader.nextName(); | ||
| String cover = reader.nextString(); | ||
| switch (size) { | ||
| case "small": | ||
| artist.smallCover = cover; | ||
| break; | ||
| case "big": | ||
| artist.bigCover = cover; | ||
| break; | ||
| default: | ||
| Log.d(TAG, "Unknown cover size " + size); | ||
| break; | ||
| } | ||
| } | ||
| reader.endObject(); | ||
| break; | ||
| default: | ||
| Log.w(TAG, "Unknown name '" + name + "'"); | ||
| reader.skipValue(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| reader.endObject(); | ||
| artists.add(artist); | ||
| } | ||
|
|
||
| reader.endArray(); | ||
|
|
||
| Log.d(TAG, "downloaded"); | ||
|
|
||
| return artists; | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
121 changes: 121 additions & 0 deletions
121
app/src/main/java/ru/yandex/yamblz/handler/CriticalSectionsHandlerImpl.java
This file contains hidden or 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,121 @@ | ||
| package ru.yandex.yamblz.handler; | ||
|
|
||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Non thread-safe implementation of {@link CriticalSectionsHandler}. | ||
| */ | ||
| @SuppressWarnings("WeakerAccess") | ||
| public class CriticalSectionsHandlerImpl implements CriticalSectionsHandler { | ||
|
|
||
| private final Handler handler; | ||
| private final Set<Integer> sections; | ||
|
|
||
| private final Runnable executeTask; | ||
| private final Set<Task> taskQueue; | ||
|
|
||
| private final Map<Task, Runnable> delayedTasks; | ||
|
|
||
|
|
||
| public CriticalSectionsHandlerImpl() { | ||
| handler = new Handler(Looper.getMainLooper()); | ||
| sections = new HashSet<>(); | ||
|
|
||
| executeTask = buildExecuteTask(); | ||
| taskQueue = new LinkedHashSet<>(); | ||
|
|
||
| delayedTasks = new HashMap<>(); | ||
| } | ||
|
|
||
| private Runnable buildExecuteTask() { | ||
| return () -> { | ||
| Task currentTask = null; | ||
| currentTask = taskQueue.iterator().next(); | ||
| taskQueue.remove(currentTask); | ||
|
|
||
| // This runnable is in execution queue iff taskQueue is not empty. | ||
| assert currentTask != null; | ||
| currentTask.run(); | ||
|
|
||
| if (!taskQueue.isEmpty() && sections.isEmpty()) { | ||
| handler.post(executeTask); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void startSection(int id) { | ||
| sections.add(id); | ||
| if (!taskQueue.isEmpty()) { | ||
| handler.removeCallbacks(executeTask); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stopSection(int id) { | ||
| sections.remove(id); | ||
| if (sections.isEmpty()) { | ||
| if (!taskQueue.isEmpty()) { | ||
| handler.post(executeTask); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stopSections() { | ||
| sections.clear(); | ||
| if (!taskQueue.isEmpty()) { | ||
| handler.post(executeTask); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void postLowPriorityTask(Task task) { | ||
| if (taskQueue.add(task) && sections.isEmpty() && taskQueue.size() == 1) { | ||
| handler.post(executeTask); | ||
| } | ||
| delayedTasks.remove(task); | ||
| } | ||
|
|
||
| @Override | ||
| public void postLowPriorityTaskDelayed(Task task, int delayMillis) { | ||
| if (taskQueue.contains(task)) { | ||
| return; | ||
| } | ||
|
|
||
| Runnable runnable = () -> { | ||
| if (delayedTasks.containsKey(task)) { | ||
| postLowPriorityTask(task); | ||
| } | ||
| }; | ||
| delayedTasks.put(task, runnable); | ||
| handler.postDelayed(runnable, delayMillis); | ||
| } | ||
|
|
||
| @Override | ||
| public void removeLowPriorityTask(Task task) { | ||
| if (taskQueue.contains(task)) { | ||
| taskQueue.remove(task); | ||
| if (taskQueue.isEmpty()) { | ||
| handler.removeCallbacks(executeTask); | ||
| } | ||
| } | ||
| if (delayedTasks.containsKey(task)) { | ||
| delayedTasks.remove(task); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void removeLowPriorityTasks() { | ||
| taskQueue.clear(); | ||
| delayedTasks.clear(); | ||
| handler.removeCallbacks(executeTask); | ||
| } | ||
| } |
This file contains hidden or 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
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.
А с помощью GSON почему не сериализовал или как то более проще?
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.
Этот код у меня уже был :)