-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from KevinSchildhorn/ks/AddingCoil
Ks/adding coil
- Loading branch information
Showing
17 changed files
with
178 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
shared/src/androidMain/kotlin/com/kevinschildhorn/fotopresenter/SMBJFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.kevinschildhorn.fotopresenter | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import coil3.ImageLoader | ||
import coil3.asImage | ||
import coil3.decode.DataSource | ||
import coil3.fetch.FetchResult | ||
import coil3.fetch.Fetcher | ||
import coil3.fetch.ImageFetchResult | ||
import coil3.request.Options | ||
import com.hierynomus.smbj.share.File | ||
import com.kevinschildhorn.fotopresenter.data.datasources.ImageCacheDataSource | ||
import com.kevinschildhorn.fotopresenter.data.network.NetworkDirectoryDetails | ||
import com.kevinschildhorn.fotopresenter.data.network.NetworkHandler | ||
import com.kevinschildhorn.fotopresenter.ui.shared.DriverFactory | ||
import com.kevinschildhorn.fotopresenter.ui.shared.SharedCache | ||
import com.kevinschildhorn.fotopresenter.ui.shared.getScaledDimensions | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlin.math.roundToInt | ||
|
||
class SMBJFetcher( | ||
private val directoryDetails: NetworkDirectoryDetails, | ||
private val networkHandler: NetworkHandler, | ||
context: Context, | ||
) : Fetcher { | ||
|
||
val imageCacheDataSource = ImageCacheDataSource( | ||
cache = SharedCache, | ||
driver = DriverFactory(context).createDriver(), | ||
logger = baseLogger.withTag("ImageCacheDataSource") | ||
) | ||
|
||
override suspend fun fetch(): FetchResult? { | ||
return withContext(Dispatchers.IO) { | ||
imageCacheDataSource.getImage(directoryDetails) | ||
if (networkHandler.isConnected) { | ||
val image = networkHandler.openImage(path = directoryDetails.fullPath) | ||
val file = image?.file | ||
if (file != null) { | ||
val bitmap = getBitmapFromFile(file, 64) | ||
if (bitmap != null) { | ||
file.close() | ||
ImageFetchResult( | ||
image = bitmap.asImage(), | ||
isSampled = true, | ||
dataSource = DataSource.NETWORK, | ||
) | ||
} else { | ||
throw Exception("Failed to fetch image from FTP") | ||
} | ||
} else { | ||
throw Exception("Failed to fetch image from FTP") | ||
} | ||
} else { | ||
throw Exception("Failed to fetch image from FTP") | ||
} | ||
} | ||
} | ||
|
||
private fun getBitmapFromFile(file: File, size: Int): Bitmap? { | ||
|
||
val options = BitmapFactory.Options() | ||
options.inJustDecodeBounds = true | ||
BitmapFactory.decodeStream(file.inputStream, null, options) | ||
|
||
val height: Int = options.outHeight | ||
val width: Int = options.outWidth | ||
val dimensions = getScaledDimensions(width, height, size) | ||
val heightRatio: Int = (height.toFloat() / dimensions.second.toFloat()).roundToInt() | ||
val widthRatio: Int = (width.toFloat() / dimensions.first.toFloat()).roundToInt() | ||
options.inSampleSize = if (heightRatio < widthRatio) heightRatio else widthRatio | ||
|
||
options.inJustDecodeBounds = false | ||
return BitmapFactory.decodeStream(file.inputStream, null, options) | ||
} | ||
|
||
class Factory(private val networkHandler: NetworkHandler) : Fetcher.Factory<NetworkDirectoryDetails> { | ||
override fun create( | ||
data: NetworkDirectoryDetails, | ||
options: Options, | ||
imageLoader: ImageLoader | ||
): Fetcher? = SMBJFetcher(data, networkHandler, options.context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.