Skip to content

Commit 4422b55

Browse files
committed
Migrate database logic to Kotlin
Room has been convereted into a KMP library in the latest stable releases and annotation processing requires KSP which only generates kotlin classes Signed-off-by: Aayush Gupta <[email protected]>
1 parent f3ca5f6 commit 4422b55

File tree

61 files changed

+1675
-1952
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1675
-1952
lines changed

app/schemas/org.schabi.newpipe.database.AppDatabase/9.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
"notNull": true
459459
},
460460
{
461-
"fieldPath": "name",
461+
"fieldPath": "orderingName",
462462
"columnName": "name",
463463
"affinity": "TEXT",
464464
"notNull": false

app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2017-2024 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package org.schabi.newpipe
8+
9+
import android.content.Context
10+
import androidx.room.Room.databaseBuilder
11+
import org.schabi.newpipe.database.AppDatabase
12+
import org.schabi.newpipe.database.Migrations.MIGRATION_1_2
13+
import org.schabi.newpipe.database.Migrations.MIGRATION_2_3
14+
import org.schabi.newpipe.database.Migrations.MIGRATION_3_4
15+
import org.schabi.newpipe.database.Migrations.MIGRATION_4_5
16+
import org.schabi.newpipe.database.Migrations.MIGRATION_5_6
17+
import org.schabi.newpipe.database.Migrations.MIGRATION_6_7
18+
import org.schabi.newpipe.database.Migrations.MIGRATION_7_8
19+
import org.schabi.newpipe.database.Migrations.MIGRATION_8_9
20+
import kotlin.concurrent.Volatile
21+
22+
object NewPipeDatabase {
23+
24+
@Volatile
25+
private var databaseInstance: AppDatabase? = null
26+
27+
private fun getDatabase(context: Context): AppDatabase {
28+
return databaseBuilder(
29+
context.applicationContext,
30+
AppDatabase::class.java,
31+
AppDatabase.Companion.DATABASE_NAME
32+
).addMigrations(
33+
MIGRATION_1_2,
34+
MIGRATION_2_3,
35+
MIGRATION_3_4,
36+
MIGRATION_4_5,
37+
MIGRATION_5_6,
38+
MIGRATION_6_7,
39+
MIGRATION_7_8,
40+
MIGRATION_8_9
41+
).build()
42+
}
43+
44+
@JvmStatic
45+
fun getInstance(context: Context): AppDatabase {
46+
var result = databaseInstance
47+
if (result == null) {
48+
synchronized(NewPipeDatabase::class.java) {
49+
result = databaseInstance
50+
if (result == null) {
51+
databaseInstance = getDatabase(context)
52+
result = databaseInstance
53+
}
54+
}
55+
}
56+
57+
return result!!
58+
}
59+
60+
@JvmStatic
61+
fun checkpoint() {
62+
checkNotNull(databaseInstance) { "database is not initialized" }
63+
val c = databaseInstance!!.query("pragma wal_checkpoint(full)", null)
64+
if (c.moveToFirst() && c.getInt(0) == 1) {
65+
throw RuntimeException("Checkpoint was blocked from completing")
66+
}
67+
}
68+
69+
@JvmStatic
70+
fun close() {
71+
if (databaseInstance != null) {
72+
synchronized(NewPipeDatabase::class.java) {
73+
if (databaseInstance != null) {
74+
databaseInstance!!.close()
75+
databaseInstance = null
76+
}
77+
}
78+
}
79+
}
80+
}

app/src/main/java/org/schabi/newpipe/database/AppDatabase.java

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2017-2024 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package org.schabi.newpipe.database
8+
9+
import androidx.room.Database
10+
import androidx.room.RoomDatabase
11+
import androidx.room.TypeConverters
12+
import org.schabi.newpipe.database.feed.dao.FeedDAO
13+
import org.schabi.newpipe.database.feed.dao.FeedGroupDAO
14+
import org.schabi.newpipe.database.feed.model.FeedEntity
15+
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
16+
import org.schabi.newpipe.database.feed.model.FeedGroupSubscriptionEntity
17+
import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity
18+
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO
19+
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO
20+
import org.schabi.newpipe.database.history.model.SearchHistoryEntry
21+
import org.schabi.newpipe.database.history.model.StreamHistoryEntity
22+
import org.schabi.newpipe.database.playlist.dao.PlaylistDAO
23+
import org.schabi.newpipe.database.playlist.dao.PlaylistRemoteDAO
24+
import org.schabi.newpipe.database.playlist.dao.PlaylistStreamDAO
25+
import org.schabi.newpipe.database.playlist.model.PlaylistEntity
26+
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
27+
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity
28+
import org.schabi.newpipe.database.stream.dao.StreamDAO
29+
import org.schabi.newpipe.database.stream.dao.StreamStateDAO
30+
import org.schabi.newpipe.database.stream.model.StreamEntity
31+
import org.schabi.newpipe.database.stream.model.StreamStateEntity
32+
import org.schabi.newpipe.database.subscription.SubscriptionDAO
33+
import org.schabi.newpipe.database.subscription.SubscriptionEntity
34+
35+
@TypeConverters(Converters::class)
36+
@Database(
37+
version = Migrations.DB_VER_9,
38+
entities = [
39+
SubscriptionEntity::class,
40+
SearchHistoryEntry::class,
41+
StreamEntity::class,
42+
StreamHistoryEntity::class,
43+
StreamStateEntity::class,
44+
PlaylistEntity::class,
45+
PlaylistStreamEntity::class,
46+
PlaylistRemoteEntity::class,
47+
FeedEntity::class,
48+
FeedGroupEntity::class,
49+
FeedGroupSubscriptionEntity::class,
50+
FeedLastUpdatedEntity::class
51+
]
52+
)
53+
abstract class AppDatabase : RoomDatabase() {
54+
abstract fun feedDAO(): FeedDAO
55+
abstract fun feedGroupDAO(): FeedGroupDAO
56+
abstract fun playlistDAO(): PlaylistDAO
57+
abstract fun playlistRemoteDAO(): PlaylistRemoteDAO
58+
abstract fun playlistStreamDAO(): PlaylistStreamDAO
59+
abstract fun searchHistoryDAO(): SearchHistoryDAO
60+
abstract fun streamDAO(): StreamDAO
61+
abstract fun streamHistoryDAO(): StreamHistoryDAO
62+
abstract fun streamStateDAO(): StreamStateDAO
63+
abstract fun subscriptionDAO(): SubscriptionDAO
64+
65+
companion object {
66+
const val DATABASE_NAME: String = "newpipe.db"
67+
}
68+
}

app/src/main/java/org/schabi/newpipe/database/BasicDAO.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2017-2022 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package org.schabi.newpipe.database
8+
9+
import androidx.room.Dao
10+
import androidx.room.Delete
11+
import androidx.room.Insert
12+
import androidx.room.Update
13+
import io.reactivex.rxjava3.core.Flowable
14+
15+
@Dao
16+
interface BasicDAO<Entity> {
17+
18+
/* Inserts */
19+
@Insert
20+
fun insert(entity: Entity): Long
21+
22+
@Insert
23+
fun insertAll(entities: Collection<Entity>): List<Long>
24+
25+
/* Searches */
26+
fun getAll(): Flowable<List<Entity>>
27+
28+
fun listByService(serviceId: Int): Flowable<List<Entity>>
29+
30+
/* Deletes */
31+
@Delete
32+
fun delete(entity: Entity)
33+
34+
fun deleteAll(): Int
35+
36+
/* Updates */
37+
@Update
38+
fun update(entity: Entity): Int
39+
40+
@Update
41+
fun update(entities: Collection<Entity>)
42+
}

app/src/main/java/org/schabi/newpipe/database/LocalItem.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)