Skip to content

Commit

Permalink
feat: Create cache for readable content
Browse files Browse the repository at this point in the history
  • Loading branch information
DesarrolloAntonio committed Jun 6, 2024
1 parent 0fa2464 commit 9799c21
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 20 deletions.
2 changes: 0 additions & 2 deletions data/src/main/java/com/desarrollodroide/data/di/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ fun dataModule() = module {
val protoDataStoreQualifier = named("protoDataStore")
val protoRememberUserDataStoreQualifier = named("protoRememberUserDataStore")



single(preferencesDataStoreQualifier) {
PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fun databaseModule() = module {
single { BookmarksDatabase.create(androidContext()) }
single { get<BookmarksDatabase>().bookmarksDao() }
single { get<BookmarksDatabase>().tagDao() }
single { get<BookmarksDatabase>().bookmarkHtmlDao() }

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.desarrollodroide.data.local.room.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.desarrollodroide.data.local.room.entity.BookmarkHtmlEntity

@Dao
interface BookmarkHtmlDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrUpdate(bookmarkHtml: BookmarkHtmlEntity)

@Query("SELECT readableContentHtml FROM bookmark_html WHERE id = :bookmarkId")
suspend fun getHtmlContent(bookmarkId: Int): String?

@Query("SELECT * FROM bookmark_html WHERE id = :bookmarkId")
suspend fun getBookmarkHtml(bookmarkId: Int): BookmarkHtmlEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import com.desarrollodroide.data.local.room.dao.BookmarksDao
import com.desarrollodroide.data.local.room.entity.BookmarkEntity
import com.desarrollodroide.data.local.room.converters.TagsConverter
import com.desarrollodroide.data.local.room.dao.BookmarkHtmlDao
import com.desarrollodroide.data.local.room.dao.TagDao
import com.desarrollodroide.data.local.room.entity.BookmarkHtmlEntity
import com.desarrollodroide.data.local.room.entity.TagEntity

@Database(entities = [BookmarkEntity::class, TagEntity::class], version = 4)
@Database(entities = [BookmarkEntity::class, TagEntity::class, BookmarkHtmlEntity::class], version = 5)
@TypeConverters(TagsConverter::class)
abstract class BookmarksDatabase : RoomDatabase() {

abstract fun bookmarksDao(): BookmarksDao
abstract fun tagDao(): TagDao
abstract fun bookmarkHtmlDao(): BookmarkHtmlDao

companion object {

Expand Down Expand Up @@ -48,13 +51,27 @@ abstract class BookmarksDatabase : RoomDatabase() {
}
}

val MIGRATION_4_5: Migration = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `bookmark_html` (
`id` INTEGER PRIMARY KEY NOT NULL,
`url` TEXT NOT NULL,
`readableContentHtml` TEXT NOT NULL
)
"""
)
}
}

fun create(context: Context): BookmarksDatabase {
return Room.databaseBuilder(
context,
BookmarksDatabase::class.java, "bookmarks_database"
)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
.build()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ data class BookmarkEntity(
@ColumnInfo(name = "create_archive")
val createArchive: Boolean,
@ColumnInfo(name = "create_ebook")
val createEbook: Boolean
val createEbook: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.desarrollodroide.data.local.room.entity

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "bookmark_html")
data class BookmarkHtmlEntity(
@PrimaryKey
val id: Int,
val url: String,
val readableContentHtml: String
)
6 changes: 3 additions & 3 deletions data/src/main/java/com/desarrollodroide/data/mapper/Mapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun BookmarkDTO.toDomainModel(serverUrl: String = "") = Bookmark(
hasEbook = hasEbook?:false,
tags = tags?.map { it.toDomainModel() }?: emptyList(),
createArchive = createArchive?:false,
createEbook = createEbook?:false
createEbook = createEbook?:false,
)

fun BookmarksDTO.toDomainModel(serverUrl: String) = Bookmarks(
Expand Down Expand Up @@ -93,7 +93,7 @@ fun BookmarkDTO.toEntityModel() = BookmarkEntity(
hasEbook = hasEbook?:false,
tags = tags?.map { it.toDomainModel() } ?: emptyList(),
createArchive = createArchive?:false,
createEbook = createEbook?:false
createEbook = createEbook?:false,
)

fun BookmarkEntity.toDomainModel() = Bookmark(
Expand All @@ -110,7 +110,7 @@ fun BookmarkEntity.toDomainModel() = Bookmark(
hasEbook = hasEbook,
tags = tags,
createArchive = createArchive,
createEbook = createEbook
createEbook = createEbook,
)

fun UpdateCachePayload.toDTO() = UpdateCachePayloadDTO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ fun presenterModule() = module {
ReadableContentViewModel(
getBookmarkReadableContentUseCase = get(),
settingsPreferenceDataSource = get(),
bookmarksDao = get(),
bookmarkHtmlDao = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun ReadableContentScreen(

LaunchedEffect(Unit) {
readableContentViewModel.loadInitialData()
readableContentViewModel.getBookmarkReadableContent(bookmarkId)
readableContentViewModel.getBookmarkReadableContent(bookmarkId = bookmarkId, bookmarkUrl = bookmarkUrl)
}

val themeMode by readableContentViewModel.themeMode.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import androidx.lifecycle.viewModelScope
import com.desarrollodroide.common.result.Result
import com.desarrollodroide.data.helpers.ThemeMode
import com.desarrollodroide.data.local.preferences.SettingsPreferenceDataSource
import com.desarrollodroide.data.local.room.dao.BookmarkHtmlDao
import com.desarrollodroide.data.local.room.dao.BookmarksDao
import com.desarrollodroide.data.local.room.entity.BookmarkHtmlEntity
import com.desarrollodroide.domain.usecase.GetBookmarkReadableContentUseCase
import com.desarrollodroide.model.ReadableMessage
import com.desarrollodroide.pagekeeper.ui.components.UiState
import com.desarrollodroide.pagekeeper.ui.components.error
import com.desarrollodroide.pagekeeper.ui.components.isLoading
import com.desarrollodroide.pagekeeper.ui.components.success
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -18,7 +22,9 @@ import kotlinx.coroutines.launch

class ReadableContentViewModel(
private val settingsPreferenceDataSource: SettingsPreferenceDataSource,
private val getBookmarkReadableContentUseCase: GetBookmarkReadableContentUseCase
private val getBookmarkReadableContentUseCase: GetBookmarkReadableContentUseCase,
private val bookmarksDao: BookmarksDao,
private val bookmarkHtmlDao: BookmarkHtmlDao
) : ViewModel() {

private var serverUrl = ""
Expand All @@ -29,7 +35,6 @@ class ReadableContentViewModel(

val themeMode = MutableStateFlow<ThemeMode>(ThemeMode.AUTO)


fun loadInitialData() {
viewModelScope.launch {
serverUrl = settingsPreferenceDataSource.getUrl()
Expand All @@ -39,7 +44,8 @@ class ReadableContentViewModel(
}

fun getBookmarkReadableContent(
bookmarkId: Int
bookmarkId: Int,
bookmarkUrl: String,
) {
viewModelScope.launch {
getBookmarkReadableContentUseCase.invoke(
Expand All @@ -51,28 +57,46 @@ class ReadableContentViewModel(
.collect() { result ->
when (result) {
is Result.Error -> {
Log.v(
"ReadableContent",
"Error getting bookmark readable content: ${result.error?.message}"
)
Log.v( "ReadableContent","Error getting bookmark readable content: ${result.error?.message}")
getLocalHtmlContent(bookmarkId)
}
is Result.Loading -> {
Log.v(
"ReadableContent",
"Loading, getting bookmark readable content..."
)
Log.v( "ReadableContent","Loading, getting bookmark readable content...")
_readableContentState.isLoading(true)
}

is Result.Success -> {
Log.v("ReadableContent", "Get bookmark readable content successfully.")
result.data?.let {
_readableContentState.success(it.message)
saveHtmlContent(
bookmarkId = bookmarkId,
url = bookmarkUrl,
html = it.message.html)
}
}
else -> {}
}
}
}
}
private fun saveHtmlContent(bookmarkId: Int, url: String, html: String) {
viewModelScope.launch {
val bookmarkHtml = BookmarkHtmlEntity(id = bookmarkId, url = url, readableContentHtml = html)
bookmarkHtmlDao.insertOrUpdate(bookmarkHtml)
}
}

private fun getLocalHtmlContent(bookmarkId: Int) {
viewModelScope.launch {
val bookmarkHtml = bookmarkHtmlDao.getBookmarkHtml(bookmarkId)
if (bookmarkHtml != null) {
val readableMessage = ReadableMessage(content = "", html = bookmarkHtml.readableContentHtml)
_readableContentState.success(readableMessage)
} else {
_readableContentState.error(errorMessage = "No local content available")
}
}
}

}

0 comments on commit 9799c21

Please sign in to comment.