music: unify indexer and repository

Unify Indexer and MusicRepository into a single class.

This is meant to create a single dependency on PlaylistDatabase and
reduce the amount of orchestration.
This commit is contained in:
Alexander Capehart
2023-03-20 15:57:27 -06:00
parent 4033a791a7
commit 9a282e2be9
18 changed files with 689 additions and 852 deletions
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2023 Auxio Project
* FakeMusicRepository.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music
import kotlinx.coroutines.Job
import org.oxycblt.auxio.music.library.Library
open class FakeMusicRepository : MusicRepository {
override var indexingState: IndexingState?
get() = throw NotImplementedError()
set(_) {
throw NotImplementedError()
}
override var library: Library?
get() = throw NotImplementedError()
set(_) {
throw NotImplementedError()
}
override var playlists: List<Playlist>?
get() = throw NotImplementedError()
set(_) {
throw NotImplementedError()
}
override fun addUpdateListener(listener: MusicRepository.UpdateListener) {
throw NotImplementedError()
}
override fun removeUpdateListener(listener: MusicRepository.UpdateListener) {
throw NotImplementedError()
}
override fun addIndexingListener(listener: MusicRepository.IndexingListener) {
throw NotImplementedError()
}
override fun removeIndexingListener(listener: MusicRepository.IndexingListener) {
throw NotImplementedError()
}
override fun registerWorker(worker: MusicRepository.IndexingWorker) {
throw NotImplementedError()
}
override fun unregisterWorker(worker: MusicRepository.IndexingWorker) {
throw NotImplementedError()
}
override fun requestIndex(withCache: Boolean) {
throw NotImplementedError()
}
override fun index(worker: MusicRepository.IndexingWorker, withCache: Boolean): Job {
throw NotImplementedError()
}
}
@@ -1,53 +0,0 @@
/*
* Copyright (c) 2023 Auxio Project
* MusicRepositoryTest.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music
import org.junit.Assert.assertEquals
import org.junit.Test
import org.oxycblt.auxio.music.library.FakeLibrary
import org.oxycblt.auxio.music.library.Library
class MusicRepositoryTest {
@Test
fun listeners() {
val listener = TestListener()
val impl =
MusicRepositoryImpl().apply {
library = null
addListener(listener)
}
impl.library = TestLibrary(0)
assertEquals(listOf(null, TestLibrary(0)), listener.updates)
val listener2 = TestListener()
impl.addListener(listener2)
impl.library = TestLibrary(1)
assertEquals(listOf(TestLibrary(0), TestLibrary(1)), listener2.updates)
}
private class TestListener : MusicRepository.Listener {
val updates = mutableListOf<Library?>()
override fun onLibraryChanged(library: Library?) {
updates.add(library)
}
}
private data class TestLibrary(private val id: Int) : FakeLibrary()
}
@@ -22,31 +22,34 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.oxycblt.auxio.music.library.FakeLibrary
import org.oxycblt.auxio.music.system.FakeIndexer
import org.oxycblt.auxio.music.system.Indexer
import org.oxycblt.auxio.music.library.Library
import org.oxycblt.auxio.util.forceClear
class MusicViewModelTest {
@Test
fun indexerState() {
val indexer =
TestIndexer().apply { state = Indexer.State.Indexing(Indexer.Indexing.Indeterminate) }
TestMusicRepository().apply {
indexingState = IndexingState.Indexing(IndexingProgress.Indeterminate)
}
val musicViewModel = MusicViewModel(indexer)
assertTrue(indexer.listener is MusicViewModel)
assertTrue(indexer.updateListener is MusicViewModel)
assertTrue(indexer.indexingListener is MusicViewModel)
assertEquals(
Indexer.Indexing.Indeterminate,
(musicViewModel.indexerState.value as Indexer.State.Indexing).indexing)
indexer.state = null
assertEquals(null, musicViewModel.indexerState.value)
IndexingProgress.Indeterminate,
(musicViewModel.indexingState.value as IndexingState.Indexing).progress)
indexer.indexingState = null
assertEquals(null, musicViewModel.indexingState.value)
musicViewModel.forceClear()
assertTrue(indexer.listener == null)
assertTrue(indexer.indexingListener == null)
}
@Test
fun statistics() {
val indexer =
TestIndexer().apply { state = Indexer.State.Complete(Result.success(TestLibrary())) }
val musicViewModel = MusicViewModel(indexer)
val musicRepository = TestMusicRepository()
val musicViewModel = MusicViewModel(musicRepository)
assertEquals(null, musicViewModel.statistics.value)
musicRepository.library = TestLibrary()
assertEquals(
MusicViewModel.Statistics(
2,
@@ -60,33 +63,49 @@ class MusicViewModelTest {
@Test
fun requests() {
val indexer = TestIndexer()
val indexer = TestMusicRepository()
val musicViewModel = MusicViewModel(indexer)
musicViewModel.refresh()
musicViewModel.rescan()
assertEquals(listOf(true, false), indexer.requests)
}
private class TestIndexer : FakeIndexer() {
var listener: Indexer.Listener? = null
var state: Indexer.State? = null
private class TestMusicRepository : FakeMusicRepository() {
override var library: Library? = null
set(value) {
field = value
listener?.onIndexerStateChanged(value)
updateListener?.onMusicChanges(
MusicRepository.Changes(library = true, playlists = false))
}
override var indexingState: IndexingState? = null
set(value) {
field = value
indexingListener?.onIndexingStateChanged()
}
var updateListener: MusicRepository.UpdateListener? = null
var indexingListener: MusicRepository.IndexingListener? = null
val requests = mutableListOf<Boolean>()
override fun registerListener(listener: Indexer.Listener) {
this.listener = listener
listener.onIndexerStateChanged(state)
override fun addUpdateListener(listener: MusicRepository.UpdateListener) {
listener.onMusicChanges(MusicRepository.Changes(library = true, playlists = false))
this.updateListener = listener
}
override fun unregisterListener(listener: Indexer.Listener) {
this.listener = null
override fun removeUpdateListener(listener: MusicRepository.UpdateListener) {
this.updateListener = null
}
override fun requestReindex(withCache: Boolean) {
override fun addIndexingListener(listener: MusicRepository.IndexingListener) {
listener.onIndexingStateChanged()
this.indexingListener = listener
}
override fun removeIndexingListener(listener: MusicRepository.IndexingListener) {
this.indexingListener = null
}
override fun requestIndex(withCache: Boolean) {
requests.add(withCache)
}
}
@@ -1,58 +0,0 @@
/*
* Copyright (c) 2023 Auxio Project
* FakeIndexer.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.system
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
open class FakeIndexer : Indexer {
override val isIndeterminate: Boolean
get() = throw NotImplementedError()
override val isIndexing: Boolean
get() = throw NotImplementedError()
override fun index(context: Context, withCache: Boolean, scope: CoroutineScope): Job {
throw NotImplementedError()
}
override fun registerController(controller: Indexer.Controller) {
throw NotImplementedError()
}
override fun unregisterController(controller: Indexer.Controller) {
throw NotImplementedError()
}
override fun registerListener(listener: Indexer.Listener) {
throw NotImplementedError()
}
override fun unregisterListener(listener: Indexer.Listener) {
throw NotImplementedError()
}
override fun requestReindex(withCache: Boolean) {
throw NotImplementedError()
}
override fun reset() {
throw NotImplementedError()
}
}