Skip to content

Commit

Permalink
- bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaflowski committed Oct 18, 2024
1 parent f687ea9 commit 6d27103
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "kaf.audiobookshelfwearos"
minSdk = 26
targetSdk = 33
versionCode = 11
versionName = "1.8"
versionCode = 13
versionName = "1.9"
vectorDrawables {
useSupportLibrary = true
}
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/kaf/audiobookshelfwearos/app/ApiHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kaf.audiobookshelfwearos.app.data.UserMediaProgress
import kaf.audiobookshelfwearos.app.userdata.UserDataManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
Expand Down Expand Up @@ -92,6 +91,7 @@ class ApiHandler(private val context: Context) {
suspend fun getItem(id: String): LibraryItem? {
return withContext(Dispatchers.IO) {
val request = getRequest("/api/items/$id?expanded=1&include=progress")
Timber.d("request = "+request)
try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
Expand All @@ -102,15 +102,16 @@ class ApiHandler(private val context: Context) {
val libraryItem = jacksonMapper.readValue<LibraryItem>(responseBody.toString())
val localLibraryItem = db.libraryItemDao().getLibraryItemById(id)
localLibraryItem?.let {
if (it.userMediaProgress.lastUpdate > libraryItem.userMediaProgress.lastUpdate) libraryItem.userMediaProgress =
localLibraryItem.userMediaProgress
if (it.userProgress.lastUpdate > libraryItem.userProgress.lastUpdate) libraryItem.userProgress =
localLibraryItem.userProgress
}

return@use libraryItem
}
} catch (e: SocketTimeoutException) {
return@withContext null
} catch (e: Exception) {
e.printStackTrace()
FirebaseCrashlytics.getInstance().log("Handled item error")
FirebaseCrashlytics.getInstance().recordException(e)
showToast(e.message.toString())
Expand Down Expand Up @@ -150,7 +151,7 @@ class ApiHandler(private val context: Context) {
val serverItem = getItem(userMediaProgress.libraryItemId)

serverItem?.let {
if (serverItem.userMediaProgress.lastUpdate > userMediaProgress.lastUpdate) {
if (serverItem.userProgress.lastUpdate > userMediaProgress.lastUpdate) {
Timber.d("Progress on server is more recent. Not uploading")
userMediaProgress.toUpload = false
insertLibraryItemToDB(userMediaProgress)
Expand Down Expand Up @@ -200,7 +201,7 @@ class ApiHandler(private val context: Context) {

private suspend fun insertLibraryItemToDB(userMediaProgress: UserMediaProgress) {
db.libraryItemDao().getLibraryItemById(userMediaProgress.libraryItemId)?.let {
it.userMediaProgress = userMediaProgress
it.userProgress = userMediaProgress
db.libraryItemDao().insertLibraryItem(it)
}
}
Expand Down Expand Up @@ -241,8 +242,6 @@ class ApiHandler(private val context: Context) {
return@withContext User()
}
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().log("Handled login error")
FirebaseCrashlytics.getInstance().recordException(e)
e.printStackTrace()
e.message?.let { showToast(it) }
return@withContext User()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/kaf/audiobookshelfwearos/app/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MainApp : Application() {
database = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "library-item-db"
).build()
).fallbackToDestructiveMigration().build()
}

internal class LineNumberDebugTree : Timber.DebugTree()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ChapterListActivity : ComponentActivity() {

itemsIndexed(media.chapters) { index, _ ->
Chapter(this@run, media.chapters[index])
if (index != media.chapters.size - 1) {
if (index != media.chapters.size) {
Divider()
}
}
Expand Down Expand Up @@ -237,8 +237,8 @@ class ChapterListActivity : ComponentActivity() {
viewModel.sync(libraryItem)
}) {
Icon(
tint = if (!libraryItem.userMediaProgress.toUpload || isSyncing) Color.Gray else Color.Yellow,
imageVector = if (isSyncing) Icons.Outlined.CloudSync else if (libraryItem.userMediaProgress.toUpload) Icons.Outlined.CloudUpload else Icons.Filled.Done,
tint = if (!libraryItem.userProgress.toUpload || isSyncing) Color.Gray else Color.Yellow,
imageVector = if (isSyncing) Icons.Outlined.CloudSync else if (libraryItem.userProgress.toUpload) Icons.Outlined.CloudUpload else Icons.Filled.Done,
contentDescription = "Sync"
)
}
Expand All @@ -259,7 +259,7 @@ class ChapterListActivity : ComponentActivity() {

@Composable
fun PlayButton(item: LibraryItem) {
val buttonText = if (item.userMediaProgress.currentTime > 10) "Continue" else "Start"
val buttonText = if (item.userProgress.currentTime > 10) "Continue" else "Start"

Button(
onClick = {
Expand Down Expand Up @@ -385,8 +385,8 @@ class ChapterListActivity : ComponentActivity() {
Text(
text = timeToString(track.start),
fontSize = 10.sp,
color = if (track.start > audiobook.userMediaProgress.currentTime) Color.Green else
if (track.end > audiobook.userMediaProgress.currentTime) Color.Cyan else Color.Gray,
color = if (track.start > audiobook.userProgress.currentTime) Color.Green else
if (track.end > audiobook.userProgress.currentTime) Color.Cyan else Color.Gray,
textAlign = TextAlign.Center,
modifier = Modifier
.padding(start = 10.dp, end = 10.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package kaf.audiobookshelfwearos.app.data

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

Expand Down Expand Up @@ -31,10 +29,16 @@ data class LibraryItem(
val size: Long = 0,
val collapsedSeries: CollapsedSeries = CollapsedSeries(),
@Embedded(prefix = "progress_")
var userMediaProgress: UserMediaProgress = UserMediaProgress(),
var userMediaProgress: UserMediaProgress? = null,
val userMediaProgressId: String? = null,
val progressLastUpdate: Long = 0
) {
var userProgress: UserMediaProgress
get() = userMediaProgress ?: UserMediaProgress()
set(value) {
userMediaProgress = value
}

var title: String
get() = media.metadata.title ?: "Unknown Title"
set(value) {
Expand All @@ -44,8 +48,6 @@ data class LibraryItem(
var author: String
get() = media.metadata.authorName ?: "Unknown Author"
set(value) {
if (value != null) {
media.metadata.authorName = value
}
media.metadata.authorName = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kaf.audiobookshelfwearos.app.data.LibraryItem
import kaf.audiobookshelfwearos.app.data.UserMediaProgress
import kaf.audiobookshelfwearos.app.data.room.dao.LibraryItemDao

@Database(entities = [LibraryItem::class, UserMediaProgress::class], version = 1)
@Database(entities = [LibraryItem::class, UserMediaProgress::class], version = 2)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun libraryItemDao(): LibraryItemDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface LibraryItemDao {

suspend fun insertLibraryItem(libraryItem: LibraryItem) {
val existingItem = getLibraryItemById(libraryItem.id)
if (existingItem == null || existingItem.userMediaProgress.lastUpdate <= libraryItem.userMediaProgress.lastUpdate) {
if (existingItem == null || existingItem.userProgress.lastUpdate <= libraryItem.userProgress.lastUpdate) {
insertLibraryItemInternal(libraryItem)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,20 @@ class PlayerService : MediaSessionService() {

private fun saveProgress() {
Timber.d("getCurrentTotalPositionInS " + getCurrentTotalPositionInS())
Timber.d("progress " + audiobook.userMediaProgress.progress)
Timber.d("duration " + audiobook.userMediaProgress.duration)
Timber.d("episodeId " + audiobook.userMediaProgress.episodeId)
Timber.d("id " + audiobook.userMediaProgress.id)
Timber.d("currentTime " + audiobook.userMediaProgress.currentTime)
Timber.d("lastUpdate " + audiobook.userMediaProgress.lastUpdate)

audiobook.userMediaProgress.lastUpdate = System.currentTimeMillis()
audiobook.userMediaProgress.currentTime = getCurrentTotalPositionInS()
audiobook.userMediaProgress.toUpload = true
audiobook.userMediaProgress.libraryItemId = audiobook.id
Timber.d("progress " + audiobook.userProgress.progress)
Timber.d("duration " + audiobook.userProgress.duration)
Timber.d("episodeId " + audiobook.userProgress.episodeId)
Timber.d("id " + audiobook.userProgress.id)
Timber.d("currentTime " + audiobook.userProgress.currentTime)
Timber.d("lastUpdate " + audiobook.userProgress.lastUpdate)

audiobook.userProgress.lastUpdate = System.currentTimeMillis()
audiobook.userProgress.currentTime = getCurrentTotalPositionInS()
audiobook.userProgress.toUpload = true
audiobook.userProgress.libraryItemId = audiobook.id
scope.launch(Dispatchers.IO) {
db.libraryItemDao().insertLibraryItem(audiobook)
ApiHandler(this@PlayerService).updateProgress(audiobook.userMediaProgress)
ApiHandler(this@PlayerService).updateProgress(audiobook.userProgress)
}
}

Expand Down Expand Up @@ -421,7 +421,7 @@ class PlayerService : MediaSessionService() {
withContext(Dispatchers.Main) {
var time = intent.getDoubleExtra("time", -1.0)
if (time < 0)
time = it.userMediaProgress.currentTime
time = it.userProgress.currentTime

if (intent.getStringExtra("action").equals("continue")) {
if (audiobook.id.equals(id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import java.net.SocketTimeoutException

class ApiViewModel(private val apiHandler: ApiHandler) : ViewModel() {
private val _loginResult = MutableLiveData<User>()
Expand Down Expand Up @@ -81,12 +80,12 @@ class ApiViewModel(private val apiHandler: ApiHandler) : ViewModel() {

val item = apiHandler.getItem(itemId)
item?.let {
if (libraryItem == null || libraryItem.userMediaProgress.lastUpdate <= item.userMediaProgress.lastUpdate) {
if (libraryItem == null || libraryItem.userProgress.lastUpdate <= item.userProgress.lastUpdate) {
Timber.d("Post server version")
libraryItem?.let {
if (libraryItem.userMediaProgress.lastUpdate >= item.userMediaProgress.lastUpdate) {
if (libraryItem.userProgress.lastUpdate >= item.userProgress.lastUpdate) {
Timber.d("Local progress is newer or the same")
item.userMediaProgress = libraryItem.userMediaProgress
item.userProgress = libraryItem.userProgress
}
}
_isLoading.value = false
Expand All @@ -100,8 +99,8 @@ class ApiViewModel(private val apiHandler: ApiHandler) : ViewModel() {
_isSyncing.value = true
viewModelScope.launch {
val newItem =
item.copy(userMediaProgress = item.userMediaProgress.copy(toUpload = !item.userMediaProgress.toUpload))
val updated = apiHandler.updateProgress(newItem.userMediaProgress)
item.copy(userMediaProgress = item.userProgress.copy(toUpload = !item.userProgress.toUpload))
val updated = apiHandler.updateProgress(newItem.userProgress)
if (updated) {
_item.postValue(newItem)
}
Expand Down

0 comments on commit 6d27103

Please sign in to comment.