forked from ShiftHackZ/Stable-Diffusion-Android
-
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 #1 from Itsfitts/feature/many-models
Feature/many models
- Loading branch information
Showing
51 changed files
with
1,076 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:tools="http://schemas.android.com/tools" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-feature | ||
android:name="android.hardware.camera" | ||
android:required="false" /> | ||
|
||
<uses-permission android:name="android.permission.CAMERA" /> | ||
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> | ||
|
||
<uses-permission | ||
android:name="android.permission.READ_EXTERNAL_STORAGE" | ||
android:maxSdkVersion="32" /> | ||
|
||
<uses-permission | ||
android:name="android.permission.WRITE_EXTERNAL_STORAGE" | ||
android:maxSdkVersion="31" | ||
tools:ignore="ScopedStorage" /> | ||
|
||
<uses-permission | ||
android:name="android.permission.MANAGE_EXTERNAL_STORAGE" | ||
tools:ignore="ScopedStorage" /> | ||
|
||
</manifest> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:tools="http://schemas.android.com/tools" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-feature | ||
android:name="android.hardware.camera" | ||
android:required="false" /> | ||
|
||
<uses-permission android:name="android.permission.CAMERA" /> | ||
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> | ||
|
||
<uses-permission | ||
android:name="android.permission.READ_EXTERNAL_STORAGE" | ||
android:maxSdkVersion="32" /> | ||
|
||
<uses-permission | ||
android:name="android.permission.WRITE_EXTERNAL_STORAGE" | ||
android:maxSdkVersion="31" | ||
tools:ignore="ScopedStorage" /> | ||
|
||
</manifest> |
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
13 changes: 13 additions & 0 deletions
13
core/common/src/main/java/com/shifthackz/aisdv1/core/common/appbuild/BuildType.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,13 @@ | ||
package com.shifthackz.aisdv1.core.common.appbuild | ||
|
||
enum class BuildType { | ||
FOSS, | ||
PLAY; | ||
|
||
companion object { | ||
fun fromBuildConfig(input: String) = when (input) { | ||
"FOSS" -> FOSS | ||
else -> PLAY | ||
} | ||
} | ||
} |
79 changes: 71 additions & 8 deletions
79
data/src/main/java/com/shifthackz/aisdv1/data/local/DownloadableModelLocalDataSource.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 |
---|---|---|
@@ -1,29 +1,92 @@ | ||
package com.shifthackz.aisdv1.data.local | ||
|
||
import com.shifthackz.aisdv1.core.common.appbuild.BuildInfoProvider | ||
import com.shifthackz.aisdv1.core.common.appbuild.BuildType | ||
import com.shifthackz.aisdv1.core.common.file.FileProviderDescriptor | ||
import com.shifthackz.aisdv1.core.common.log.debugLog | ||
import com.shifthackz.aisdv1.data.mappers.mapDomainToEntity | ||
import com.shifthackz.aisdv1.data.mappers.mapEntityToDomain | ||
import com.shifthackz.aisdv1.domain.datasource.DownloadableModelDataSource | ||
import com.shifthackz.aisdv1.domain.entity.LocalAiModel | ||
import com.shifthackz.aisdv1.domain.preference.PreferenceManager | ||
import com.shifthackz.aisdv1.storage.db.persistent.dao.LocalModelDao | ||
import com.shifthackz.aisdv1.storage.db.persistent.entity.LocalModelEntity | ||
import io.reactivex.rxjava3.core.Completable | ||
import io.reactivex.rxjava3.core.Observable | ||
import io.reactivex.rxjava3.core.Single | ||
import java.io.File | ||
|
||
internal class DownloadableModelLocalDataSource( | ||
private val fileProviderDescriptor: FileProviderDescriptor, | ||
private val dao: LocalModelDao, | ||
private val preferenceManager: PreferenceManager, | ||
private val buildInfoProvider: BuildInfoProvider, | ||
) : DownloadableModelDataSource.Local { | ||
override fun getAll(): Single<List<LocalAiModel>> = dao.query() | ||
.map(List<LocalModelEntity>::mapEntityToDomain) | ||
.map { models -> | ||
buildList { | ||
addAll(models) | ||
if (buildInfoProvider.type == BuildType.FOSS) add(LocalAiModel.CUSTOM) | ||
} | ||
} | ||
.flatMap { models -> models.withLocalData() } | ||
|
||
override fun getById(id: String): Single<LocalAiModel> { | ||
val chain = if (id == LocalAiModel.CUSTOM.id) Single.just(LocalAiModel.CUSTOM) | ||
else dao | ||
.queryById(id) | ||
.map(LocalModelEntity::mapEntityToDomain) | ||
|
||
return chain.flatMap { model -> model.withLocalData() } | ||
} | ||
|
||
override fun getSelected(): Single<LocalAiModel> = Single | ||
.just(preferenceManager.localModelId) | ||
.flatMap(::getById) | ||
.onErrorResumeNext { Single.error(Throwable("No selected model")) } | ||
|
||
override fun select(id: String): Completable = Completable.fromAction { | ||
preferenceManager.localModelId = id | ||
} | ||
|
||
private val localModelDirectory: File | ||
get() = File(fileProviderDescriptor.localModelDirPath) | ||
override fun save(list: List<LocalAiModel>) = list | ||
.filter { it.id != LocalAiModel.CUSTOM.id } | ||
.mapDomainToEntity() | ||
.let(dao::insertList) | ||
|
||
override fun exists(): Single<Boolean> = Single.create { emitter -> | ||
override fun isDownloaded(id: String): Single<Boolean> = Single.create { emitter -> | ||
try { | ||
val files = (localModelDirectory.listFiles()?.filter { it.isDirectory }) ?: emptyList<File>() | ||
if (!emitter.isDisposed) emitter.onSuccess(localModelDirectory.exists() && files.size == 4) | ||
if (id == LocalAiModel.CUSTOM.id) { | ||
if (!emitter.isDisposed) emitter.onSuccess(true) | ||
} else { | ||
val localModelDir = getLocalModelDirectory(id) | ||
val files = | ||
(localModelDir.listFiles()?.filter { it.isDirectory }) ?: emptyList<File>() | ||
if (!emitter.isDisposed) emitter.onSuccess(localModelDir.exists() && files.size == 4) | ||
} | ||
} catch (e: Exception) { | ||
if (!emitter.isDisposed) emitter.onSuccess(false) | ||
} | ||
} | ||
|
||
override fun delete(): Completable = Completable.fromAction { | ||
localModelDirectory.deleteRecursively() | ||
override fun delete(id: String): Completable = Completable.fromAction { | ||
getLocalModelDirectory(id).deleteRecursively() | ||
} | ||
|
||
private fun getLocalModelDirectory(id: String): File { | ||
return File("${fileProviderDescriptor.localModelDirPath}/${id}") | ||
} | ||
|
||
private fun List<LocalAiModel>.withLocalData(): Single<List<LocalAiModel>> = Observable | ||
.fromIterable(this) | ||
.flatMapSingle { model -> model.withLocalData() } | ||
.toList() | ||
|
||
private fun LocalAiModel.withLocalData(): Single<LocalAiModel> = isDownloaded(id) | ||
.map { downloaded -> | ||
copy( | ||
downloaded = downloaded, | ||
selected = preferenceManager.localModelId == id, | ||
) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
data/src/main/java/com/shifthackz/aisdv1/data/mappers/LocalAiModelMappers.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,37 @@ | ||
package com.shifthackz.aisdv1.data.mappers | ||
|
||
import com.shifthackz.aisdv1.domain.entity.LocalAiModel | ||
import com.shifthackz.aisdv1.network.response.DownloadableModelResponse | ||
import com.shifthackz.aisdv1.storage.db.persistent.entity.LocalModelEntity | ||
|
||
//region RAW --> DOMAIN | ||
fun List<DownloadableModelResponse>.mapRawToDomain(): List<LocalAiModel> = | ||
map(DownloadableModelResponse::mapRawToDomain) | ||
|
||
fun DownloadableModelResponse.mapRawToDomain(): LocalAiModel = with(this) { | ||
LocalAiModel( | ||
id = id ?: "", | ||
name = name ?: "", | ||
size = size ?: "", | ||
sources = sources ?: emptyList(), | ||
) | ||
} | ||
//endregion | ||
|
||
//region DOMAIN --> ENTITY | ||
fun List<LocalAiModel>.mapDomainToEntity(): List<LocalModelEntity> = | ||
map(LocalAiModel::mapDomainToEntity) | ||
|
||
fun LocalAiModel.mapDomainToEntity(): LocalModelEntity = with(this) { | ||
LocalModelEntity(id, name, size, sources) | ||
} | ||
//endregion | ||
|
||
//region ENTITY --> DOMAIN | ||
fun List<LocalModelEntity>.mapEntityToDomain(): List<LocalAiModel> = | ||
map(LocalModelEntity::mapEntityToDomain) | ||
|
||
fun LocalModelEntity.mapEntityToDomain(): LocalAiModel = with(this) { | ||
LocalAiModel(id, name, size, sources) | ||
} | ||
//endregion |
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.