Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/src/main/java/org/cis_india/wsreader/MyneApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import okhttp3.OkHttpClient
import org.cis_india.wsreader.api.BookAPI
import java.io.File
import java.util.concurrent.TimeUnit

Expand All @@ -58,6 +59,9 @@ class MyneApp : Application(), ImageLoaderFactory {
lateinit var bookRepository: BookRepository
private set

lateinit var bookApi: BookAPI
private set

lateinit var bookshelf: Bookshelf
private set

Expand All @@ -81,6 +85,8 @@ class MyneApp : Application(), ImageLoaderFactory {

bookRepository = BookRepository(database.booksDao())

bookApi = BookAPI(this)

val downloadsDir = File(cacheDir, "downloads")

// Cleans the download dir.
Expand Down Expand Up @@ -108,7 +114,8 @@ class MyneApp : Application(), ImageLoaderFactory {
this@MyneApp,
readium,
bookRepository,
navigatorPreferences
navigatorPreferences,
bookApi
)
}

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/org/cis_india/wsreader/data/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class BookRepository(
mediaType: MediaType,
publication: Publication,
cover: File,
wdIdentifier: String? = null
wdIdentifier: String? = null,
bookLanguageCode: String? = null
): Long {
val book = Book(
creation = Date().time,
Expand All @@ -93,11 +94,19 @@ class BookRepository(
identifier = wdIdentifier?: publication.metadata.identifier ?: "",
mediaType = mediaType,
progression = "{}",
cover = cover.path
cover = cover.path,
languageCode = bookLanguageCode ?: "en"
)
return booksDao.insertBook(book)
}

suspend fun updateBookLanguage(bookId: Long, languageCode: String) =
booksDao.updateBookLanguage(bookId, languageCode)

fun getBookLanguage(bookId: Long): Flow<String?> {
return booksDao.getBookLanguage(bookId)
}

suspend fun deleteBook(id: Long) =
booksDao.deleteBook(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.cis_india.wsreader.data.model.Highlight

@Database(
entities = [Book::class, Bookmark::class, Highlight::class, Catalog::class],
version = 1,
version = 2,
exportSchema = false
)
@TypeConverters(
Expand All @@ -45,7 +45,9 @@ abstract class AppDatabase : RoomDatabase() {
context.applicationContext,
AppDatabase::class.java,
"database"
).build()
)
.addMigrations(MIGRATION_1_2)
.build()
INSTANCE = instance
return instance
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/cis_india/wsreader/data/db/BooksDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@ interface BooksDao {
"UPDATE " + Book.TABLE_NAME + " SET " + Book.PROGRESSION + " = :locator WHERE " + Book.ID + "= :id"
)
suspend fun saveProgression(locator: String, id: Long)

@Query("UPDATE " + Book.TABLE_NAME + " SET language_code = :languageCode WHERE " + Book.ID + " = :bookId")
suspend fun updateBookLanguage(bookId: Long, languageCode: String)

@Query("SELECT language_code FROM books WHERE id = :bookId LIMIT 1")
fun getBookLanguage(bookId: Long): Flow<String?>
}
11 changes: 11 additions & 0 deletions app/src/main/java/org/cis_india/wsreader/data/db/Migrations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cis_india.wsreader.data.db

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
// Add the new column language_code with default value
database.execSQL("ALTER TABLE books ADD COLUMN language_code TEXT NOT NULL DEFAULT 'auto'")
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/org/cis_india/wsreader/data/model/Book.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ data class Book(
val rawMediaType: String,
@ColumnInfo(name = COVER)
val cover: String,
@ColumnInfo(name = LANGUAGE_CODE)
val languageCode: String
) {

constructor(
Expand All @@ -47,6 +49,7 @@ data class Book(
progression: String? = null,
mediaType: MediaType,
cover: String,
languageCode: String,
) : this(
id = id,
creation = creation,
Expand All @@ -56,7 +59,8 @@ data class Book(
identifier = identifier,
progression = progression,
rawMediaType = mediaType.toString(),
cover = cover
cover = cover,
languageCode = languageCode
)

val url: AbsoluteUrl get() = AbsoluteUrl(href)!!
Expand All @@ -81,5 +85,6 @@ data class Book(
const val PROGRESSION = "progression"
const val MEDIA_TYPE = "media_type"
const val COVER = "cover"
const val LANGUAGE_CODE = "language_code"
}
}
16 changes: 10 additions & 6 deletions app/src/main/java/org/cis_india/wsreader/domain/Bookshelf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ class Bookshelf(

fun importPublicationFromStorage(
uri: Uri,
wdIdentifier: String? = null
wdIdentifier: String? = null,
bookLanguageCode: String? = null
) {
coroutineScope.launch {
addBookFeedback(publicationRetriever.retrieveFromStorage(uri),wdIdentifier)
addBookFeedback(publicationRetriever.retrieveFromStorage(uri),wdIdentifier, bookLanguageCode)
}
}

Expand Down Expand Up @@ -93,10 +94,11 @@ class Bookshelf(

private suspend fun addBookFeedback(
retrieverResult: Try<PublicationRetriever.Result, ImportError>,
wdIdentifier: String? = null
wdIdentifier: String? = null,
bookLanguageCode: String? = null
) {
retrieverResult
.map { addBook(it.publication.toUrl(), it.format, it.coverUrl, wdIdentifier) }
.map { addBook(it.publication.toUrl(), it.format, it.coverUrl, wdIdentifier, bookLanguageCode) }
.onSuccess { channel.send(Event.ImportPublicationSuccess) }
.onFailure { channel.send(Event.ImportPublicationError(it)) }
}
Expand All @@ -116,7 +118,8 @@ class Bookshelf(
url: AbsoluteUrl,
format: Format? = null,
coverUrl: AbsoluteUrl? = null,
wdIdentifier: String? = null
wdIdentifier: String? = null,
bookLanguageCode: String? = null
): Try<Unit, ImportError> {
val asset =
if (format == null) {
Expand Down Expand Up @@ -148,7 +151,8 @@ class Bookshelf(
asset.format.mediaType,
publication,
coverFile,
wdIdentifier
wdIdentifier,
bookLanguageCode,
)
if (id == -1L) {
coverFile.delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.readium.r2.shared.util.DebugError
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.getOrElse
import org.cis_india.wsreader.Readium
import org.cis_india.wsreader.api.BookAPI
import org.cis_india.wsreader.data.BookRepository
import org.cis_india.wsreader.domain.PublicationError
import org.cis_india.wsreader.reader.preferences.AndroidTtsPreferencesManagerFactory
Expand All @@ -46,6 +47,7 @@ class ReaderRepository(
private val readium: Readium,
private val bookRepository: BookRepository,
private val preferencesDataStore: DataStore<JetpackPreferences>,
private val bookApi: BookAPI
) {

private val coroutineQueue: CoroutineQueue =
Expand Down Expand Up @@ -73,6 +75,9 @@ class ReaderRepository(

val book = checkNotNull(bookRepository.get(bookId)) { "Cannot find book in database." }

//Update book language if not set in books entity in room db
updateBookLanguageIfAuto(bookId, book.identifier, book.languageCode)

val asset = readium.assetRetriever.retrieve(
book.url,
book.mediaType
Expand Down Expand Up @@ -124,6 +129,19 @@ class ReaderRepository(
}
}

private suspend fun updateBookLanguageIfAuto(bookId: Long, identifier: String, currentLang: String) {
if(currentLang != "auto") return

try {
val bookSet = bookApi.getBookById(identifier).getOrNull()!!
val language = bookSet.books.firstOrNull()?.languages?.firstOrNull()?:"en"

bookRepository.updateBookLanguage(bookId, language)
} catch (e: Exception) {

}
}

private suspend fun openAudio(
bookId: Long,
publication: Publication,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class ReaderViewModel(
bookRepository.deleteBookmark(id)
}


val bookLanguage: Flow<String> = bookRepository
.getBookLanguage(bookId)
.map { it ?: "en" }

// Highlights

val highlights: Flow<List<Highlight>> by lazy {
Expand Down
Loading