Rework: extract AvatarFactory and MediaRequestDataFactory from CoilMediaFetcher
This commit is contained in:
parent
b1d4e5c206
commit
1949c2ec0b
4 changed files with 85 additions and 43 deletions
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 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
|
||||||
|
*
|
||||||
|
* https://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.libraries.matrix.ui.media
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import coil.ImageLoader
|
||||||
|
import coil.fetch.Fetcher
|
||||||
|
import coil.request.Options
|
||||||
|
import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
|
||||||
|
internal class AvatarDataFetcherFactory(
|
||||||
|
private val context: Context,
|
||||||
|
private val client: MatrixClient
|
||||||
|
) : Fetcher.Factory<AvatarData> {
|
||||||
|
override fun create(
|
||||||
|
data: AvatarData,
|
||||||
|
options: Options,
|
||||||
|
imageLoader: ImageLoader
|
||||||
|
): Fetcher {
|
||||||
|
return CoilMediaFetcher(
|
||||||
|
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||||
|
mediaLoader = client.mediaLoader,
|
||||||
|
mediaData = data.toMediaRequestData(),
|
||||||
|
options = options
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,16 +16,12 @@
|
||||||
|
|
||||||
package io.element.android.libraries.matrix.ui.media
|
package io.element.android.libraries.matrix.ui.media
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import coil.ImageLoader
|
|
||||||
import coil.decode.DataSource
|
import coil.decode.DataSource
|
||||||
import coil.decode.ImageSource
|
import coil.decode.ImageSource
|
||||||
import coil.fetch.FetchResult
|
import coil.fetch.FetchResult
|
||||||
import coil.fetch.Fetcher
|
import coil.fetch.Fetcher
|
||||||
import coil.fetch.SourceResult
|
import coil.fetch.SourceResult
|
||||||
import coil.request.Options
|
import coil.request.Options
|
||||||
import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
|
||||||
import io.element.android.libraries.matrix.api.MatrixClient
|
|
||||||
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
|
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
|
||||||
import io.element.android.libraries.matrix.api.media.MediaSource
|
import io.element.android.libraries.matrix.api.media.MediaSource
|
||||||
import io.element.android.libraries.matrix.api.media.toFile
|
import io.element.android.libraries.matrix.api.media.toFile
|
||||||
|
|
@ -102,41 +98,4 @@ internal class CoilMediaFetcher(
|
||||||
dataSource = DataSource.MEMORY
|
dataSource = DataSource.MEMORY
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
class MediaRequestDataFactory(
|
|
||||||
private val context: Context,
|
|
||||||
private val client: MatrixClient
|
|
||||||
) :
|
|
||||||
Fetcher.Factory<MediaRequestData> {
|
|
||||||
override fun create(
|
|
||||||
data: MediaRequestData,
|
|
||||||
options: Options,
|
|
||||||
imageLoader: ImageLoader
|
|
||||||
): Fetcher {
|
|
||||||
return CoilMediaFetcher(
|
|
||||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
|
||||||
mediaLoader = client.mediaLoader,
|
|
||||||
mediaData = data,
|
|
||||||
options = options
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AvatarFactory(
|
|
||||||
private val context: Context,
|
|
||||||
private val client: MatrixClient
|
|
||||||
) : Fetcher.Factory<AvatarData> {
|
|
||||||
override fun create(
|
|
||||||
data: AvatarData,
|
|
||||||
options: Options,
|
|
||||||
imageLoader: ImageLoader
|
|
||||||
): Fetcher {
|
|
||||||
return CoilMediaFetcher(
|
|
||||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
|
||||||
mediaLoader = client.mediaLoader,
|
|
||||||
mediaData = data.toMediaRequestData(),
|
|
||||||
options = options
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,8 +52,8 @@ class DefaultLoggedInImageLoaderFactory @Inject constructor(
|
||||||
}
|
}
|
||||||
add(AvatarDataKeyer())
|
add(AvatarDataKeyer())
|
||||||
add(MediaRequestDataKeyer())
|
add(MediaRequestDataKeyer())
|
||||||
add(CoilMediaFetcher.AvatarFactory(context, matrixClient))
|
add(AvatarDataFetcherFactory(context, matrixClient))
|
||||||
add(CoilMediaFetcher.MediaRequestDataFactory(context, matrixClient))
|
add(MediaRequestDataFetcherFactory(context, matrixClient))
|
||||||
}
|
}
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 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
|
||||||
|
*
|
||||||
|
* https://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.libraries.matrix.ui.media
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import coil.ImageLoader
|
||||||
|
import coil.fetch.Fetcher
|
||||||
|
import coil.request.Options
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
|
||||||
|
internal class MediaRequestDataFetcherFactory(
|
||||||
|
private val context: Context,
|
||||||
|
private val client: MatrixClient
|
||||||
|
) : Fetcher.Factory<MediaRequestData> {
|
||||||
|
override fun create(
|
||||||
|
data: MediaRequestData,
|
||||||
|
options: Options,
|
||||||
|
imageLoader: ImageLoader
|
||||||
|
): Fetcher {
|
||||||
|
return CoilMediaFetcher(
|
||||||
|
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||||
|
mediaLoader = client.mediaLoader,
|
||||||
|
mediaData = data,
|
||||||
|
options = options
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue