Browse Source
#7: Add Koin as dependency injection library See merge request funkwhale/funkwhale-android!58enhancement/speed-up-pipelines
Ryan Harg
3 years ago
37 changed files with 531 additions and 438 deletions
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
package audio.funkwhale.ffa.koin |
||||
|
||||
import android.content.Context |
||||
import audio.funkwhale.ffa.playback.CacheDataSourceFactoryProvider |
||||
import audio.funkwhale.ffa.playback.MediaSession |
||||
import audio.funkwhale.ffa.utils.AuthorizationServiceFactory |
||||
import audio.funkwhale.ffa.utils.DefaultOAuth |
||||
import audio.funkwhale.ffa.utils.OAuth |
||||
import com.google.android.exoplayer2.database.DatabaseProvider |
||||
import com.google.android.exoplayer2.database.ExoDatabaseProvider |
||||
import com.google.android.exoplayer2.offline.DefaultDownloadIndex |
||||
import com.google.android.exoplayer2.offline.DefaultDownloaderFactory |
||||
import com.google.android.exoplayer2.offline.DownloadManager |
||||
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper |
||||
import com.google.android.exoplayer2.upstream.cache.Cache |
||||
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor |
||||
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor |
||||
import com.google.android.exoplayer2.upstream.cache.SimpleCache |
||||
import com.preference.PowerPreference |
||||
import org.koin.core.qualifier.named |
||||
import org.koin.dsl.module |
||||
|
||||
fun ffaModule(context: Context) = module { |
||||
|
||||
single<OAuth> { DefaultOAuth(get()) } |
||||
|
||||
single { AuthorizationServiceFactory() } |
||||
|
||||
single { |
||||
val cacheDataSourceFactoryProvider = get<CacheDataSourceFactoryProvider>() |
||||
DownloaderConstructorHelper( |
||||
get(named("exoDownloadCache")), cacheDataSourceFactoryProvider.create(context) |
||||
).run { |
||||
DownloadManager( |
||||
context, |
||||
DefaultDownloadIndex(get(named("exoDatabase"))), |
||||
DefaultDownloaderFactory(this) |
||||
) |
||||
} |
||||
} |
||||
|
||||
single { |
||||
CacheDataSourceFactoryProvider( |
||||
get(), |
||||
get(named("exoCache")), |
||||
get(named("exoDownloadCache")) |
||||
) |
||||
} |
||||
|
||||
single<DatabaseProvider>(named("exoDatabase")) { ExoDatabaseProvider(context) } |
||||
|
||||
single<Cache>(named("exoDownloadCache")) { |
||||
SimpleCache( |
||||
context.cacheDir.resolve("downloads"), |
||||
NoOpCacheEvictor(), |
||||
get<DatabaseProvider>(named("exoDatabase")) |
||||
) |
||||
} |
||||
|
||||
single<Cache>(named("exoCache")) { |
||||
val cacheSize = PowerPreference.getDefaultFile().getInt("media_cache_size", 1).toLong() |
||||
.let { if (it == 0L) 0 else it * 1024 * 1024 * 1024 } |
||||
SimpleCache( |
||||
context.cacheDir.resolve("media"), |
||||
LeastRecentlyUsedCacheEvictor(cacheSize), |
||||
get<DatabaseProvider>(named("exoDatabase")) |
||||
) |
||||
} |
||||
|
||||
single { MediaSession(context) } |
||||
} |
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
package audio.funkwhale.ffa |
||||
|
||||
import android.content.Context |
||||
import com.preference.PowerPreference |
||||
import com.preference.Preference |
||||
import io.mockk.every |
||||
import io.mockk.mockk |
||||
import io.mockk.mockkStatic |
||||
import io.mockk.verify |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.rules.TemporaryFolder |
||||
import strikt.api.expectThat |
||||
import strikt.assertions.isFalse |
||||
|
||||
class FFATest { |
||||
|
||||
@get:Rule |
||||
val temporaryFolder = TemporaryFolder() |
||||
|
||||
@Test |
||||
fun `deleteAllData() should clear credentials preferences`() { |
||||
mockkStatic(PowerPreference::class) |
||||
val preference = mockk<Preference>(relaxed = true) |
||||
every { PowerPreference.getFileByName("credentials") } returns preference |
||||
|
||||
val context = mockk<Context>() |
||||
every { context.cacheDir } returns mockk(relaxed = true) |
||||
FFA().deleteAllData(context) |
||||
verify { preference.clear() } |
||||
} |
||||
|
||||
@Test |
||||
fun `deleteAllData() should delete cacheDir contents`() { |
||||
mockkStatic(PowerPreference::class) |
||||
every { PowerPreference.getFileByName("credentials") } returns mockk(relaxed = true) |
||||
|
||||
val tempFile = temporaryFolder.newFile() |
||||
|
||||
val context = mockk<Context>() |
||||
every { context.cacheDir } returns temporaryFolder.root |
||||
FFA().deleteAllData(context) |
||||
|
||||
expectThat(tempFile.exists()).isFalse() |
||||
} |
||||
|
||||
@Test |
||||
fun `deleteAllData() should delete picasso cache`() { |
||||
mockkStatic(PowerPreference::class) |
||||
every { PowerPreference.getFileByName("credentials") } returns mockk(relaxed = true) |
||||
|
||||
val picassoCache = temporaryFolder.newFolder("picasso-cache") |
||||
|
||||
val context = mockk<Context>() |
||||
every { context.cacheDir } returns temporaryFolder.root |
||||
|
||||
FFA().deleteAllData(context) |
||||
|
||||
expectThat(picassoCache.exists()).isFalse() |
||||
} |
||||
} |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
package audio.funkwhale.ffa |
||||
|
||||
import android.app.Application |
||||
import com.preference.PowerPreference |
||||
import org.koin.android.ext.koin.androidContext |
||||
import org.koin.core.context.loadKoinModules |
||||
import org.koin.core.context.startKoin |
||||
import org.koin.core.context.unloadKoinModules |
||||
import org.koin.core.module.Module |
||||
|
||||
class KoinTestApp : Application() { |
||||
|
||||
override fun onCreate() { |
||||
super.onCreate() |
||||
PowerPreference.init(this) |
||||
startKoin { |
||||
androidContext(this@KoinTestApp) |
||||
modules(emptyList()) |
||||
} |
||||
} |
||||
|
||||
fun loadModules(module: Module, block: () -> Unit) { |
||||
loadKoinModules(module) |
||||
block() |
||||
unloadKoinModules(module) |
||||
} |
||||
} |
Loading…
Reference in new issue