Private
Public Access
1
0

Hook up and tweak UI to DB. Now stuck because simplexml uses too much memory.

This commit is contained in:
Kevin Whitaker
2015-11-19 23:43:26 -05:00
parent edeaa695c5
commit a938ef0512
11 changed files with 128 additions and 15 deletions

View File

@@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.magnatune.eyecreate.companionformagnatune" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@@ -8,6 +8,7 @@ import android.support.v4.content.ContextCompat;
import com.heinrichreimersoftware.materialdrawer.DrawerActivity;
import com.heinrichreimersoftware.materialdrawer.structure.DrawerItem;
import com.heinrichreimersoftware.materialdrawer.structure.DrawerProfile;
import com.magnatune.eyecreate.companionformagnatune.fragments.AlbumsFragment;
import com.magnatune.eyecreate.companionformagnatune.login.MagnatuneAccountManager;
public class BrowseActivity extends DrawerActivity {
@@ -21,5 +22,11 @@ public class BrowseActivity extends DrawerActivity {
.setDescription(MagnatuneAccountManager.isLoggedIn()?getString(R.string.click_to_logout):getString(R.string.click_to_login))
.setRoundedAvatar((BitmapDrawable) ContextCompat.getDrawable(this, R.drawable.ic_account_white_48dp))
.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.primary))));
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_holder,new AlbumsFragment(),"albums")
.commit();
}
}
}

View File

@@ -1,10 +1,13 @@
package com.magnatune.eyecreate.companionformagnatune.adapters;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import com.magnatune.eyecreate.companionformagnatune.R;
import com.magnatune.eyecreate.companionformagnatune.model.Album;
import com.magnatune.eyecreate.companionformagnatune.viewholders.AlbumViewHolder;
import com.squareup.picasso.Picasso;
import io.realm.RealmBasedRecyclerViewAdapter;
import io.realm.RealmResults;
@@ -17,11 +20,15 @@ public class AlbumsAdapter extends RealmBasedRecyclerViewAdapter<Album,AlbumView
@Override
public AlbumViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int i) {
return null;
View v = inflater.inflate(R.layout.listitem_album_card,viewGroup,false);
AlbumViewHolder albumView = new AlbumViewHolder(v);
return albumView;
}
@Override
public void onBindRealmViewHolder(AlbumViewHolder albumViewHolder, int i) {
albumViewHolder.artistName.setText(realmResults.get(i).getArtist());
albumViewHolder.albumName.setText(realmResults.get(i).getAlbumname());
Picasso.with(albumViewHolder.itemView.getContext()).load(realmResults.get(i).getCover_small()).into(albumViewHolder.albumArt);
}
}

View File

@@ -1,7 +1,86 @@
package com.magnatune.eyecreate.companionformagnatune.fragments;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.magnatune.eyecreate.companionformagnatune.R;
import com.magnatune.eyecreate.companionformagnatune.adapters.AlbumsAdapter;
import com.magnatune.eyecreate.companionformagnatune.model.Album;
import com.magnatune.eyecreate.companionformagnatune.model.MagnatuneDBManager;
import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
import io.realm.Realm;
public class AlbumsFragment extends Fragment {
Realm db;
RealmRecyclerView recyclerView;
BroadcastReceiver refreshDone = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
recyclerView.setRefreshing(false);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = MagnatuneDBManager.getAlbumDB();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
recyclerView = (RealmRecyclerView) inflater.inflate(R.layout.fragment_albums,container,false);
recyclerView.setOnRefreshListener(new RealmRecyclerView.OnRefreshListener() {
@Override
public void onRefresh() {
triggerDBUpdate();
}
});
recyclerView.setAdapter(new AlbumsAdapter(getActivity(),db.where(Album.class).findAll(),true,true,null));
if(db.where(Album.class).findAll().size()<1) {
recyclerView.setRefreshing(true);
triggerDBUpdate();
}
return recyclerView;
}
private void triggerDBUpdate() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
MagnatuneDBManager.updateDB();
return null;
}
}.execute();
}
@Override
public void onDestroy() {
super.onDestroy();
db.close();
}
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(refreshDone,new IntentFilter(MagnatuneDBManager.ACTION_DB_UPDATE_DONE));
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(refreshDone);
}
}

View File

@@ -1,5 +1,7 @@
package com.magnatune.eyecreate.companionformagnatune.model;
import com.magnatune.eyecreate.companionformagnatune.model.xml.*;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@@ -8,7 +10,7 @@ import java.util.List;
@Root(name="AllAlbums")
public class AlbumResponse {
@ElementList(inline=true)
@ElementList(inline=true,type= com.magnatune.eyecreate.companionformagnatune.model.xml.Album.class)
public List<com.magnatune.eyecreate.companionformagnatune.model.xml.Album> albums;
}

View File

@@ -7,7 +7,7 @@ import org.simpleframework.xml.Root;
import java.util.Date;
import java.util.List;
@Root
@Root(name="Album",strict = false)
public class Album {
@Element
@@ -28,7 +28,7 @@ public class Album {
@Element
public int year;
@Element
@Element(required = false)
public String album_notes;
@Element
@@ -46,6 +46,6 @@ public class Album {
@Element
public String albumsku;
@ElementList(inline=true)
@ElementList(inline=true,type=Track.class)
public List<Track> tracks;
}

View File

@@ -3,7 +3,7 @@ package com.magnatune.eyecreate.companionformagnatune.model.xml;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
@Root(name="Track",strict = false)
public class Track {
@Element
public String artist;

View File

@@ -1,11 +1,23 @@
package com.magnatune.eyecreate.companionformagnatune.viewholders;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.magnatune.eyecreate.companionformagnatune.R;
import io.realm.RealmViewHolder;
public class AlbumViewHolder extends RealmViewHolder {
public ImageView albumArt;
public TextView albumName;
public TextView artistName;
public AlbumViewHolder(View itemView) {
super(itemView);
albumArt = (ImageView) itemView.findViewById(R.id.album_art);
albumName = (TextView) itemView.findViewById(R.id.album_name);
artistName = (TextView) itemView.findViewById(R.id.artist_name);
}
}

View File

@@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".BrowseActivity">
<View android:layout_width="wrap_content"
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_holder"/>
</RelativeLayout>

View File

@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
<co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_layout">
<co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/album_grid"
app:rrvLayoutType="Grid" app:rrvIsRefreshable="true" app:rrvGridLayoutSpanCount="2"/>
</SwipeRefreshLayout>
android:id="@+id/album_grid"
app:rrvLayoutType="Grid"
app:rrvIsRefreshable="true"
app:rrvGridLayoutSpanCount="2"
xmlns:android="http://schemas.android.com/apk/res/android" />

View File

@@ -13,5 +13,9 @@
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"/>
</LinearLayout>
</CardView>