- Room Databases are working

This commit is contained in:
Javier Varona 2017-09-10 19:13:32 -04:00
parent 19d9924bac
commit 8b9eee7fb2
12 changed files with 77 additions and 38 deletions

View file

@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 26
@ -10,6 +11,11 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
@ -28,7 +34,7 @@ dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'android.arch.persistence.room:runtime:1.0.0-alpha9';
apt "android.arch.persistence.room:compiler:1.0.0-alpha9";
}

View file

@ -1,15 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="carbon.crypto.com.carbon">
package="cy.agorise.crystalwallet">
<application
android:name="cy.agorise.crystalwallet.application.CrystalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".IntroActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View file

@ -3,6 +3,8 @@ package carbon.crypto.com.carbon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import cy.agorise.crystalwallet.R;
public class MainActivity extends AppCompatActivity {
@Override

View file

@ -3,8 +3,6 @@ package cy.agorise.crystalwallet;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import carbon.crypto.com.carbon.R;
public class IntroActivity extends AppCompatActivity {
@Override

View file

@ -2,7 +2,7 @@ package cy.agorise.crystalwallet.application;
import android.app.Application;
import cy.agorise.crystalwallet.dao.DatabaseConnection;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
/**
* Created by Henry Varona on 6/9/2017.
@ -15,6 +15,7 @@ public class CrystalApplication extends Application {
super.onCreate();
//initialize the database
DatabaseConnection.getConnection(this.getApplicationContext());
CrystalDatabase db = CrystalDatabase.getAppDatabase(this.getApplicationContext());
db.accountSeedDao().getAll();
}
}

View file

@ -0,0 +1,19 @@
package cy.agorise.crystalwallet.dao;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Query;
import java.util.List;
import cy.agorise.crystalwallet.models.AccountSeed;
/**
* Created by Henry Varona on 10/9/2017.
*/
@Dao
public interface AccountSeedDao {
@Query("SELECT * FROM account_seed")
List<AccountSeed> getAll();
}

View file

@ -0,0 +1,20 @@
package cy.agorise.crystalwallet.dao;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Query;
import java.util.List;
import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
/**
* Created by Henry Varona on 10/9/2017.
*/
@Dao
public interface CryptoNetAccountDao {
@Query("SELECT * FROM crypto_net_account")
List<CryptoNetAccount> getAll();
}

View file

@ -3,6 +3,7 @@ package cy.agorise.crystalwallet.dao;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.*;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -11,9 +12,25 @@ import cy.agorise.crystalwallet.models.CryptoNetAccount;
* Created by Henry Varona on 4/9/2017.
*/
@Database(entities = {AccountSeed.class, CryptoNetAccount.class}, version = 2)
@Database(entities = {AccountSeed.class/*, CryptoNetAccount.class*/}, version = 2)
public abstract class CrystalDatabase extends RoomDatabase {
private static CrystalDatabase instance;
public abstract AccountSeedDao accountSeedDao();
public abstract CryptoNetAccountDao cryptoNetAccountDao();
public static CrystalDatabase getAppDatabase(Context context) {
if (instance == null) {
instance =
Room.databaseBuilder(context,
CrystalDatabase.class, "CrystalWallet.db")
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.build();
}
return instance;
}
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override

View file

@ -1,27 +0,0 @@
package cy.agorise.crystalwallet.dao;
import android.arch.persistence.room.Room;
import android.content.Context;
/**
* Created by Henry Varona on 6/9/2017.
*/
public class DatabaseConnection {
private static final DatabaseConnection instance = new DatabaseConnection();
private CrystalDatabase db;
private DatabaseConnection(){
}
public static CrystalDatabase getConnection(Context context){
if (instance.db == null){
instance.db = Room.databaseBuilder(context,
CrystalDatabase.class, "CrystalWallet.db")
.addMigrations(CrystalDatabase.MIGRATION_1_2)
.build();
}
return instance.db;
}
}

View file

@ -5,7 +5,7 @@ import android.arch.persistence.room.*;
/**
* Created by Henry Varona on 6/9/2017.
*/
@Entity
@Entity(tableName = "account_seed")
public class AccountSeed {
@PrimaryKey(autoGenerate = true)

View file

@ -6,7 +6,9 @@ import android.arch.persistence.room.*;
* Created by Henry Varona on 6/9/2017.
*/
@Entity (foreignKeys = @ForeignKey(entity = AccountSeed.class,
@Entity (tableName = "crypto_net_account",
indices = {@Index("id"),@Index("seed_id")},
foreignKeys = @ForeignKey(entity = AccountSeed.class,
parentColumns = "id",
childColumns = "seed_id"))
public class CryptoNetAccount {

View file

@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}