Skip to content

Commit

Permalink
Improve shortcut update method and fix one edge case
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed Jun 5, 2024
1 parent 6c4d537 commit b14a131
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions app/src/main/java/com/starry/myne/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class MainActivity : AppCompatActivity() {
}

enableEdgeToEdge() // enable edge to edge for the activity.
setAppShortcuts() // set launcher shortcuts for the app.

setContent {
MyneTheme(settingsViewModel = settingsViewModel) {
Expand Down Expand Up @@ -86,7 +85,12 @@ class MainActivity : AppCompatActivity() {
}
}

private fun setAppShortcuts() {
override fun onPause() {
super.onPause()
updateShortcuts()
}

private fun updateShortcuts() {
val shortcutManager = getSystemService(ShortcutManager::class.java)
mainViewModel.buildDynamicShortcuts(
context = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ interface ReaderDao {
fun getAllReaderItems(): List<ReaderData>

@Query("SELECT * FROM reader_table WHERE library_item_id = :libraryItemId")
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ReaderData?>
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ReaderData>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ class ReaderDetailViewModel @Inject constructor(

var state by mutableStateOf(ReaderDetailScreenState())

val readerData: Flow<ReaderData?>?
get() = _readerData
private var _readerData: Flow<ReaderData?>? = null
var readerData: Flow<ReaderData>? = null
private set

fun loadEbookData(libraryItemId: String, networkStatus: NetworkObserver.Status) {
viewModelScope.launch(Dispatchers.IO) {
// Library item is not null as this screen is only accessible from the library.
val libraryItem = libraryDao.getItemById(libraryItemId.toInt())!!
val libraryItem = libraryDao.getItemById(libraryItemId.toInt())
// Check if library item exists.
if (libraryItem == null) {
state = state.copy(isLoading = false, error = "Library item not found.")
return@launch
}
// Get reader data if it exists.
_readerData = readerDao.getReaderDataAsFlow(libraryItemId.toInt())
readerData = readerDao.getReaderDataAsFlow(libraryItemId.toInt())
val coverImage: String? = try {
if (!libraryItem.isExternalBook
&& networkStatus == NetworkObserver.Status.Available
Expand Down

0 comments on commit b14a131

Please sign in to comment.