1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
* Copyright 2019 Allan Wang
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.pitchedapps.frost.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.pitchedapps.frost.BuildConfig
import org.koin.dsl.module
interface FrostPrivateDao {
fun cookieDao(): CookieDao
fun notifDao(): NotificationDao
fun cacheDao(): CacheDao
}
@Database(
entities = [CookieEntity::class, NotificationEntity::class, CacheEntity::class],
version = 2,
exportSchema = true
)
abstract class FrostPrivateDatabase : RoomDatabase(), FrostPrivateDao {
companion object {
const val DATABASE_NAME = "frost-priv-db"
}
}
interface FrostPublicDao {
fun genericDao(): GenericDao
}
@Database(entities = [GenericEntity::class], version = 1, exportSchema = true)
abstract class FrostPublicDatabase : RoomDatabase(), FrostPublicDao {
companion object {
const val DATABASE_NAME = "frost-db"
}
}
interface FrostDao : FrostPrivateDao, FrostPublicDao {
fun close()
}
/**
* Composition of all database interfaces
*/
class FrostDatabase(
private val privateDb: FrostPrivateDatabase,
private val publicDb: FrostPublicDatabase
) :
FrostDao,
FrostPrivateDao by privateDb,
FrostPublicDao by publicDb {
override fun close() {
privateDb.close()
publicDb.close()
}
companion object {
private fun <T : RoomDatabase> RoomDatabase.Builder<T>.frostBuild() =
if (BuildConfig.DEBUG) {
fallbackToDestructiveMigration().build()
} else {
build()
}
fun create(context: Context): FrostDatabase {
val privateDb = Room.databaseBuilder(
context, FrostPrivateDatabase::class.java,
FrostPrivateDatabase.DATABASE_NAME
).addMigrations(COOKIES_MIGRATION_1_2).frostBuild()
val publicDb = Room.databaseBuilder(
context, FrostPublicDatabase::class.java,
FrostPublicDatabase.DATABASE_NAME
).frostBuild()
return FrostDatabase(privateDb, publicDb)
}
fun module() = module {
single { create(get()) }
single { get<FrostDatabase>().cookieDao() }
single { get<FrostDatabase>().cacheDao() }
single { get<FrostDatabase>().notifDao() }
single { get<FrostDatabase>().genericDao() }
}
}
}
|