Skip to content

Commit

Permalink
Merge pull request #101 from amitshekhariitbhu/development
Browse files Browse the repository at this point in the history
 Add example for room database
  • Loading branch information
amitshekhariitbhu authored Feb 12, 2018
2 parents 3e2482f + 3cd1c74 commit 4b35ebc
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 10 deletions.
10 changes: 6 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "24.0.3"
compileSdkVersion 27
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.sample"
minSdkVersion 15
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -47,8 +47,10 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:appcompat-v7:27.0.0'
testCompile 'junit:junit:4.12'
compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
compile "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
debugCompile project(':debug-db')
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import com.sample.database.ContactDBHelper;
import com.sample.database.ExtTestDBHelper;
import com.sample.database.PersonDBHelper;
import com.sample.database.room.User;
import com.sample.database.room.UserDBHelper;
import com.sample.utils.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -108,6 +112,19 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

// Room database
UserDBHelper userDBHelper = new UserDBHelper(getApplicationContext());
if (userDBHelper.count() == 0) {
List<User> userList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
User user = new User();
user.id = (long) (i + 1);
user.name = "user_" + i;
userList.add(user);
}
userDBHelper.insertUser(userList);
}

Utils.setCustomDatabaseFiles(getApplicationContext());
}

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/sample/database/room/AppDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sample.database.room;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;

/**
* Created by anandgaurav on 12/02/18.
*/
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

public abstract UserDao userDao();

}
17 changes: 17 additions & 0 deletions app/src/main/java/com/sample/database/room/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sample.database.room;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

/**
* Created by anandgaurav on 12/02/18.
*/
@Entity(tableName = "users")
public class User {

@PrimaryKey
public Long id;

public String name;

}
30 changes: 30 additions & 0 deletions app/src/main/java/com/sample/database/room/UserDBHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sample.database.room;

import android.arch.persistence.room.Room;
import android.content.Context;

import java.util.List;

/**
* Created by anandgaurav on 12/02/18.
*/

public class UserDBHelper {

private final AppDatabase appDatabase;

public UserDBHelper(Context context) {
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database")
.allowMainThreadQueries()
.build();
}

public void insertUser(List<User> userList) {
appDatabase.userDao().insertAll(userList);
}

public int count() {
return appDatabase.userDao().loadAll().size();
}

}
33 changes: 33 additions & 0 deletions app/src/main/java/com/sample/database/room/UserDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sample.database.room;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;

import java.util.List;

/**
* Created by anandgaurav on 12/02/18.
*/

@Dao
public interface UserDao {

@Query("SELECT * FROM users")
List<User> loadAll();

@Query("SELECT * FROM users WHERE id IN (:userIds)")
List<User> loadAllByIds(List<Integer> userIds);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(User user);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<User> users);

@Delete
void delete(User user);

}
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

Expand All @@ -36,6 +37,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
9 changes: 5 additions & 4 deletions debug-db/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "24.0.3"
compileSdkVersion 27
buildToolsVersion '25.0.3'

defaultConfig {
minSdkVersion 9
targetSdkVersion 25
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"

Expand All @@ -50,6 +50,7 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.8.0'
compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
compile "android.arch.persistence.room:runtime:1.0.0"
}

//apply from: 'debug-db-upload.gradle'
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

0 comments on commit 4b35ebc

Please sign in to comment.