From 57ecdd8062ded0a44282d67ccd79745af1106d6a Mon Sep 17 00:00:00 2001 From: Matte23 Date: Thu, 12 Sep 2019 16:58:01 +0200 Subject: [PATCH] Add Room base classes --- app/build.gradle | 5 ++ .../underdesk/circolapp/data/AppDatabase.kt | 47 +++++++++++++++++++ .../net/underdesk/circolapp/data/Circular.kt | 29 ++++++++++++ .../underdesk/circolapp/data/CircularDao.kt | 34 ++++++++++++++ .../underdesk/circolapp/server/DataFetcher.kt | 18 +++++++ 5 files changed, 133 insertions(+) create mode 100644 app/src/main/java/net/underdesk/circolapp/data/AppDatabase.kt create mode 100644 app/src/main/java/net/underdesk/circolapp/data/Circular.kt create mode 100644 app/src/main/java/net/underdesk/circolapp/data/CircularDao.kt diff --git a/app/build.gradle b/app/build.gradle index dfdc63d..c37e69c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,6 +4,8 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' + android { compileSdkVersion 29 buildToolsVersion "29.0.2" @@ -42,6 +44,9 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0' implementation 'androidx.navigation:navigation-ui-ktx:2.1.0' + implementation "androidx.room:room-runtime:2.2.0-rc01" + kapt "androidx.room:room-compiler:2.2.0-rc01" + implementation "androidx.room:room-ktx:2.2.0-rc01" testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/java/net/underdesk/circolapp/data/AppDatabase.kt b/app/src/main/java/net/underdesk/circolapp/data/AppDatabase.kt new file mode 100644 index 0000000..4d73749 --- /dev/null +++ b/app/src/main/java/net/underdesk/circolapp/data/AppDatabase.kt @@ -0,0 +1,47 @@ +/* + * Circolapp + * Copyright (C) 2019 Matteo Schiff + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.underdesk.circolapp.data + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase + +@Database(entities = [Circular::class], version = 1, exportSchema = false) +abstract class AppDatabase : RoomDatabase() { + abstract fun circularDao(): CircularDao + + companion object { + + @Volatile + private var instance: AppDatabase? = null + + private const val DATABASE_NAME = "database" + + fun getInstance(context: Context): AppDatabase { + return instance ?: synchronized(this) { + instance ?: Room.databaseBuilder( + context, + AppDatabase::class.java, + DATABASE_NAME + ).build().also { instance = it } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/net/underdesk/circolapp/data/Circular.kt b/app/src/main/java/net/underdesk/circolapp/data/Circular.kt new file mode 100644 index 0000000..6f239ae --- /dev/null +++ b/app/src/main/java/net/underdesk/circolapp/data/Circular.kt @@ -0,0 +1,29 @@ +/* + * Circolapp + * Copyright (C) 2019 Matteo Schiff + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.underdesk.circolapp.data + +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "circulars") +data class Circular( + @PrimaryKey(autoGenerate = true) val id: Long, + val name: String, + val url: String +) \ No newline at end of file diff --git a/app/src/main/java/net/underdesk/circolapp/data/CircularDao.kt b/app/src/main/java/net/underdesk/circolapp/data/CircularDao.kt new file mode 100644 index 0000000..49c54ac --- /dev/null +++ b/app/src/main/java/net/underdesk/circolapp/data/CircularDao.kt @@ -0,0 +1,34 @@ +/* + * Circolapp + * Copyright (C) 2019 Matteo Schiff + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.underdesk.circolapp.data + +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface CircularDao { + @Query("SELECT * FROM circulars") + fun getCirculars(): LiveData> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertAll(circulars: List) +} \ No newline at end of file diff --git a/app/src/main/java/net/underdesk/circolapp/server/DataFetcher.kt b/app/src/main/java/net/underdesk/circolapp/server/DataFetcher.kt index 04432b7..8177f23 100644 --- a/app/src/main/java/net/underdesk/circolapp/server/DataFetcher.kt +++ b/app/src/main/java/net/underdesk/circolapp/server/DataFetcher.kt @@ -1,3 +1,21 @@ +/* + * Circolapp + * Copyright (C) 2019 Matteo Schiff + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package net.underdesk.circolapp.server import com.google.gson.Gson