Commits

Anonymous committed aba10c6

changed back to async loader from async task and fixed a bug that was resetting loader

Comments (0)

Files changed (5)

AndroidManifest.xml

         android:theme="@style/AppTheme" >
         <activity
             android:name="com.w4rlock.backpapermusic.MusicActivity"
+            android:launchMode="singleTop"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

src/com/w4rlock/backpapermusic/MusicPlayListActivity.java

 package com.w4rlock.backpapermusic;
 
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.Menu;
 		int id = item.getItemId();
 		if (id == android.R.id.home)
 		{
-//			Intent i = new Intent(this, MusicActivity.class);
-//			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-//			startActivity(i);
+			Intent i = new Intent(this, MusicActivity.class);
+			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+			startActivity(i);
 			finish();
 			return true;
 		}

src/com/w4rlock/backpapermusic/MusicPlayerActivity.java

 package com.w4rlock.backpapermusic;
 
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.Menu;
 	public boolean onOptionsItemSelected(MenuItem item)
 	{
 		int id = item.getItemId();
-		if(id == android.R.id.home){
-//			Intent i = new Intent(this,MusicActivity.class);
-//			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-//			startActivity(i);
-			finish();
+		if (id == android.R.id.home)
+		{
+			Intent i = new Intent(this, MusicActivity.class);
+			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+			startActivity(i);
 			return true;
 		}
 		if (id == R.id.action_settings)

src/com/w4rlock/backpapermusic/fragments/MusicListFragment.java

 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
+import android.support.v4.app.LoaderManager;
+import android.support.v4.content.Loader;
 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.widget.SearchView;
 import android.view.LayoutInflater;
 import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.ListView;
+import android.widget.Toast;
 import android.widget.ViewFlipper;
 
 import com.w4rlock.backpapermusic.MusicPlayListActivity;
 import com.w4rlock.backpapermusic.adapters.MusicListViewAdapter;
 import com.w4rlock.backpapermusic.loaders.LoadSongsList;
 import com.w4rlock.backpapermusic.loaders.LoadingInterface;
-import com.w4rlock.backpapermusic.loaders.MusicDataAsyncTaskLoad;
+import com.w4rlock.backpapermusic.loaders.MusicDataLoader;
 import com.w4rlock.backpapermusic.models.AsyncResult;
 import com.w4rlock.backpapermusic.models.Song;
 import com.w4rlock.backpapermusic.models.SongItem;
 	private ViewFlipper mViewFlipper;
 	private GridView songsGridView;
 
+	final MusicLoaderCallbacks musicLoaderCallbacks = new MusicLoaderCallbacks();
+	private static final int LOADER_ID = 0;
+
 	public MusicListFragment()
 	{
 	}
 		mViewFlipper.setDisplayedChild(whichChild);
 	}
 
-	private final MusicDataAsyncTaskLoad.MusicDataAsyncTaskCallback musicLoadedCallback = new MusicDataAsyncTaskLoad.MusicDataAsyncTaskCallback()
-	{
-
-		@Override
-		public void onAsyncLoadComplete(AsyncResult<ArrayList<Song>> result)
-		{
-			if (result.isError())
-			{
-				return;
-			}
-			musics.clear();
-			for (Song m : result.getResult())
-			{
-				musics.add(m);
-			}
-			if(mMusicAdapter != null)
-				mMusicAdapter.notifyDataSetChanged();
-			if(mMusicGridAdapter != null)
-				mMusicGridAdapter.notifyDataSetChanged();
-		}
-	};
-
 	private void loadData()
 	{
-
-		MusicDataAsyncTaskLoad musicDataAsyncTaskLoad = new MusicDataAsyncTaskLoad(getActivity(), (LoadingInterface) getActivity(),
-				musicLoadedCallback);
-		musicDataAsyncTaskLoad.execute();
+		getLoaderManager().initLoader(LOADER_ID, null, musicLoaderCallbacks);
 	}
 
 	public void forceLoadDataFromLoader()
 	{
 		L.d(DEBUG_TAG, "force load called ");
-
-		loadData();
+		try
+		{
+			getLoaderManager().restartLoader(0, null, musicLoaderCallbacks);
+		} catch (Exception e)
+		{
+			L.d(DEBUG_TAG, "force load called but unable to fulfill it so failing silently");
+		}
 
 	}
 
 			}
 			mMusicAdapter = new MusicListViewAdapter(getActivity(), tempMusic);
 		}
-		if(songsList != null)
+		if (songsList != null)
 			songsList.setAdapter(mMusicAdapter);
 	}
 
 	{
 		super.onPause();
 	}
+
+	private class MusicLoaderCallbacks implements LoaderManager.LoaderCallbacks<AsyncResult<ArrayList<Song>>>
+	{
+
+		@Override
+		public Loader<AsyncResult<ArrayList<Song>>> onCreateLoader(int id, Bundle args)
+		{
+			return new MusicDataLoader(getActivity());
+		}
+
+		@Override
+		public void onLoadFinished(Loader<AsyncResult<ArrayList<Song>>> loader, AsyncResult<ArrayList<Song>> data)
+		{
+			if (data.getResult() == null || data.isError())
+			{
+				Toast.makeText(getActivity(), "No Data Found locally and unable to fetch data online", Toast.LENGTH_LONG).show();
+				return;
+			}
+			L.d("MusicLoaderCallbacks", data.getResult().size() + "");
+			if (data.isError())
+			{
+				return;
+			}
+			musics.clear();
+			for (Song m : data.getResult())
+			{
+				musics.add(m);
+			}
+			if(mMusicAdapter != null)
+				mMusicAdapter.notifyDataSetChanged();
+			if(mMusicGridAdapter != null)
+				mMusicGridAdapter.notifyDataSetChanged();
+		}
+
+		@Override
+		public void onLoaderReset(Loader<AsyncResult<ArrayList<Song>>> loader)
+		{
+
+		}
+
+	}
 }

src/com/w4rlock/backpapermusic/loaders/MusicDataLoader.java

 
 }
 
-
-//private class MusicLoaderCallbacks implements LoaderManager.LoaderCallbacks<AsyncResult<ArrayList<Song>>>
-//{
-//
-//	@Override
-//	public Loader<AsyncResult<ArrayList<Song>>> onCreateLoader(int id, Bundle args)
-//	{
-//		return new MusicDataLoader(getActivity());
-//	}
-//
-//	@Override
-//	public void onLoadFinished(Loader<AsyncResult<ArrayList<Song>>> loader, AsyncResult<ArrayList<Song>> data)
-//	{
-//		networkTries += 1;
-//		if (data.getResult() == null)
-//		{
-//			if (networkTries <= NETWORK_MAX_TRIES)
-//			{
-//				L.d("MusicLoaderCallbacks", "null data retrying");
-//				loader.reset();
-//				getLoaderManager().restartLoader(loader.getId(), null, this);
-//				return;
-//			}
-//			Toast.makeText(getActivity(), "Tries Exceeded Please Try Later", Toast.LENGTH_LONG).show();
-//			getActivity().finish();
-//			return;
-//		}
-//		L.d("MusicLoaderCallbacks", data.getResult().size() + "");
-//		if (data.isError())
-//		{
-//			return;
-//		}
-//		musics.clear();
-//		networkTries = 0;
-//		for (Song m : data.getResult())
-//		{
-//			musics.add(m);
-//		}
-//		mMusicAdapter.notifyDataSetChanged();
-//	}
-//
-//	@Override
-//	public void onLoaderReset(Loader<AsyncResult<ArrayList<Song>>> loader)
-//	{
-//
-//	}
-//
-//}
-