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 2 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
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;
}
}
58 changes: 58 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/ArtistFetcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.yandex.yamblz.model;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import ru.yandex.yamblz.model.Artist;

public class ArtistFetcher {
private static final String JSON_URL = "http://download.cdn.yandex.net/mobilization-2016/artists.json";

public String getJson() throws IOException {
return getUrlString(JSON_URL);
}

public List<Artist> getArtistsFromJson(String json) {
Gson gson = new Gson();
Type type = new TypeToken<List<Artist>>() {
}.getType();

return gson.fromJson(json, type);
}

private String getUrlString(String urlSpec) throws IOException {
return new String(getUrlBytes(urlSpec));
}

private byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException(connection.getResponseMessage() + ": with " + urlSpec);
}

int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
}
100 changes: 100 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/model/ArtistLab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ru.yandex.yamblz.model;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ArtistLab implements ArtistProvider {
private static final String TAG = "ArtistLab";
private static final String ARTIST_LIST_NAME = "artistList";

private static ArtistLab sArtistLab;

private Context mContext;
private List<Artist> mArtists;

private ArtistLab(Context context) {
mContext = context.getApplicationContext();
mArtists = new ArrayList<>();
}

public static ArtistLab get(Context context) {
if (sArtistLab == null) {
sArtistLab = new ArtistLab(context);
}
return sArtistLab;
}

public List<Artist> getArtists() {
List<Artist> tempArtists = loadArtists();

if (tempArtists != null) {
mArtists = tempArtists;
} else {
mArtists = new ArrayList<>();
}
return mArtists;
}

public void setArtists(List<Artist> artists) {
mArtists = artists;
saveArtists(mArtists);
}

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

@Override
public int getArtistCount() {
if (mArtists != null) {
return mArtists.size();
} else {
mArtists = loadArtists();
return mArtists.size();
}
}

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

// В БД нет смысла, слишком простой случай
public void saveArtists(List<Artist> artists) {
try {
FileOutputStream fos = mContext.openFileOutput(ARTIST_LIST_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(artists);
oos.close();
} catch (IOException ioe) {
Log.w(TAG, "Can't save artists: " + ioe.getMessage());
}
}

public List<Artist> loadArtists() {
try {
FileInputStream fis;
fis = mContext.openFileInput(ARTIST_LIST_NAME);
ObjectInputStream ois = new ObjectInputStream(fis);
List<Artist> artists = (ArrayList<Artist>) ois.readObject();
ois.close();
return artists;
} catch (Exception e) {
Log.w(TAG, "Can't load artists: " + e.getMessage());
File file = new File(mContext.getFilesDir().getAbsolutePath() + "/" + ARTIST_LIST_NAME);
if (!file.delete()) {
Log.e(TAG, "Can't delete file!");
}
return null;
}
}
}
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);
}
}
Loading