Commit bb0f6387 authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge branch 'master' into classes

parents ae4dbf50 ccac092a
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
...@@ -92,10 +92,6 @@ ...@@ -92,10 +92,6 @@
android:exported="false"></service> android:exported="false"></service>
<provider
android:name="in.ac.iitb.gymkhana.iitbapp.data.IITBAppContentProvider"
android:authorities="in.ac.iitb.gymkhana.iitbapp"
android:exported="false" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.TypeConverters;
import android.content.Context;
/**
* Created by mrunz on 14/3/18.
*/
@Database(entities = {Event.class, Body.class,Venue.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract DbDao dbDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database")
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
...@@ -2,13 +2,17 @@ package in.ac.iitb.gymkhana.iitbapp.data; ...@@ -2,13 +2,17 @@ package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity; import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
@Entity @Entity(tableName = "bodies")
class Body { class Body {
@PrimaryKey(autoGenerate = true)
int db_id;
@ColumnInfo(name = "id") @ColumnInfo(name = "id")
@SerializedName("id") @SerializedName("id")
String bodyID; String bodyID;
...@@ -108,4 +112,4 @@ class Body { ...@@ -108,4 +112,4 @@ class Body {
public void setBodyFollowersCount(int bodyFollowersCount) { public void setBodyFollowersCount(int bodyFollowersCount) {
this.bodyFollowersCount = bodyFollowersCount; this.bodyFollowersCount = bodyFollowersCount;
} }
} }
\ No newline at end of file
package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.TypeConverter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mrunz on 14/3/18.
*/
public class Converters {
@TypeConverter
public static List<Event> eventsfromString(String value) {
Type listType = new TypeToken<List<Event>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String stringfromEvents(List<Event> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
@TypeConverter
public static List<User> usersfromString(String value) {
Type listType = new TypeToken<List<User>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String stringfromUsers(List<User> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
@TypeConverter
public static List<Venue> venuesfromString(String value) {
Type listType = new TypeToken<List<Venue>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String stringfromVenues(List<Venue> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
@TypeConverter
public static List<Body> bodiesfromString(String value) {
Type listType = new TypeToken<List<Body>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String stringfromBodies(List<Body> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
}
\ No newline at end of file
package in.ac.iitb.gymkhana.iitbapp.data;
import android.net.Uri;
import android.provider.BaseColumns;
public class DatabaseContract {
public static final String CONTENT_AUTHORITY = "in.ac.iitb.gymkhana.iitbapp";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_MAP = "map";
public static final String PATH_USER_PROFILE = "userProfile";
public static final String PATH_USER_FOLLOWERS = "userFollowers";
public static final String PATH_USER_FOLLOWS = "userFollows";
public static final String PATH_NEWS_FEED = "newsFeed";
public static final class MapEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_MAP)
.build();
public static final String TABLE_NAME = "map";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_TYPE = "type";
}
public static final class UserProfileEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_USER_PROFILE)
.build();
public static final String TABLE_NAME = "userProfile";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_USER_ROLLNO = "user_rollno";
public static final String COLUMN_USER_POR = "user_por";
public static final String COLUMN_USER_PROFILE_PICTURE = "user_profile_picture";
public static final String COLUMN_USER_HOSTELNO = "user_hostelno";
public static final String COLUMN_USER_ABOUTME = "user_aboutme";
public static final String COLUMN_USER_FOLLOWING_COUNT = "user_following_count";
public static final String COLUMN_USER_FOLLOWERS_COUNT = "user_follwers_count";
public static final String COLUMN_USER_EVENTS_COUNT = "user_events_count";
public static final String COLUMN_IS_FOLLOWED = "isFollowed";
public static final String COLUMN_FOLLOWS_YOU = "followsYou";
public static final String COLUMN_USER_ROOM_NO = "user_roomno";
public static final String COLUMN_USER_PHONE_NO = "user_phoneno";
}
public static final class UserFollowersEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_USER_FOLLOWERS)
.build();
public static final String TABLE_NAME = "userFollowers";
public static final String COLUMN_USER_PROFILE_PICTURE = "user_profile_picture";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_USER_PROFILE = "userProfile";
}
public static final class UserFollowsEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_USER_FOLLOWS)
.build();
public static final String TABLE_NAME = "userFollows";
public static final String COLUMN_USER_PROFILE_PICTURE = "user_profile_picture";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_USER_PROFILE = "userProfile";
}
public static final class NewsFeedEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_NEWS_FEED)
.build();
public static final String TABLE_NAME = "newsFeed";
public static final String COLUMN_EVENT_NAME = "event_name";
public static final String COLUMN_EVENT_DESCRIPTION = "event_description";
public static final String COLUMN_EVENT_IMAGE = "event_image";
public static final String COLUMN_EVENT_CREATOR_NAME = "event_creator_name";
public static final String COLUMN_EVENT_CREATOR_ID = "event_creator_id";
public static final String COLUMN_EVENT_GOING_STATUS = "event_going_status";
}
}
package in.ac.iitb.gymkhana.iitbapp.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "IITBAppDb.db";
private static final int VERSION = 1;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_TABLE_MAP = "CREATE TABLE " + DatabaseContract.MapEntry.TABLE_NAME + " (" +
DatabaseContract.MapEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.MapEntry.COLUMN_LATITUDE + " DOUBLE NOT NULL, " +
DatabaseContract.MapEntry.COLUMN_LONGITUDE + " DOUBLE NOT NULL, " +
DatabaseContract.MapEntry.COLUMN_NAME + " TEXT NOT NULL, " +
DatabaseContract.MapEntry.COLUMN_TYPE + " TEXT NOT NULL);";
final String CREATE_TABLE_USER_PROFILE = "CREATE TABLE " + DatabaseContract.UserProfileEntry.TABLE_NAME + " (" +
DatabaseContract.UserProfileEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_NAME + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_ROLLNO + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_POR + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_PROFILE_PICTURE + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_HOSTELNO + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_ABOUTME + " TEXT NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_FOLLOWING_COUNT + " INTEGER NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_FOLLOWERS_COUNT + " INTEGER NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_EVENTS_COUNT + " INTEGER NOT NULL, " +
DatabaseContract.UserProfileEntry.COLUMN_IS_FOLLOWED + " BOOLEAN, " +
DatabaseContract.UserProfileEntry.COLUMN_FOLLOWS_YOU + " BOOLEAN, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_ROOM_NO + " TEXT, " +
DatabaseContract.UserProfileEntry.COLUMN_USER_PHONE_NO + " TEXT);";
final String CREATE_TABLE_USER_FOLLOWERS = "CREATE TABLE " + DatabaseContract.UserFollowersEntry.TABLE_NAME + " (" +
DatabaseContract.UserFollowersEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.UserFollowersEntry.COLUMN_USER_PROFILE_PICTURE + " TEXT NOT NULL, " +
DatabaseContract.UserFollowersEntry.COLUMN_USER_NAME + " TEXT NOT NULL, " +
DatabaseContract.UserFollowersEntry.COLUMN_USER_PROFILE + " TEXT NOT NULL);";
final String CREATE_TABLE_USER_FOLLOWS = "CREATE TABLE " + DatabaseContract.UserFollowsEntry.TABLE_NAME + " (" +
DatabaseContract.UserFollowsEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.UserFollowsEntry.COLUMN_USER_PROFILE_PICTURE + " TEXT NOT NULL, " +
DatabaseContract.UserFollowsEntry.COLUMN_USER_NAME + " TEXT NOT NULL, " +
DatabaseContract.UserFollowsEntry.COLUMN_USER_PROFILE + " TEXT NOT NULL);";
final String CREATE_TABLE_NEWS_FEED = "CREATE TABLE " + DatabaseContract.NewsFeedEntry.TABLE_NAME + " (" +
DatabaseContract.NewsFeedEntry._ID + " INTEGER PRIMARY KEY, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_NAME + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_ID + " TEXT NOT NULL, " +
DatabaseContract.NewsFeedEntry.COLUMN_EVENT_GOING_STATUS + " INTEGER NOT NULL);";
db.execSQL(CREATE_TABLE_MAP);
db.execSQL(CREATE_TABLE_USER_PROFILE);
db.execSQL(CREATE_TABLE_USER_FOLLOWERS);
db.execSQL(CREATE_TABLE_USER_FOLLOWS);
db.execSQL(CREATE_TABLE_NEWS_FEED);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.MapEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserProfileEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowersEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.UserFollowsEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + DatabaseContract.NewsFeedEntry.TABLE_NAME);
onCreate(db);
}
}
package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import java.util.List;
import retrofit2.http.DELETE;
/**
* Created by mrunz on 13/3/18.
*/
@Dao
public interface DbDao {
@Query("SELECT * FROM events")
List<Event> getAllEvents();
@Query("SELECT * FROM bodies")
List<Body> getAllBodies();
@Query("SELECT * FROM venues")
List<Venue> getAllVenues();
@Query("SELECT COUNT(*) from events")
int countEvents();
@Query("SELECT COUNT(*) from venues")
int countVenues();
@Query("SELECT COUNT(*) from bodies")
int countBodies();
@Insert
void insertEvents(List<Event> events);
@Insert
void insertBodies(List<Body> bodies);
@Insert
void insertVenues(List<Venue> venues);
@Delete
void deleteEvent(Event event);
@Delete
void deleteVenue(Venue venue);
@Delete
void deleteBody(Body body);
@Query("DELETE from events")
void deleteEvents();
@Query("DELETE from venues")
void deleteVenues();
@Query("DELETE from bodies")
void deleteBodies();
}
package in.ac.iitb.gymkhana.iitbapp.data; package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.List; import java.util.List;
@Entity(tableName = "events")
public class Event { public class Event {
@PrimaryKey(autoGenerate = true)
int db_id;
@ColumnInfo(name = "id") @ColumnInfo(name = "id")
@SerializedName("id") @SerializedName("id")
String eventID; String eventID;
......
package in.ac.iitb.gymkhana.iitbapp.data;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
public class IITBAppContentProvider extends ContentProvider {
public static final int LOCS = 100;
public static final int LOC_WITH_ID = 101;
public static final int USER_PROFILES = 200;
public static final int USER_PROFILE_WITH_ID = 201;
public static final int USER_FOLLOWERS = 300;
public static final int USER_FOLLOWER_WITH_ID = 301;
public static final int USER_FOLLOWS = 400;
public static final int USER_FOLLOWS_WITH_ID = 401;
public static final int NEWS_FEED = 500;
public static final int NEWS_FEED_WITH_ID = 501;
private static final UriMatcher sUriMatcher = buildUriMatcher();
private DatabaseHelper databaseHelper;
public static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_MAP, LOCS);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_MAP + "/#", LOC_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_PROFILE, USER_PROFILES);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_PROFILE + "/#", USER_PROFILE_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWERS, USER_FOLLOWERS);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWERS + "/#", USER_FOLLOWER_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS, USER_FOLLOWS);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_USER_FOLLOWS + "/#", USER_FOLLOWS_WITH_ID);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_NEWS_FEED, NEWS_FEED);
matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_NEWS_FEED + "/#", NEWS_FEED_WITH_ID);
return matcher;
}
@Override
public boolean onCreate() {
Context context = getContext();
databaseHelper = new DatabaseHelper(context);
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
final SQLiteDatabase db = databaseHelper.getReadableDatabase();
int match = sUriMatcher.match(uri);
Cursor cursor;
String id;
String selectionArguments[];
switch (match) {
case LOCS:
cursor = db.query(DatabaseContract.MapEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case LOC_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.MapEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
case USER_PROFILES:
cursor = db.query(DatabaseContract.UserProfileEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case USER_PROFILE_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.UserProfileEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
case USER_FOLLOWERS:
cursor = db.query(DatabaseContract.UserFollowersEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case USER_FOLLOWER_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.UserFollowersEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
case USER_FOLLOWS:
cursor = db.query(DatabaseContract.UserFollowsEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case USER_FOLLOWS_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.UserFollowsEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
case NEWS_FEED:
cursor = db.query(DatabaseContract.NewsFeedEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
selectionArguments = new String[]{id};
cursor = db.query(DatabaseContract.NewsFeedEntry.TABLE_NAME,
projection,
"_id=?",
selectionArguments,
null,
null,
sortOrder);
break;
default:
throw new SQLException("Wrong Uri: " + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
int match = sUriMatcher.match(uri);
switch (match) {
case LOCS:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_MAP;
case LOC_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_MAP;
case USER_PROFILES:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_PROFILE;
case USER_PROFILE_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_PROFILE;
case USER_FOLLOWERS:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWERS;
case USER_FOLLOWER_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWERS;
case USER_FOLLOWS:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS;
case USER_FOLLOWS_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_USER_FOLLOWS;
case NEWS_FEED:
return "vnd.android.cursor.dir" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_NEWS_FEED;
case NEWS_FEED_WITH_ID:
return "vnd.android.cursor.item" + "/" + DatabaseContract.CONTENT_AUTHORITY + "/" + DatabaseContract.PATH_NEWS_FEED;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
final SQLiteDatabase db = databaseHelper.getWritableDatabase();
int match = sUriMatcher.match(uri);
Uri returnUri;
long id;
switch (match) {
case LOCS:
id = db.insert(DatabaseContract.MapEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.MapEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
case USER_PROFILES:
id = db.insert(DatabaseContract.UserProfileEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.UserProfileEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
case USER_FOLLOWERS:
id = db.insert(DatabaseContract.UserFollowersEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.UserFollowersEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
case USER_FOLLOWS:
id = db.insert(DatabaseContract.UserFollowsEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.UserFollowsEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
case NEWS_FEED:
id = db.insert(DatabaseContract.NewsFeedEntry.TABLE_NAME, null, values);
if (id > 0) {
returnUri = ContentUris.withAppendedId(DatabaseContract.NewsFeedEntry.CONTENT_URI, id);
} else
throw new SQLException("Failed to insert row into " + uri);
break;
default:
throw new SQLException("Wrong uri: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
final SQLiteDatabase db = databaseHelper.getWritableDatabase();
int rowsInserted;
switch (sUriMatcher.match(uri)) {
case LOCS:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.MapEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
case USER_PROFILES:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.UserProfileEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
case USER_FOLLOWERS:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.UserFollowersEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
case USER_FOLLOWS:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.UserFollowsEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
case NEWS_FEED:
db.beginTransaction();
rowsInserted = 0;
try {
for (ContentValues value : values) {
long _id = db.insert(DatabaseContract.NewsFeedEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
default:
return super.bulkInsert(uri, values);
}
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
int numRowsDeleted;
String id;
if (null == selection) selection = "1";
switch (sUriMatcher.match(uri)) {
case LOCS:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.MapEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case USER_PROFILES:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.UserProfileEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case USER_FOLLOWERS:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.UserFollowersEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case USER_FOLLOWS:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.UserFollowsEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case NEWS_FEED:
numRowsDeleted = databaseHelper.getWritableDatabase().delete(
DatabaseContract.NewsFeedEntry.TABLE_NAME,
selection,
selectionArgs);
break;
case LOC_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.MapEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
case USER_PROFILE_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserProfileEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
case USER_FOLLOWER_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserFollowersEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
case USER_FOLLOWS_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.UserFollowsEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
numRowsDeleted = databaseHelper.getWritableDatabase().delete(DatabaseContract.NewsFeedEntry.TABLE_NAME, "_id=?", new String[]{id});
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
if (numRowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return numRowsDeleted;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
int itemsUpdated;
int match = sUriMatcher.match(uri);
String id;
switch (match) {
case LOC_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.MapEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
case USER_PROFILE_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserProfileEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
case USER_FOLLOWER_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserFollowersEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
case USER_FOLLOWS_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.UserFollowsEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
case NEWS_FEED_WITH_ID:
id = uri.getPathSegments().get(1);
itemsUpdated = databaseHelper.getWritableDatabase().update(DatabaseContract.NewsFeedEntry.TABLE_NAME, values, "_id=?", new String[]{id});
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
if (itemsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return itemsUpdated;
}
}
...@@ -2,13 +2,17 @@ package in.ac.iitb.gymkhana.iitbapp.data; ...@@ -2,13 +2,17 @@ package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity; import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
@Entity @Entity(tableName = "users")
class User { class User {
@PrimaryKey(autoGenerate = true)
int db_id;
@ColumnInfo(name = "id") @ColumnInfo(name = "id")
@SerializedName("id") @SerializedName("id")
String userID; String userID;
......
package in.ac.iitb.gymkhana.iitbapp.data; package in.ac.iitb.gymkhana.iitbapp.data;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@Entity(tableName = "venues")
public class Venue { public class Venue {
@PrimaryKey(autoGenerate = true)
int db_id;
@ColumnInfo(name = "id") @ColumnInfo(name = "id")
@SerializedName("id") @SerializedName("id")
String venueID; String venueID;
......
...@@ -3,6 +3,7 @@ package in.ac.iitb.gymkhana.iitbapp.fragment; ...@@ -3,6 +3,7 @@ package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
...@@ -27,7 +28,7 @@ import in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter; ...@@ -27,7 +28,7 @@ import in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter;
import in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface; import in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface;
import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator; import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator;
import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse; import in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse;
import in.ac.iitb.gymkhana.iitbapp.data.DatabaseContract; import in.ac.iitb.gymkhana.iitbapp.data.AppDatabase;
import in.ac.iitb.gymkhana.iitbapp.data.Event; import in.ac.iitb.gymkhana.iitbapp.data.Event;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
...@@ -40,6 +41,7 @@ public class FeedFragment extends Fragment { ...@@ -40,6 +41,7 @@ public class FeedFragment extends Fragment {
private RecyclerView feedRecyclerView; private RecyclerView feedRecyclerView;
private SwipeRefreshLayout feedSwipeRefreshLayout; private SwipeRefreshLayout feedSwipeRefreshLayout;
private AppDatabase appDatabase;
public FeedFragment() { public FeedFragment() {
// Required empty public constructor // Required empty public constructor
...@@ -50,42 +52,34 @@ public class FeedFragment extends Fragment { ...@@ -50,42 +52,34 @@ public class FeedFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_feed, container, false); return inflater.inflate(R.layout.fragment_feed, container, false);
} }
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
// Cursor cursor = getContext().getContentResolver().query(DatabaseContract.NewsFeedEntry.CONTENT_URI, null, null, null, null);
// if (cursor.getCount() != 0) { appDatabase= AppDatabase.getAppDatabase(getContext());
// final List<Event> events = new ArrayList<>(); final List<Event> events=appDatabase.dbDao().getAllEvents();
// while (cursor.moveToNext()) { FeedAdapter feedAdapter = new FeedAdapter(events, new ItemClickListener() {
// Event event = new Event(cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME)), @Override
// cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION)), public void onItemClick(View v, int position) {
// cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE)), String eventJson = new Gson().toJson(events.get(position));
// cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_NAME)), Bundle bundle = new Bundle();
// cursor.getString(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_CREATOR_ID)), bundle.putString(Constants.EVENT_JSON, eventJson);
// cursor.getInt(cursor.getColumnIndex(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_GOING_STATUS))); EventFragment eventFragment = new EventFragment();
// events.add(event); eventFragment.setArguments(bundle);
// } FragmentManager manager = getActivity().getSupportFragmentManager();
// FeedAdapter feedAdapter = new FeedAdapter(events, new ItemClickListener() { FragmentTransaction transaction = manager.beginTransaction();
// @Override transaction.replace(R.id.framelayout_for_fragment, eventFragment, eventFragment.getTag());
// public void onItemClick(View v, int position) { transaction.commit();
// String eventJson = new Gson().toJson(events.get(position)); }
// Bundle bundle = new Bundle(); });
// bundle.putString(Constants.EVENT_JSON, eventJson); feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
// EventFragment eventFragment = new EventFragment(); feedRecyclerView.setAdapter(feedAdapter);
// eventFragment.setArguments(bundle); feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// FragmentManager manager = getActivity().getSupportFragmentManager();
// FragmentTransaction transaction = manager.beginTransaction();
// transaction.replace(R.id.framelayout_for_fragment, eventFragment, eventFragment.getTag());
// transaction.commit();
// }
// });
// feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
// feedRecyclerView.setAdapter(feedAdapter);
// feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//
// } // }
updateFeed(); updateFeed();
...@@ -127,22 +121,12 @@ public class FeedFragment extends Fragment { ...@@ -127,22 +121,12 @@ public class FeedFragment extends Fragment {
feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view); feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
feedRecyclerView.setAdapter(feedAdapter); feedRecyclerView.setAdapter(feedAdapter);
feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
long itemsRemoved = getContext().getContentResolver().delete(DatabaseContract.NewsFeedEntry.CONTENT_URI, null, null);
appDatabase.dbDao().deleteEvents();
Log.d("FeedFragment", itemsRemoved + " items removed."); appDatabase.dbDao().insertEvents(events);
ContentValues contentValues[] = new ContentValues[events.size()]; //Server Error
for (int i = 0; i < events.size(); i++) { feedSwipeRefreshLayout.setRefreshing(false);
ContentValues contentValues1 = new ContentValues();
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_NAME, events.get(i).getEventName());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_DESCRIPTION, events.get(i).getEventDescription());
contentValues1.put(DatabaseContract.NewsFeedEntry.COLUMN_EVENT_IMAGE, events.get(i).getEventImageURL());
contentValues[i] = contentValues1;
}
int insertCount = getContext().getContentResolver().bulkInsert(DatabaseContract.NewsFeedEntry.CONTENT_URI, contentValues);
Log.d("FeedFragment", Integer.toString(insertCount) + " elements inserted");
} }
//Server Error
feedSwipeRefreshLayout.setRefreshing(false);
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment