-
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 #34 from KevinSchildhorn/CachingImages
Adding cache and cleaning up
- Loading branch information
Showing
29 changed files
with
475 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<resources> | ||
<string name="app_name">My application</string> | ||
<string name="app_name">FotoPresenter</string> | ||
</resources> |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
TEAM_ID= | ||
BUNDLE_ID=com.kevinschildhorn.fotopresenter | ||
APP_NAME=My application | ||
APP_NAME=Foto Presenter |
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
29 changes: 29 additions & 0 deletions
29
...rc/androidMain/kotlin/com/kevinschildhorn/fotopresenter/ui/shared/SharedImageConverter.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,29 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.shared | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asAndroidBitmap | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import java.nio.ByteBuffer | ||
|
||
actual object SharedImageConverter { | ||
actual fun convertBytes(byteArray: ByteArray): ImageBitmap { | ||
val bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size) | ||
return bmp.asImageBitmap() | ||
} | ||
|
||
actual fun convertImage(imageBitmap: ImageBitmap): ByteArray { | ||
return imageBitmap.asAndroidBitmap().convertToByteArray() | ||
} | ||
|
||
private fun Bitmap.convertToByteArray(): ByteArray { | ||
val size = this.byteCount | ||
val buffer = ByteBuffer.allocate(size) | ||
val bytes = ByteArray(size) | ||
this.copyPixelsToBuffer(buffer) | ||
buffer.rewind() | ||
buffer.get(bytes) | ||
return bytes | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
...monMain/kotlin/com/kevinschildhorn/fotopresenter/data/datasources/ImageCacheDataSource.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,19 @@ | ||
package com.kevinschildhorn.fotopresenter.data.datasources | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import com.kevinschildhorn.fotopresenter.data.network.NetworkDirectoryDetails | ||
import com.kevinschildhorn.fotopresenter.ui.shared.SharedCache | ||
|
||
class ImageCacheDataSource { | ||
fun getImage(directory: NetworkDirectoryDetails): ImageBitmap? = SharedCache.getImage(directory.cacheId) | ||
|
||
fun saveImage( | ||
directory: NetworkDirectoryDetails, | ||
bitmap: ImageBitmap, | ||
) { | ||
SharedCache.cacheImage(directory.cacheId, bitmap) | ||
} | ||
|
||
private val NetworkDirectoryDetails.cacheId: String | ||
get() = "$name.$id" | ||
} |
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
19 changes: 19 additions & 0 deletions
19
shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/LogoutUseCase.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,19 @@ | ||
package com.kevinschildhorn.fotopresenter.domain | ||
|
||
import co.touchlab.kermit.Logger | ||
import com.kevinschildhorn.fotopresenter.data.network.NetworkHandler | ||
import com.kevinschildhorn.fotopresenter.data.repositories.CredentialsRepository | ||
|
||
/** | ||
Logging out of App | ||
**/ | ||
class LogoutUseCase( | ||
private val credentialsRepository: CredentialsRepository, | ||
private val networkHandler: NetworkHandler, | ||
private val logger: Logger, | ||
) { | ||
suspend operator fun invoke() { | ||
networkHandler.disconnect() | ||
credentialsRepository.clearAutoConnect() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...d/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/RetrieveImagesUseCase.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,34 @@ | ||
package com.kevinschildhorn.fotopresenter.domain | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import co.touchlab.kermit.Logger | ||
import com.kevinschildhorn.fotopresenter.data.ImageDirectory | ||
import com.kevinschildhorn.fotopresenter.data.State | ||
import com.kevinschildhorn.fotopresenter.ui.shared.SharedCache | ||
|
||
/** | ||
Retrieving Directories from Location | ||
**/ | ||
class RetrieveImagesUseCase( | ||
private val logger: Logger, | ||
) { | ||
suspend operator fun invoke( | ||
directory: ImageDirectory, | ||
callback: suspend (State<ImageBitmap>) -> Unit, | ||
) { | ||
callback(State.LOADING) | ||
SharedCache.getImage(directory.name)?.let { | ||
callback(State.SUCCESS(it)) | ||
} | ||
val imageBitmap: ImageBitmap? = directory.image?.getImageBitmap(400) | ||
|
||
callback( | ||
imageBitmap?.let { | ||
State.SUCCESS(it) | ||
} ?: State.ERROR("No Image Found"), | ||
) | ||
imageBitmap?.let { | ||
SharedCache.cacheImage(directory.name, it) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/extension/ListExtension.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,19 @@ | ||
package com.kevinschildhorn.fotopresenter.extension | ||
|
||
fun List<Any>.getNextIndex(index: Int): Int { | ||
val nextIndex = index + 1 | ||
return if (nextIndex >= this.count()) { | ||
0 | ||
} else { | ||
nextIndex | ||
} | ||
} | ||
|
||
fun List<Any>.getPreviousIndex(index: Int): Int { | ||
val previousIndex = index - 1 | ||
return if (previousIndex < 0) { | ||
this.count() - 1 | ||
} else { | ||
previousIndex | ||
} | ||
} |
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.