-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback destructive migration. Up version to 2.4.0
- Loading branch information
Showing
4 changed files
with
187 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
app/src/main/java/com/garnegsoft/hubs/api/article/offline/OfflineArticle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package com.garnegsoft.hubs.api.article.offline | ||
|
||
import android.content.Context | ||
import androidx.room.* | ||
import androidx.room.migration.Migration | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
|
||
private const val SNIPPETS_TABLE_NAME = "offline_articles_snippets" | ||
private const val ARTICLES_TABLE_NAME = "offline_articles" | ||
private const val DATABASE_NAME = "offline_db" | ||
|
||
@Entity( | ||
tableName = SNIPPETS_TABLE_NAME, | ||
indices = [Index("article_id", unique = true)] | ||
) | ||
data class OfflineArticleSnippet( | ||
|
||
@ColumnInfo("article_id") | ||
val articleId: Int, | ||
|
||
@ColumnInfo("author_name") | ||
val authorName: String?, | ||
|
||
@ColumnInfo("author_avatar_url") | ||
val authorAvatarUrl: String?, | ||
|
||
@ColumnInfo("time_published") | ||
val timePublished: String, | ||
|
||
val title: String, | ||
|
||
@ColumnInfo("reading_time") | ||
val readingTime: Int, | ||
|
||
@ColumnInfo("is_translation") | ||
val isTranslation: Boolean, | ||
|
||
@ColumnInfo("text_snippet") | ||
val textSnippet: String, | ||
|
||
@ColumnInfo("thumbnail_url") | ||
val thumbnailUrl: String?, | ||
|
||
@TypeConverters(HubsConverter::class) | ||
val hubs: HubsList, | ||
|
||
@ColumnInfo | ||
@PrimaryKey(autoGenerate = true) | ||
val id: Int = 0, | ||
) | ||
|
||
@Entity( | ||
tableName = ARTICLES_TABLE_NAME, | ||
) | ||
data class OfflineArticle( | ||
@ColumnInfo("article_id", index = true) | ||
val articleId: Int, | ||
|
||
@ColumnInfo("author_name") | ||
val authorName: String?, | ||
|
||
@ColumnInfo("author_avatar_url") | ||
val authorAvatarUrl: String?, | ||
|
||
@ColumnInfo("time_published") | ||
val timePublished: String, | ||
|
||
val title: String, | ||
|
||
@ColumnInfo("reading_time") | ||
val readingTime: Int, | ||
|
||
@ColumnInfo("is_translation") | ||
val isTranslation: Boolean, | ||
|
||
|
||
@ColumnInfo("content_html") | ||
val contentHtml: String, | ||
|
||
@TypeConverters(HubsConverter::class) | ||
val hubs: HubsList, | ||
|
||
@PrimaryKey(autoGenerate = true) | ||
val id: Int = 0, | ||
|
||
) | ||
|
||
@ProvidedTypeConverter | ||
class HubsConverter { | ||
|
||
@TypeConverter | ||
fun fromHubs(hubs: HubsList): String { | ||
return hubs.hubsList.joinToString(",") | ||
} | ||
|
||
@TypeConverter | ||
fun fromString(hubs: String): HubsList { | ||
return HubsList(hubs.split(",")) | ||
} | ||
|
||
|
||
} | ||
|
||
class HubsList( | ||
val hubsList: List<String> | ||
) | ||
|
||
val Context.offlineArticlesDatabase: OfflineArticlesDatabase | ||
get() = OfflineArticlesDatabase.getDb(this) | ||
|
||
|
||
@Dao | ||
interface OfflineArticlesDao { | ||
|
||
/** | ||
* @return article entity by **article_id**, not by room table id | ||
*/ | ||
@Query("SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId") | ||
fun getArticleById(articleId: Int): OfflineArticle | ||
|
||
@Query("SELECT EXISTS (SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId)") | ||
fun exists(articleId: Int): Boolean | ||
|
||
@Query("SELECT EXISTS (SELECT * FROM $ARTICLES_TABLE_NAME WHERE article_id = :articleId)") | ||
fun existsFlow(articleId: Int): Flow<Boolean> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insertSnippet(entity: OfflineArticleSnippet) | ||
|
||
@Delete | ||
fun deleteSnippet(entity: OfflineArticleSnippet) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insert(entity: OfflineArticle) | ||
|
||
@Query("DELETE FROM $ARTICLES_TABLE_NAME WHERE article_id = :id") | ||
fun delete(id: Int) | ||
|
||
@Query("DELETE FROM $SNIPPETS_TABLE_NAME WHERE article_id = :id") | ||
fun deleteSnippet(id: Int) | ||
|
||
/** | ||
* @return list of article entities from older to newer | ||
*/ | ||
@Query("SELECT * FROM $SNIPPETS_TABLE_NAME ORDER BY id ASC") | ||
fun getAllSnippetsSortedByIdAsc(): Flow<List<OfflineArticleSnippet>> | ||
|
||
/** | ||
* @return list of article entities from newer to older | ||
*/ | ||
@Query("SELECT * FROM $SNIPPETS_TABLE_NAME ORDER BY id DESC") | ||
fun getAllSnippetsSortedByIdDesc(): Flow<List<OfflineArticleSnippet>> | ||
|
||
} | ||
|
||
@TypeConverters(HubsConverter::class) | ||
@Database( | ||
entities = [OfflineArticleSnippet::class, OfflineArticle::class], | ||
version = 2 | ||
) | ||
abstract class OfflineArticlesDatabase : RoomDatabase() { | ||
|
||
abstract fun articlesDao(): OfflineArticlesDao | ||
|
||
companion object { | ||
@Volatile | ||
private var instance: OfflineArticlesDatabase? = null | ||
fun getDb(context: Context): OfflineArticlesDatabase { | ||
return instance ?: synchronized(this) { | ||
val inst = Room.databaseBuilder( | ||
context = context, | ||
klass = OfflineArticlesDatabase::class.java, | ||
name = DATABASE_NAME | ||
) | ||
.addTypeConverter(HubsConverter()) | ||
.fallbackToDestructiveMigration() | ||
.build() | ||
instance = inst | ||
inst | ||
} | ||
} | ||
} | ||
} |
179 changes: 0 additions & 179 deletions
179
app/src/main/java/com/garnegsoft/hubs/api/article/offline/OfflineArticleSnippet.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters