Add Room base classes

This commit is contained in:
2019-09-12 16:58:01 +02:00
committed by Matte23
parent c0e69e658a
commit 57ecdd8062
5 changed files with 133 additions and 0 deletions

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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 }
}
}
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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
)

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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<List<Circular>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(circulars: List<Circular>)
}

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.server
import com.google.gson.Gson