Private
Public Access
1
0

Rework DB to have artist data separated into Artist table. Rework refresh notifications to be more in line with actions with multiple fragments. Filed bug against what I'm seeing on artist fragment.

https://github.com/thorbenprimke/realm-recyclerview/issues/4
This commit is contained in:
Kevin Whitaker
2015-11-21 19:41:51 -05:00
parent 74b4e31fc7
commit 00d898873a
10 changed files with 190 additions and 39 deletions

View File

@@ -31,7 +31,7 @@ public class AlbumsAdapter extends RealmBasedRecyclerViewAdapter<Album,AlbumView
@Override
public void onBindRealmViewHolder(final AlbumViewHolder albumViewHolder, int i) {
albumViewHolder.artistName.setText(realmResults.get(i).getArtist());
albumViewHolder.artistName.setText(realmResults.get(i).getArtist().getArtistname());
albumViewHolder.albumName.setText(realmResults.get(i).getAlbumname());
Picasso.with(albumViewHolder.itemView.getContext()).load(realmResults.get(i).getCover_small()).into(albumViewHolder.albumArt, new Callback() {
@Override

View File

@@ -0,0 +1,31 @@
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.Artist;
import com.magnatune.eyecreate.companionformagnatune.viewholders.ArtistViewHolder;
import io.realm.RealmBasedRecyclerViewAdapter;
import io.realm.RealmResults;
public class ArtistsAdapter extends RealmBasedRecyclerViewAdapter<Artist,ArtistViewHolder> {
public ArtistsAdapter(Context context, RealmResults<Artist> realmResults, boolean automaticUpdate, boolean animateResults, boolean addSectionHeaders, String headerColumnName) {
super(context, realmResults, automaticUpdate, animateResults, addSectionHeaders, headerColumnName);
}
@Override
public ArtistViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int i) {
View v = inflater.inflate(R.layout.listitem_artist,viewGroup,false);
ArtistViewHolder holder = new ArtistViewHolder(v);
return holder;
}
@Override
public void onBindRealmViewHolder(ArtistViewHolder artistViewHolder, int i) {
artistViewHolder.artistName.setText(realmResults.get(i).getArtistname());
}
}

View File

@@ -49,17 +49,9 @@ public class AlbumsFragment extends Fragment {
}
});
recyclerView.setAdapter(new AlbumsAdapter(getActivity(), db.where(Album.class).findAllSorted("albumname"), true, true, null));
recyclerView.post(new Runnable() {
@Override
public void run() {
if(db.where(Album.class).findAll().size()<1) {
recyclerView.setRefreshing(true);
if (db.where(Album.class).findAll().size() < 1) {
triggerDBUpdate();
} else if(!MagnatuneDBManager.isProcessingDone()) {
recyclerView.setRefreshing(true);
}
}
});
return recyclerView;
}
@@ -82,6 +74,12 @@ public class AlbumsFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.setRefreshing(!MagnatuneDBManager.isProcessingDone());
}
});
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(refreshDone,new IntentFilter(MagnatuneDBManager.ACTION_DB_UPDATE_DONE));
}

View File

@@ -1,19 +1,91 @@
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.ArtistsAdapter;
import com.magnatune.eyecreate.companionformagnatune.model.Artist;
import com.magnatune.eyecreate.companionformagnatune.model.MagnatuneDBManager;
import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
import io.realm.Realm;
public class ArtistFragment extends Fragment {
Realm db;
RealmRecyclerView recyclerView;
BroadcastReceiver refreshDone = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
recyclerView.setRefreshing(false);
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_artists,container,false);
return v;
recyclerView = (RealmRecyclerView) inflater.inflate(R.layout.fragment_artists,container,false);
recyclerView.setOnRefreshListener(new RealmRecyclerView.OnRefreshListener() {
@Override
public void onRefresh() {
triggerDBUpdate();
}
});
recyclerView.setAdapter(new ArtistsAdapter(getActivity(), db.where(Artist.class).findAllSorted("artistname"), true, true,true,"artistname"));
if (db.where(Artist.class).findAll().size() < 1) {
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 onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = MagnatuneDBManager.getAlbumDB();
}
@Override
public void onDestroy() {
super.onDestroy();
db.close();
}
@Override
public void onResume() {
super.onResume();
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.setRefreshing(!MagnatuneDBManager.isProcessingDone());
}
});
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

