Private
Public Access
1
0

Clean up card view. Migrate new xml parser to use Object Streams.

This commit is contained in:
Kevin Whitaker
2015-11-20 14:21:18 -05:00
parent 3dfb0b592b
commit 526b6b6f5c
6 changed files with 56 additions and 63 deletions

View File

@@ -1,6 +1,5 @@
package com.magnatune.eyecreate.companionformagnatune.api;
import com.magnatune.eyecreate.companionformagnatune.model.AlbumResponse;
import com.magnatune.eyecreate.companionformagnatune.model.xml.Album;
import com.magnatune.eyecreate.companionformagnatune.model.xml.Track;
import com.squareup.okhttp.ResponseBody;
@@ -10,37 +9,20 @@ import com.thoughtworks.xstream.io.xml.KXml2Driver;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import retrofit.Converter;
public class MagnatuneAlbumsConverter implements Converter<ResponseBody,AlbumResponse> {
public class MagnatuneAlbumsConverter implements Converter<ResponseBody,ObjectInputStream> {
@Override
public AlbumResponse convert(ResponseBody value) throws IOException {
public ObjectInputStream convert(ResponseBody value) throws IOException {
GZIPInputStream compressedStream = new GZIPInputStream(value.byteStream());
XStream parser = new XStream(new KXml2Driver());
parser.processAnnotations(Album.class);
parser.processAnnotations(Track.class);
parser.processAnnotations(AlbumResponse.class);
parser.registerConverter(new DateConverter("yyyy-MM-dd",new String[]{}));
parser.ignoreUnknownElements();
ObjectInputStream objectStream = parser.createObjectInputStream(compressedStream);
boolean processingDone = false;
List<Album> albums = new ArrayList<>();
while(!processingDone) {
try {
albums.add((Album) objectStream.readObject());
} catch(OptionalDataException e) {
processingDone = true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
AlbumResponse response = new AlbumResponse();
response.albums = albums;
return response;
return objectStream;
}
}

View File

@@ -1,8 +1,8 @@
package com.magnatune.eyecreate.companionformagnatune.api;
import com.magnatune.eyecreate.companionformagnatune.model.AlbumResponse;
import com.squareup.okhttp.ResponseBody;
import java.io.ObjectInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@@ -11,7 +11,7 @@ import retrofit.Converter;
public class MagnatuneAlbumsConverterFactory extends Converter.Factory {
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
if(AlbumResponse.class.equals(type)) {
if(ObjectInputStream.class.equals(type)) {
return new MagnatuneAlbumsConverter();
} else {
return null;

View File

@@ -1,13 +1,13 @@
package com.magnatune.eyecreate.companionformagnatune.api;
import com.magnatune.eyecreate.companionformagnatune.model.AlbumResponse;
import java.io.ObjectInputStream;
import retrofit.Call;
import retrofit.http.GET;
public interface data_api {
@GET("/info/album_info_xml.gz")
Call<AlbumResponse> getAlbumList();
Call<ObjectInputStream> getAlbumList();
@GET("/info/changed.txt")
Call<String> getUpdateChecksum();

View File

@@ -1,14 +0,0 @@
package com.magnatune.eyecreate.companionformagnatune.model;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.util.List;
@XStreamAlias("AllAlbums")
public class AlbumResponse {
@XStreamImplicit(itemFieldName = "Album")
public List<com.magnatune.eyecreate.companionformagnatune.model.xml.Album> albums;
}

View File

@@ -6,8 +6,11 @@ import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager;
import com.magnatune.eyecreate.companionformagnatune.MagnatuneCompainionApplication;
import com.thoughtworks.xstream.converters.ConversionException;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Arrays;
import java.util.List;
@@ -26,8 +29,8 @@ public class MagnatuneDBManager {
//Check if DB should update
SharedPreferences prefs = MagnatuneCompainionApplication.getApplication().getSharedPreferences(DB_CHECK_PREFS, Context.MODE_PRIVATE);
boolean shouldUpdate = false;
String hash = "";
if(prefs.contains(SP_KEY_DB_HASH)) {
String hash = "";
try {
hash = MagnatuneCompainionApplication.getDataApi().getUpdateChecksum().execute().body();
} catch (IOException e) {
@@ -35,12 +38,11 @@ public class MagnatuneDBManager {
}
if(!hash.equals(prefs.getString(SP_KEY_DB_HASH,""))) {
shouldUpdate = true;
prefs.edit().putString(SP_KEY_DB_HASH,hash).apply();
}
} else {
try {
shouldUpdate = true;
prefs.edit().putString(SP_KEY_DB_HASH,MagnatuneCompainionApplication.getDataApi().getUpdateChecksum().execute().body()).apply();
hash = MagnatuneCompainionApplication.getDataApi().getUpdateChecksum().execute().body();
} catch (IOException e) {
e.printStackTrace();
}
@@ -49,14 +51,25 @@ public class MagnatuneDBManager {
if(shouldUpdate) {
//Update DB
try {
Response<AlbumResponse> response = MagnatuneCompainionApplication.getDataApi().getAlbumList().execute();
Response<ObjectInputStream> response = MagnatuneCompainionApplication.getDataApi().getAlbumList().execute();
Realm db = getAlbumDB();
db.beginTransaction();
for(com.magnatune.eyecreate.companionformagnatune.model.xml.Album album:response.body().albums) {
db.copyToRealmOrUpdate(xmlAlbumToDBAlbum(album));
boolean processingDone = false;
while(!processingDone) {
db.beginTransaction();
try {
db.copyToRealmOrUpdate(xmlAlbumToDBAlbum((com.magnatune.eyecreate.companionformagnatune.model.xml.Album) response.body().readObject()));
} catch(EOFException e) {
processingDone = true;
} catch(ConversionException e) {
e.getMessage();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
db.commitTransaction();
}
db.commitTransaction();
response.body().close();
db.close();
prefs.edit().putString(SP_KEY_DB_HASH,hash).apply();
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -1,21 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<CardView xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/album_art"/>
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp" app:cardElevation="4dp" app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/album_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/artist_name"/>
android:orientation="vertical"
android:layout_margin="5dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:id="@+id/album_art"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="@+id/album_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/artist_name"/>
</LinearLayout>
</LinearLayout>
</CardView>
</android.support.v7.widget.CardView>