From 75e5536adb2888ec59319c9b8a1f81525e6d5227 Mon Sep 17 00:00:00 2001 From: Anamika Tripathi Date: Thu, 29 Mar 2018 23:54:21 +0530 Subject: [PATCH] room library added for SQLite (#13) --- app/build.gradle | 6 +- .../majorproject/HomeScreen.java | 19 ++++- .../majorproject/RoomSample/AppDatabase.java | 33 ++++++++ .../RoomSample/DatabaseInitializer.java | 61 +++++++++++++++ .../majorproject/RoomSample/Project.java | 75 +++++++++++++++++++ .../majorproject/RoomSample/ProjectDao.java | 27 +++++++ app/src/main/res/values/colors.xml | 11 ++- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 9 files changed, 227 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/example/androidtechies/majorproject/RoomSample/AppDatabase.java create mode 100644 app/src/main/java/com/example/androidtechies/majorproject/RoomSample/DatabaseInitializer.java create mode 100644 app/src/main/java/com/example/androidtechies/majorproject/RoomSample/Project.java create mode 100644 app/src/main/java/com/example/androidtechies/majorproject/RoomSample/ProjectDao.java diff --git a/app/build.gradle b/app/build.gradle index 9ede0a1..10200c9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,7 +28,11 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + + //Room dependencies + implementation 'android.arch.persistence.room:runtime:1.0.0' + annotationProcessor 'android.arch.persistence.room:compiler:1.0.0' // butter knife - compile 'com.jakewharton:butterknife:8.8.1' + implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } diff --git a/app/src/main/java/com/example/androidtechies/majorproject/HomeScreen.java b/app/src/main/java/com/example/androidtechies/majorproject/HomeScreen.java index 65a80ca..0e5eb3d 100644 --- a/app/src/main/java/com/example/androidtechies/majorproject/HomeScreen.java +++ b/app/src/main/java/com/example/androidtechies/majorproject/HomeScreen.java @@ -6,12 +6,16 @@ import android.view.View; import android.widget.Button; +import com.example.androidtechies.majorproject.RoomSample.AppDatabase; +import com.example.androidtechies.majorproject.RoomSample.DatabaseInitializer; + public class HomeScreen extends AppCompatActivity { public static final String HomeScreenTag = "HomeScreen"; public static final Integer cseValue = 0; public static final Integer it = 0; public static final Integer ece = 0; public static final Integer eee = 0; + AppDatabase appDatabase; @Override protected void onCreate(Bundle savedInstanceState) { @@ -24,12 +28,17 @@ protected void onCreate(Bundle savedInstanceState) { cse.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent cseIntent = new Intent(HomeScreen.this, ListPage.class); - cseIntent.putExtra(HomeScreenTag, cseValue); - startActivity(cseIntent); +// Intent cseIntent = new Intent(HomeScreen.this, ListPage.class); +// cseIntent.putExtra(HomeScreenTag, cseValue); +// startActivity(cseIntent); + populateDatabase(); } }); + + + + // ece.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { @@ -55,5 +64,9 @@ public void onClick(View view) { // } // }); } + + private void populateDatabase() { + DatabaseInitializer.populateAsync(AppDatabase.getAppDatabase(this)); } +} diff --git a/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/AppDatabase.java b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/AppDatabase.java new file mode 100644 index 0000000..17fa5ca --- /dev/null +++ b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/AppDatabase.java @@ -0,0 +1,33 @@ +package com.example.androidtechies.majorproject.RoomSample; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.Room; +import android.arch.persistence.room.RoomDatabase; +import android.content.Context; + + +@Database(entities = {Project.class}, version = 1) +public abstract class AppDatabase extends RoomDatabase { + + private static AppDatabase INSTANCE; + + public abstract ProjectDao projectDao(); + + + public static AppDatabase getAppDatabase(Context context) { + if (INSTANCE == null) { + INSTANCE = + Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database") + // allow queries on the main thread. + // Not suggested to do on real application! + .allowMainThreadQueries() + .build(); + } + return INSTANCE; + } + + public static void destroyInstance() { + INSTANCE = null; + } + +} diff --git a/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/DatabaseInitializer.java b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/DatabaseInitializer.java new file mode 100644 index 0000000..ab45c0d --- /dev/null +++ b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/DatabaseInitializer.java @@ -0,0 +1,61 @@ +package com.example.androidtechies.majorproject.RoomSample; + +import android.content.Context; +import android.os.AsyncTask; +import android.support.annotation.NonNull; +import android.util.Log; + +import java.util.List; + +public class DatabaseInitializer { + private static final String TAG = DatabaseInitializer.class.getName(); + + + public static void populateAsync(@NonNull final AppDatabase db) { + PopulateDbAsync task = new PopulateDbAsync(db); + task.execute(); + } + + public static void populateSync(@NonNull final AppDatabase db) { + populateWithTestData(db); + } + + private static Project addProject (final AppDatabase db, Project project) { + db.projectDao().insertAll(project); + return project; + } + + private static void populateWithTestData(AppDatabase db) { + + Project project = new Project(); + project.setTitleOfProject("Hinton"); + project.setIntroOfProject("Hinton is a fake news generator (video) platform that aims to create\n" + + " awareness among the society "); + project.setModulesOfProject("news"); + project.setProjectBranch("IT"); + project.setTechnologyUsed("Machine learning"); + addProject(db, project); + + List projectsList = db.projectDao().getAll(); + + int count = db.projectDao().countProjects(); + // Log.d(DatabaseInitializer.TAG,); + Log.d(DatabaseInitializer.TAG, "Branch name: " + projectsList.get(0).getProjectBranch()+ "\nNumber of rows: "+count); + } + + private static class PopulateDbAsync extends AsyncTask { + + private final AppDatabase mDb; + + PopulateDbAsync(AppDatabase db) { + mDb = db; + } + + @Override + protected Void doInBackground(final Void... params) { + populateWithTestData(mDb); + return null; + } + + } +} diff --git a/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/Project.java b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/Project.java new file mode 100644 index 0000000..dab46d9 --- /dev/null +++ b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/Project.java @@ -0,0 +1,75 @@ +package com.example.androidtechies.majorproject.RoomSample; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; + +@Entity(tableName = "project_table") +public class Project { + + @PrimaryKey(autoGenerate = true) + private int id; + + @ColumnInfo(name = "branch") + private String projectBranch; + + @ColumnInfo(name = "title_project") + private String titleOfProject; + + @ColumnInfo(name = "intro_project") + private String introOfProject; + + @ColumnInfo(name = "technology_used") + private String technologyUsed; + + @ColumnInfo(name = "modules") + private String modulesOfProject; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getProjectBranch() { + return projectBranch; + } + + public void setProjectBranch(String projectBranch) { + this.projectBranch = projectBranch; + } + + public String getTitleOfProject() { + return titleOfProject; + } + + public void setTitleOfProject(String titleOfProject) { + this.titleOfProject = titleOfProject; + } + + public String getIntroOfProject() { + return introOfProject; + } + + public void setIntroOfProject(String introOfProject) { + this.introOfProject = introOfProject; + } + + public String getTechnologyUsed() { + return technologyUsed; + } + + public void setTechnologyUsed(String technologyUsed) { + this.technologyUsed = technologyUsed; + } + + public String getModulesOfProject() { + return modulesOfProject; + } + + public void setModulesOfProject(String modulesOfProject) { + this.modulesOfProject = modulesOfProject; + } +} diff --git a/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/ProjectDao.java b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/ProjectDao.java new file mode 100644 index 0000000..26ded65 --- /dev/null +++ b/app/src/main/java/com/example/androidtechies/majorproject/RoomSample/ProjectDao.java @@ -0,0 +1,27 @@ +package com.example.androidtechies.majorproject.RoomSample; + + +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; + +@Dao +public interface ProjectDao { + + @Query("SELECT * FROM project_table") + List getAll(); + + + @Query("SELECT COUNT(*) from project_table") + int countProjects(); + + @Insert + void insertAll(Project... users); + + @Delete + void delete(Project user); + +} diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index e04526b..fe20846 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,8 +1,11 @@ - #9E9E9E - #616161 - #FF4081 + #673AB7 + #512DA8 + #607D8B #000000 - + #212121 + #757575 + #FFFFFF + #BDBDBD diff --git a/build.gradle b/build.gradle index e6b32bc..47825b2 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.0' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 57a3ad8..abedcad 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Mar 13 13:19:59 IST 2018 +#Tue Mar 27 20:08:43 IST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip