-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrades! Makes it work with android SDK version 33 (#109)
* Dependency updates, build the app on API 33 (credit to @dkowis) * Collections MVP Implementation - Fetch collections during library refreshes - Add bottom nav menu entry for collections (if library has collections) - Implement a screen to show all collections - Implement very basic "collection details" screen listing the items in a collection. Not currently using library sorting! Not showing any details - Probably plenty of bugs re: weird data types, but simple implementation working * Update AGP to 8.0.0 * Re-enable silent audio, weaken sensor requirements --------- Co-authored-by: Matt Vaughn <[email protected]>
- Loading branch information
1 parent
ff318fc
commit b4dc495
Showing
63 changed files
with
1,974 additions
and
268 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ captures/ | |
bin/ | ||
gen/ | ||
target/ | ||
app/freeAsInBeer | ||
app/googlePlay | ||
app/release | ||
|
||
# android studio files | ||
.idea/ | ||
|
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
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
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
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
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
57 changes: 57 additions & 0 deletions
57
app/src/main/java/io/github/mattpvaughn/chronicle/data/local/CollectionsDatabase.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,57 @@ | ||
package io.github.mattpvaughn.chronicle.data.local | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
import io.github.mattpvaughn.chronicle.data.model.Collection | ||
|
||
private const val COLLECTIONS_DATABASE_NAME = "collections_db" | ||
|
||
private lateinit var INSTANCE: CollectionsDatabase | ||
fun getCollectionsDatabase(context: Context): CollectionsDatabase { | ||
synchronized(CollectionsDatabase::class.java) { | ||
if (!::INSTANCE.isInitialized) { | ||
INSTANCE = Room.databaseBuilder( | ||
context.applicationContext, | ||
CollectionsDatabase::class.java, | ||
COLLECTIONS_DATABASE_NAME | ||
).addMigrations().build() | ||
} | ||
} | ||
return INSTANCE | ||
} | ||
|
||
@Database(entities = [Collection::class], version = 1, exportSchema = false) | ||
abstract class CollectionsDatabase : RoomDatabase() { | ||
abstract val collectionsDao: CollectionsDao | ||
} | ||
|
||
@Dao | ||
interface CollectionsDao { | ||
@Query("SELECT * FROM Collection ORDER BY title") | ||
fun getAllRows(): LiveData<List<Collection>> | ||
|
||
@Query("SELECT * FROM Collection WHERE id = :id LIMIT 1") | ||
fun getCollection(id: Int): LiveData<Collection?> | ||
|
||
@Query("SELECT * FROM Collection WHERE :collectionId = id") | ||
suspend fun getCollectionAsync(collectionId: Int): Collection | ||
|
||
@Query("SELECT * FROM Collection") | ||
fun getCollections(): List<Collection> | ||
|
||
@Query("SELECT Count(id) FROM Collection") | ||
fun countCollections(): LiveData<Long> | ||
|
||
@Query("DELETE FROM Collection") | ||
suspend fun clear() | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insertAll(rows: List<Collection>) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun update(collection: Collection) | ||
|
||
@Query("DELETE FROM Collection WHERE id IN (:collectionsToRemove)") | ||
fun removeAll(collectionsToRemove: List<Long>): Int | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/io/github/mattpvaughn/chronicle/data/local/CollectionsRepository.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,81 @@ | ||
package io.github.mattpvaughn.chronicle.data.local | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.map | ||
import io.github.mattpvaughn.chronicle.data.model.Collection | ||
import io.github.mattpvaughn.chronicle.data.sources.plex.PlexMediaService | ||
import io.github.mattpvaughn.chronicle.data.sources.plex.PlexPrefsRepo | ||
import io.github.mattpvaughn.chronicle.data.sources.plex.model.asAudiobooks | ||
import io.github.mattpvaughn.chronicle.data.sources.plex.model.asCollections | ||
import kotlinx.coroutines.* | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class CollectionsRepository @Inject constructor( | ||
private val plexMediaService: PlexMediaService, | ||
private val prefsRepo: PrefsRepo, | ||
private val plexPrefsRepo: PlexPrefsRepo, | ||
private val collectionsDao: CollectionsDao | ||
) { | ||
|
||
// TODO: handle collections sorting! | ||
suspend fun getChildIds(collectionId: Int): List<Long> { | ||
return collectionsDao.getCollectionAsync(collectionId).childIds | ||
} | ||
|
||
fun getCollection(id: Int): LiveData<Collection?> = collectionsDao.getCollection(id) | ||
|
||
fun getAllCollections(): LiveData<List<Collection>> = collectionsDao.getAllRows() | ||
|
||
fun hasCollections(): LiveData<Boolean> = collectionsDao | ||
.countCollections() | ||
.map { it > 0 } | ||
|
||
suspend fun refreshCollectionsPaginated() { | ||
prefsRepo.lastRefreshTimeStamp = System.currentTimeMillis() | ||
val networkCollections: MutableList<Collection> = mutableListOf() | ||
withContext(Dispatchers.IO) { | ||
try { | ||
val libraryId = plexPrefsRepo.library?.id ?: return@withContext | ||
var chaptersLeft = 1L | ||
// Maximum number of pages of data we fetch. Failsafe in case of bad data from the | ||
// server since we don't want infinite loops. This limits us to a maximum 1,000,000 | ||
// collections for now | ||
val maxIterations = 5000 | ||
var i = 0 | ||
while (chaptersLeft > 0 && i < maxIterations) { | ||
val response = plexMediaService | ||
.retrieveCollectionsPaginated(libraryId, i * 100) | ||
.plexMediaContainer | ||
chaptersLeft = response.totalSize - (response.offset + response.size) | ||
networkCollections.addAll(response.asCollections()) | ||
i++ | ||
} | ||
} catch (t: Throwable) { | ||
Timber.i("Failed to retrieve books: $t") | ||
} | ||
} | ||
|
||
withContext(Dispatchers.IO) { | ||
try { | ||
val collectionsWithChildIds = networkCollections.map { | ||
val collectionItems = plexMediaService.fetchBooksInCollection(it.id) | ||
.plexMediaContainer | ||
.asAudiobooks() | ||
|
||
val childIds = collectionItems.map { book -> book.id.toLong() } | ||
it.copy(childIds = childIds) | ||
} | ||
collectionsDao.insertAll(collectionsWithChildIds) | ||
} catch (t: Throwable) { | ||
Timber.i("Failed to retrieve books: $t") | ||
} | ||
} | ||
} | ||
|
||
suspend fun clear() { | ||
collectionsDao.clear() | ||
} | ||
} |
Oops, something went wrong.