@@ -11,11 +11,9 @@ public class Album extends RealmObject {
@PrimaryKey
private String albumsku;
private String albumname;
private String artist;
private String artistdesc;
private Artist artist;
private String cover_small;
private String cover_large;
private String artistphoto;
private int year;
private String album_notes;
private String homepage;
@@ -40,22 +38,14 @@ public class Album extends RealmObject {
this.albumname = albumname;
}
public String getArtist() {
public Artist getArtist() {
return artist;
}
public void setArtist(String artist) {
public void setArtist(Artist artist) {
this.artist = artist;
}
public String getArtistdesc() {
return artistdesc;
}
public void setArtistdesc(String artistdesc) {
this.artistdesc = artistdesc;
}
public String getCover_small() {
return cover_small;
}
@@ -72,14 +62,6 @@ public class Album extends RealmObject {
this.cover_large = cover_large;
}
public String getArtistphoto() {
return artistphoto;
}
public void setArtistphoto(String artistphoto) {
this.artistphoto = artistphoto;
}
public int getYear() {
return year;
}

View File

@@ -0,0 +1,35 @@
package com.magnatune.eyecreate.companionformagnatune.model;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class Artist extends RealmObject {
@PrimaryKey
private String artistname;
private String artistdesc;
private String artistphoto;
public String getArtistname() {
return artistname;
}
public void setArtistname(String artistname) {
this.artistname = artistname;
}
public String getArtistdesc() {
return artistdesc;
}
public void setArtistdesc(String artistdesc) {
this.artistdesc = artistdesc;
}
public String getArtistphoto() {
return artistphoto;
}
public void setArtistphoto(String artistphoto) {
this.artistphoto = artistphoto;
}
}

View File

@@ -100,15 +100,17 @@ public class MagnatuneDBManager {
}
public static Album xmlAlbumToDBAlbum(com.magnatune.eyecreate.companionformagnatune.model.xml.Album album) {
Artist artist = new Artist();
artist.setArtistname(album.artist);
artist.setArtistdesc(album.artistdesc);
artist.setArtistphoto(album.artistphoto);
Album entry = new Album();
entry.setAlbumsku(album.albumsku);
entry.setAlbumname(album.albumname);
entry.setArtist(album.artist);
entry.setArtistdesc(album.artistdesc);
entry.setArtist(artist);
entry.setAlbum_notes(album.album_notes);
entry.setCover_small(album.cover_small);
entry.setCover_large(album.cover_small.replace("200", "800"));
entry.setArtistphoto(album.artistphoto);
entry.setYear(album.year);
entry.setHomepage(album.home);
entry.setMp3genre(album.mp3genre);

View File

@@ -0,0 +1,18 @@
package com.magnatune.eyecreate.companionformagnatune.viewholders;
import android.view.View;
import android.widget.TextView;
import com.magnatune.eyecreate.companionformagnatune.R;
import io.realm.RealmViewHolder;
public class ArtistViewHolder extends RealmViewHolder {
public TextView artistName;
public ArtistViewHolder(View itemView) {
super(itemView);
artistName = (TextView) itemView.findViewById(R.id.artist_name);
}
}

View File

@@ -4,6 +4,6 @@
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/artist_list"
app:rrvLayoutType="LinearLayout"
app:rrvLayoutType="LinearLayoutWithHeaders"
app:rrvIsRefreshable="true"
xmlns:android="http://schemas.android.com/apk/res/android" />

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_margin="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="?attr/selectableItemBackground"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/artist_name"/>
</LinearLayout>