Skip to content

Commit

Permalink
Merge pull request #132 from WatchFriends/MichielDev
Browse files Browse the repository at this point in the history
Contentprovider & SQLite
  • Loading branch information
mickverm authored Dec 5, 2016
2 parents b367c2a + 14580d0 commit 9c5ff9f
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 334 deletions.
4 changes: 3 additions & 1 deletion WatchFriends/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ dependencies {
compile 'com.github.sayyam:carouselview:4d8ae40889'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.github.hotchemi:permissionsdispatcher:2.2.0'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.2.0'

compile 'com.facebook.stetho:stetho:1.4.1'

annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.2.0'
testCompile 'junit:junit:4.12'
}
10 changes: 3 additions & 7 deletions WatchFriends/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,14 @@
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />

<provider
android:name=".provider.FavoritesProvider"
android:authorities="nmct.jaspernielsmichielhein.watchfriends"
android:exported="false" />
<provider
android:name=".provider.WatchedProvider"
android:authorities="nmct.jaspernielsmichielhein.watchfriends"
android:authorities="nmct.jaspernielsmichielhein.watchfriends.watchedepisodes"
android:exported="false" />
<provider
android:name=".provider.FollowedSeriesProvider"
android:authorities="nmct.jaspernielsmichielhein.watchfriends"
android:exported="false" />
android:authorities="nmct.jaspernielsmichielhein.watchfriends.followedseries"
android:exported="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package nmct.jaspernielsmichielhein.watchfriends.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -13,6 +17,9 @@
import android.widget.ListView;

import nmct.jaspernielsmichielhein.watchfriends.R;
import nmct.jaspernielsmichielhein.watchfriends.database.Contract;
import nmct.jaspernielsmichielhein.watchfriends.database.tasks.DeleteWatchedEpisodeFromDBTask;
import nmct.jaspernielsmichielhein.watchfriends.database.tasks.SaveWatchedEpisodeToDBTask;
import nmct.jaspernielsmichielhein.watchfriends.databinding.RowEpisodeBinding;
import nmct.jaspernielsmichielhein.watchfriends.helper.Interfaces;
import nmct.jaspernielsmichielhein.watchfriends.model.Episode;
Expand Down Expand Up @@ -46,26 +53,61 @@ public View getView(int position, View convertView, ViewGroup parent) {
rowEpisodeBinding.setEpisode(episode);

ImageButton imgViewed = (ImageButton) rowEpisodeBinding.getRoot().findViewById(R.id.imgViewed);
imgViewed.setTag(position);
if (watched) {
imgViewed.setImageResource(R.drawable.ic_visibility_off_white_24dp);
} else {
imgViewed.setImageResource(R.drawable.ic_visibility_white_24dp);
}
imgViewed.setOnClickListener(this);

return rowEpisodeBinding.getRoot();
}

@Override
public void onClick(View v) {
ImageButton imgViewed = (ImageButton) v;
int position = (int) imgViewed.getTag();
Episode e = getItem(position);

if (watched) {
imgViewed.setImageResource(R.drawable.ic_visibility_white_24dp);
Snackbar.make(v, "Marked episode as not watched", Snackbar.LENGTH_LONG).show();

unWatch(e);

} else {
imgViewed.setImageResource(R.drawable.ic_visibility_off_white_24dp);
Snackbar.make(v, "Marked episode as watched", Snackbar.LENGTH_LONG).show();

watch(e);
}
watched = !watched;
}

private void watch(Episode e) {
ContentValues values = new ContentValues();
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SERIES_NR, e.getId());
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SEASON_NR, e.getSeason_number());
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NR, e.getEpisode_number());
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NAME, e.getName());

executeAsyncTask(new SaveWatchedEpisodeToDBTask(context), values);
}

private void unWatch(Episode e) {
ContentValues values = new ContentValues();
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SERIES_NR, e.getId());
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SEASON_NR, e.getSeason_number());
values.put(Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NR, e.getEpisode_number());

executeAsyncTask(new DeleteWatchedEpisodeFromDBTask(context), values);
}

