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

Alexander Vasin (prieran) vs CallbackHell #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ dependencies {
compile libraries.supportRecyclerView
compile libraries.supportCardView

compile libraries.gson
compile libraries.picasso

compile libraries.icepick
provided libraries.icepickProcessor

compile libraries.butterKnife
apt libraries.butterKnifeCompiler

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

import ru.yandex.yamblz.model.ArtistProvider;


public interface Provider {
ArtistProvider provideArtistProvider();
}
147 changes: 147 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,147 @@
package ru.yandex.yamblz.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Artist implements Comparable<Artist>, Parcelable, Serializable {

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];
}
};
@SerializedName("id")
private long mId;
@SerializedName("name")
private String mName;
@SerializedName("genres")
private String[] mGenres;
@SerializedName("tracks")
private int mCountOfTracks;
@SerializedName("albums")
private int mCountOfAlbums;
@SerializedName("link")
private String mSiteUrl;
@SerializedName("description")
private String mDescription;

@SerializedName("cover")
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection") // GSON
private Map<String, String> mCover;

public Artist(long id, String name, int countOfTracks, int countOfAlbums, String siteUrl, String description, String bigCover, String smallCover, String[] genres) {
mId = id;
mName = name;
mCountOfTracks = countOfTracks;
mCountOfAlbums = countOfAlbums;
mSiteUrl = siteUrl;
mDescription = description;
mCover = new HashMap<>();
mCover.put("big", bigCover);
mCover.put("small", smallCover);
mGenres = genres;
}

protected Artist(Parcel in) {
mId = in.readLong();
mName = in.readString();
mGenres = in.createStringArray();
mCountOfTracks = in.readInt();
mCountOfAlbums = in.readInt();
mSiteUrl = in.readString();
mDescription = in.readString();
}

public long getId() {
return mId;
}

public String getName() {
return mName;
}

public String getGenresAsString() {
return TextUtils.join(", ", mGenres);
}

public String[] getGenres() {
return mGenres;
}

public int getCountOfTracks() {
return mCountOfTracks;
}

public int getCountOfAlbums() {
return mCountOfAlbums;
}

public String getSiteUrl() {
return mSiteUrl;
}

public String getDescription() {
return mDescription;
}

public String getUrlOfBigCover() {
return mCover.get("big");
}

public String getUrlOfSmallCover() {
return mCover.get("small");
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mId);
dest.writeString(mName);
dest.writeStringArray(mGenres);
dest.writeInt(mCountOfTracks);
dest.writeInt(mCountOfAlbums);
dest.writeString(mSiteUrl);
dest.writeString(mDescription);
}

@Override
public int compareTo(@NonNull Artist another) {
return getName().compareTo(another.getName());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Artist)) return false;

Artist artist = (Artist) o;

return mId == artist.mId && (mName != null ? mName.equals(artist.mName) : artist.mName == null);

}

@Override
public int hashCode() {
int result = (int) (mId ^ (mId >>> 32));
result = 31 * result + (mName != null ? mName.hashCode() : 0);
return result;
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/ArtistProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.yandex.yamblz.model;

import java.util.List;

public interface ArtistProvider {
public List<Artist> getArtists();

public Artist getArtist(int position);

public int getArtistCount();

public int getPositionForArtist(Artist artist);
}
54 changes: 54 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/ArtistProviderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.yandex.yamblz.model;

import android.content.res.Resources;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;

import ru.yandex.yamblz.R;

public class ArtistProviderImpl implements ArtistProvider {
private Resources mResources;
private List<Artist> mArtists;

public ArtistProviderImpl(Resources resources) {
mResources = resources;
mArtists = getList();
}

@Override
public List<Artist> getArtists() {
return mArtists;
}

@Override
public Artist getArtist(int position) {
return mArtists.get(position);
}

@Override
public int getArtistCount() {
return mArtists.size();
}

@Override
public int getPositionForArtist(Artist artist) {
return mArtists.indexOf(artist);
}

private List<Artist> getList() {
Type type = new TypeToken<List<Artist>>() {
}.getType();

Gson gson = new Gson();
InputStream inStream = mResources.openRawResource(R.raw.artists);
InputStreamReader inStreamReader = new InputStreamReader(inStream);

return gson.fromJson(inStreamReader, type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ru.yandex.yamblz.model.content_provider;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

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

import ru.yandex.yamblz.model.Artist;
import ru.yandex.yamblz.model.ArtistContract;
import ru.yandex.yamblz.model.ArtistProvider;


public class ArtistProviderCP implements ArtistProvider {
private Context mContext;
private List<Artist> mArtists;

public ArtistProviderCP(Context context) {
mContext = context.getApplicationContext();
mArtists = getList();
if (mArtists == null) {
mArtists = new ArrayList<>();
}
}

@Override
public List<Artist> getArtists() {
return mArtists;
}

@Override
public Artist getArtist(int position) {
return mArtists.get(position);
}

@Override
public int getArtistCount() {
return mArtists.size();
}

@Override
public int getPositionForArtist(Artist artist) {
return mArtists.indexOf(artist);
}

private List<Artist> getList() {
ContentResolver contentResolver = mContext.getContentResolver();
Cursor cursor = contentResolver.query(ArtistContract.Artist.CONTENT_URI,
new String[]{
ArtistContract.Artist.ID,
ArtistContract.Artist.NAME,
ArtistContract.Artist.TRACKS,
ArtistContract.Artist.ALBUMS,
ArtistContract.Artist.GENRES,
ArtistContract.Artist.BIG_COVER,
ArtistContract.Artist.SMALL_COVER,
ArtistContract.Artist.URL,
ArtistContract.Artist.DESCRIPTION,
},
null,
null,
ArtistContract.Artist.NAME);
return new ArtistMaker().getArtistsFromCursor(cursor);
}
}
Loading