Refactor where Dagger Components belongs (in node)

This commit is contained in:
ganfra 2023-01-12 21:14:48 +01:00
parent 020fd3b458
commit ae2534488b
14 changed files with 98 additions and 193 deletions

View file

@ -1,34 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.matrix.ui
import coil.ComponentRegistry
import io.element.android.x.matrix.MatrixClient
import io.element.android.x.matrix.ui.media.MediaFetcher
import io.element.android.x.matrix.ui.media.MediaKeyer
import javax.inject.Inject
class MatrixUi @Inject constructor() {
fun registerCoilComponents(
builder: ComponentRegistry.Builder,
activeClientProvider: () -> MatrixClient?
) {
builder.add(MediaKeyer())
builder.add(MediaFetcher.Factory(activeClientProvider))
}
}

View file

@ -0,0 +1,12 @@
package io.element.android.x.matrix.ui.di
import com.squareup.anvil.annotations.ContributesTo
import io.element.android.x.di.SessionScope
import io.element.android.x.matrix.ui.media.LoggedInImageLoaderFactory
import io.element.android.x.matrix.ui.media.NotLoggedInImageLoaderFactory
@ContributesTo(SessionScope::class)
interface MatrixUIBindings {
fun loggedInImageLoaderFactory(): LoggedInImageLoaderFactory
fun notLoggedInImageLoaderFactory(): NotLoggedInImageLoaderFactory
}

View file

@ -0,0 +1,34 @@
package io.element.android.x.matrix.ui.media
import android.content.Context
import coil.ImageLoader
import coil.ImageLoaderFactory
import io.element.android.x.di.ApplicationContext
import io.element.android.x.matrix.MatrixClient
import javax.inject.Inject
class LoggedInImageLoaderFactory @Inject constructor(
@ApplicationContext private val context: Context,
private val matrixClient: MatrixClient,
) : ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader
.Builder(context)
.components {
add(MediaKeyer())
add(MediaFetcher.Factory(matrixClient))
}
.build()
}
}
class NotLoggedInImageLoaderFactory @Inject constructor(
@ApplicationContext private val context: Context,
) : ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader
.Builder(context)
.build()
}
}

View file

@ -37,16 +37,15 @@ internal class MediaFetcher(
return imageLoader.components.newFetcher(byteBuffer, options, imageLoader)?.first?.fetch()
}
class Factory(private val activeClientProvider: () -> MatrixClient?) :
class Factory(private val client: MatrixClient) :
Fetcher.Factory<MediaResolver.Meta> {
override fun create(
data: MediaResolver.Meta,
options: Options,
imageLoader: ImageLoader
): Fetcher {
val activeClient = activeClientProvider()
return MediaFetcher(
mediaResolver = activeClient?.mediaResolver(),
mediaResolver = client.mediaResolver(),
meta = data,
options = options,
imageLoader = imageLoader