static private <T> void executeAsyncTask(AsyncTask<T, ?, ?> task, T... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,7 @@
public class Contract {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "database.db";
private static final String TEXT_TYPE = " TEXT";

//interface uitbreiden
public interface FavoritesColumns extends BaseColumns {
public static final String TABLE_NAME = "favorites";
public static final String COLUMN_FAVORITE_NR = "favoritenr";
public static final String COLUMN_FAVORITE_NAME = "favoritename";
}

public static abstract class FavoritesDB implements FavoritesColumns {
public static final String CREATE_TABLE = "create table "
+ TABLE_NAME + "(" + _ID
+ " integer primary key autoincrement, "
+ COLUMN_FAVORITE_NR + " integer, "
+ COLUMN_FAVORITE_NAME + " text not null"
+ ")";

public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}
public static final String DATABASE_NAME = "watchfriends.db";

public interface FollowedSeriesColumns extends BaseColumns {
public static final String TABLE_NAME = "followingseries";
Expand All @@ -43,18 +24,22 @@ public static abstract class FollowedSeriesDB implements FollowedSeriesColumns {
public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}

public interface WatchedColumns extends BaseColumns {
public interface WatchedEpisodeColumns extends BaseColumns {
public static final String TABLE_NAME = "watched";
public static final String COLUMN_WATCHED_NR = "watchednr";
public static final String COLUMN_WATCHED_NAME = "watchedname";
public static final String COLUMN_WATCHED_SERIES_NR = "watchedseriesnr";
public static final String COLUMN_WATCHED_SEASON_NR = "watchedseasonnr";
public static final String COLUMN_WATCHED_EPISODE_NR = "watchedepisodenr";
public static final String COLUMN_WATCHED_EPISODE_NAME = "watchedname";
}

public static abstract class WatchedDB implements WatchedColumns {
public static abstract class WatchedEpisodeDB implements WatchedEpisodeColumns {
public static final String CREATE_TABLE = "create table "
+ TABLE_NAME + "(" + _ID
+ " integer primary key autoincrement, "
+ COLUMN_WATCHED_NR + " integer, "
+ COLUMN_WATCHED_NAME + " text not null"
+ COLUMN_WATCHED_SERIES_NR + " integer, "
+ COLUMN_WATCHED_SEASON_NR + " integer, "
+ COLUMN_WATCHED_EPISODE_NR + " integer, "
+ COLUMN_WATCHED_EPISODE_NAME + " text not null"
+ ")";

public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import nmct.jaspernielsmichielhein.watchfriends.database.Contract;

public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper INSTANCE;
private static Object object = new Object();
Expand Down Expand Up @@ -41,7 +39,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
oldVersion++;
break;
case 1:
//upgrade logic from version 1 to 2
case 2:
//upgrade logic from version 2 to 3
case 3:
Expand All @@ -55,7 +52,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

private void upgradeTo1(SQLiteDatabase db) {
db.execSQL(Contract.FavoritesDB.CREATE_TABLE);
db.execSQL(Contract.FollowedSeriesDB.CREATE_TABLE);
db.execSQL(Contract.WatchedEpisodeDB.CREATE_TABLE);
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ protected void onStartLoading() {
@Override
public Cursor loadInBackground() {
String[] columns = new String[]{
Contract.WatchedColumns._ID,
Contract.WatchedColumns.COLUMN_WATCHED_NR,
Contract.WatchedColumns.COLUMN_WATCHED_NAME
Contract.WatchedEpisodeColumns._ID,
Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SERIES_NR,
Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SEASON_NR,
Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NR,
Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NAME
};

mData = mContext.getContentResolver().query(nmct.jaspernielsmichielhein.watchfriends.provider.Contract.WATCHED_URI, columns, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nmct.jaspernielsmichielhein.watchfriends.database.tasks;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;

import java.util.ArrayList;

import nmct.jaspernielsmichielhein.watchfriends.provider.Contract;

public class DeleteFollowedSerieFromDBTask extends AsyncTask<ContentValues, Void, Void> {

private Context mContext;

public DeleteFollowedSerieFromDBTask(Context context) {
mContext = context;
}

@Override
protected Void doInBackground(ContentValues... params) {
String[] args = new String[]{String.valueOf(params[0].get(nmct.jaspernielsmichielhein.watchfriends.database.Contract.FollowedSeriesColumns.COLUMN_FOLLOWEDSERIES_NR))};
mContext.getContentResolver().delete(
Contract.FOLLOWEDSERIES_URI,
nmct.jaspernielsmichielhein.watchfriends.database.Contract.FollowedSeriesColumns.COLUMN_FOLLOWEDSERIES_NR + "=?",
args
);
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nmct.jaspernielsmichielhein.watchfriends.database.tasks;

import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;

import nmct.jaspernielsmichielhein.watchfriends.provider.Contract;

public class DeleteWatchedEpisodeFromDBTask extends AsyncTask<ContentValues, Void, Void> {

private Context mContext;

public DeleteWatchedEpisodeFromDBTask(Context context) {
mContext = context;
}

@Override
protected Void doInBackground(ContentValues... params) {
String[] args = new String[]{
String.valueOf(params[0].get(nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SERIES_NR)),
String.valueOf(params[0].get(nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SEASON_NR)),
String.valueOf(params[0].get(nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NR))
};
mContext.getContentResolver().delete(
Contract.WATCHED_URI,
nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SERIES_NR + "=? AND "
+ nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_SEASON_NR + "=? AND "
+ nmct.jaspernielsmichielhein.watchfriends.database.Contract.WatchedEpisodeColumns.COLUMN_WATCHED_EPISODE_NR + "=?",
args
);
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package nmct.jaspernielsmichielhein.watchfriends.database;
package nmct.jaspernielsmichielhein.watchfriends.database.tasks;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;

import nmct.jaspernielsmichielhein.watchfriends.provider.*;
import nmct.jaspernielsmichielhein.watchfriends.provider.Contract;

public class SaveFavoriteToDBTask extends AsyncTask<ContentValues, Void, Void> {
public class SaveFollowedSerieToDBTask extends AsyncTask<ContentValues, Void, Void> {

private Context mContext;
public SaveFavoriteToDBTask(Context context) {mContext = context; }
public SaveFollowedSerieToDBTask(Context context) {mContext = context; }

@Override
protected Void doInBackground(ContentValues... params) {
Uri newUri = mContext.getContentResolver().insert(Contract.FAVORITES_URI, params[0]);
mContext.getContentResolver().insert(Contract.FOLLOWEDSERIES_URI, params[0]);
return null;
}

Expand Down
Loading

0 comments on commit 9c5ff9f

Please sign in to comment.