Skip to content

Commit

Permalink
room library added for SQLite (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekanamika authored Mar 29, 2018
1 parent 0fd7b7e commit 75e5536
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 11 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -55,5 +64,9 @@ public void onClick(View view) {
// }
// });
}

private void populateDatabase() {
DatabaseInitializer.populateAsync(AppDatabase.getAppDatabase(this));
}
}

Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Project> 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<Void, Void, Void> {

private final AppDatabase mDb;

PopulateDbAsync(AppDatabase db) {
mDb = db;
}

@Override
protected Void doInBackground(final Void... params) {
populateWithTestData(mDb);
return null;
}

}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Project> getAll();


@Query("SELECT COUNT(*) from project_table")
int countProjects();

@Insert
void insertAll(Project... users);

@Delete
void delete(Project user);

}
11 changes: 7 additions & 4 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#9E9E9E</color>
<color name="colorPrimaryDark">#616161</color>
<color name="colorAccent">#FF4081</color>
<color name="colorPrimary">#673AB7</color>
<color name="colorPrimaryDark">#512DA8</color>
<color name="colorAccent">#607D8B</color>
<color name="black">#000000</color>

<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 75e5536

Please sign in to comment.