diff --git a/app/src/main/java/audio/funkwhale/ffa/koin/Modules.kt b/app/src/main/java/audio/funkwhale/ffa/koin/Modules.kt index 936019c..028f771 100644 --- a/app/src/main/java/audio/funkwhale/ffa/koin/Modules.kt +++ b/app/src/main/java/audio/funkwhale/ffa/koin/Modules.kt @@ -4,10 +4,13 @@ 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 @@ -22,6 +25,19 @@ fun exoplayerModule(context: Context) = module { ExoDatabaseProvider(context) } + single { + val cacheDataSourceFactoryProvider = get() + DownloaderConstructorHelper( + get(named("exoDownloadCache")), cacheDataSourceFactoryProvider.create(context) + ).run { + DownloadManager( + context, + DefaultDownloadIndex(get(named("exoDatabase"))), + DefaultDownloaderFactory(this) + ) + } + } + single { CacheDataSourceFactoryProvider( get(), @@ -52,7 +68,6 @@ fun exoplayerModule(context: Context) = module { } fun authModule() = module { - single { DefaultOAuth(get()) } - + single { OAuth(get()) } single { AuthorizationServiceFactory() } } \ No newline at end of file diff --git a/app/src/main/java/audio/funkwhale/ffa/repositories/FavoritesRepository.kt b/app/src/main/java/audio/funkwhale/ffa/repositories/FavoritesRepository.kt index 84e27cf..049c115 100644 --- a/app/src/main/java/audio/funkwhale/ffa/repositories/FavoritesRepository.kt +++ b/app/src/main/java/audio/funkwhale/ffa/repositories/FavoritesRepository.kt @@ -7,7 +7,6 @@ import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.upstream.cache.Cache -import com.google.android.exoplayer2.upstream.cache.SimpleCache import com.google.gson.Gson import com.google.gson.reflect.TypeToken import kotlinx.coroutines.CoroutineScope diff --git a/app/src/main/java/audio/funkwhale/ffa/repositories/SearchRepository.kt b/app/src/main/java/audio/funkwhale/ffa/repositories/SearchRepository.kt index 0ccb5c8..c23788c 100644 --- a/app/src/main/java/audio/funkwhale/ffa/repositories/SearchRepository.kt +++ b/app/src/main/java/audio/funkwhale/ffa/repositories/SearchRepository.kt @@ -5,7 +5,6 @@ import audio.funkwhale.ffa.utils.* import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.upstream.cache.Cache -import com.google.android.exoplayer2.upstream.cache.SimpleCache import com.google.gson.reflect.TypeToken import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList diff --git a/app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt b/app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt index d06fdeb..b6b5f3e 100644 --- a/app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt +++ b/app/src/main/java/audio/funkwhale/ffa/repositories/TracksRepository.kt @@ -6,7 +6,6 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.google.android.exoplayer2.offline.Download import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.upstream.cache.Cache -import com.google.android.exoplayer2.upstream.cache.SimpleCache import com.google.gson.reflect.TypeToken import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList diff --git a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt index 518e284..25fcd32 100644 --- a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt +++ b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt @@ -30,27 +30,6 @@ fun AuthState.save() { } } -interface OAuth { - - fun exchange(context: Context, authorization: Intent, success: () -> Unit, error: () -> Unit) - - fun init(hostname: String): AuthState - - fun register(authState: AuthState? = null, callback: () -> Unit) - - fun authorize(activity: Activity) - - fun isAuthorized(context: Context): Boolean - - fun tryRefreshAccessToken(context: Context): Boolean - - fun tryState(): AuthState? - - fun state(): AuthState - - fun service(context: Context): AuthorizationService -} - class AuthorizationServiceFactory { fun create(context: Context): AuthorizationService { @@ -58,17 +37,16 @@ class AuthorizationServiceFactory { } } -class DefaultOAuth(private val authorizationServiceFactory: AuthorizationServiceFactory) : OAuth { +class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory) { companion object { - private val REDIRECT_URI = Uri.parse("urn:/audio.funkwhale.funkwhale-android/oauth/callback") } data class App(val client_id: String, val client_secret: String) - override fun tryState(): AuthState? { + fun tryState(): AuthState? { val savedState = PowerPreference .getFileByName(AppContext.PREFS_CREDENTIALS) @@ -81,10 +59,10 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService } } - override fun state(): AuthState = + fun state(): AuthState = tryState() ?: throw IllegalStateException("Couldn't find saved state") - override fun isAuthorized(context: Context): Boolean { + fun isAuthorized(context: Context): Boolean { val state = tryState() return if (state != null) { state.isAuthorized || doTryRefreshAccessToken(state, context) @@ -95,7 +73,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService } } - override fun tryRefreshAccessToken(context: Context): Boolean { + fun tryRefreshAccessToken(context: Context): Boolean { tryState()?.let { state -> return doTryRefreshAccessToken(state, context) } @@ -124,23 +102,22 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService } } - override fun init(hostname: String): AuthState { + fun init(hostname: String): AuthState { return AuthState( AuthorizationServiceConfiguration( Uri.parse("$hostname/authorize"), Uri.parse("$hostname/api/v1/oauth/token/"), Uri.parse("$hostname/api/v1/oauth/apps/") ) - ) - .also { - it.save() - } + ).also { + it.save() + } } - override fun service(context: Context): AuthorizationService = + fun service(context: Context): AuthorizationService = authorizationServiceFactory.create(context) - override fun register(authState: AuthState?, callback: () -> Unit) { + fun register(authState: AuthState? = null, callback: () -> Unit) { (authState ?: state()).authorizationServiceConfiguration?.let { config -> runBlocking { val (_, _, result: Result) = Fuel.post(config.registrationEndpoint.toString()) @@ -183,7 +160,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService ) } - override fun authorize(activity: Activity) { + fun authorize(activity: Activity) { val authService = service(activity) authorizationRequest()?.let { it -> val intent = authService.getAuthorizationRequestIntent(it) @@ -191,7 +168,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService } } - override fun exchange( + fun exchange( context: Context, authorization: Intent, success: () -> Unit, diff --git a/app/src/test/java/audio/funkwhale/ffa/utils/DefaultOAuthTest.kt b/app/src/test/java/audio/funkwhale/ffa/utils/OAuthTest.kt similarity index 99% rename from app/src/test/java/audio/funkwhale/ffa/utils/DefaultOAuthTest.kt rename to app/src/test/java/audio/funkwhale/ffa/utils/OAuthTest.kt index 52a117a..cba3780 100644 --- a/app/src/test/java/audio/funkwhale/ffa/utils/DefaultOAuthTest.kt +++ b/app/src/test/java/audio/funkwhale/ffa/utils/OAuthTest.kt @@ -33,10 +33,10 @@ import strikt.assertions.isNotNull import strikt.assertions.isNull import strikt.assertions.isTrue -class DefaultOAuthTest { +class OAuthTest { @InjectMockKs - private lateinit var oAuth: DefaultOAuth + private lateinit var oAuth: OAuth @MockK private lateinit var authServiceFactory: AuthorizationServiceFactory