Created
October 6, 2019 02:00
-
-
Save enginebai/6aaa6ee6b145fad5d95721e5cb897f2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class GalleryViewModel : ViewModel(), KoinComponent { | |
var setting: AlbumSetting? = null | |
val multipleSelectMedia = BehaviorSubject.createDefault<MutableList<Media>>(mutableListOf()) | |
val singleSelectMedia = PublishSubject.create<Media>() | |
val currentAlbumItem = BehaviorSubject.create<AlbumItem>() | |
private val albumRepo: AlbumRepo by inject() | |
fun loadAlbums(): Completable { | |
return albumRepo.fetchAlbums(setting) | |
.doOnComplete { selectAlbum(albumRepo.getAlbumItemSync(ALL_MEDIA_ALBUM_NAME, setting)!!) } | |
} | |
fun getAlbums(): Observable<List<AlbumItem>> = albumRepo.getAlbums(setting) | |
fun selectAlbum(album: AlbumItem) { | |
currentAlbumItem.onNext(album) | |
} | |
fun selectMedia(media: Media) { | |
if (true == setting?.multipleSelection) { | |
multipleSelectMedia.value?.run { | |
if (this.contains(media)) | |
this.remove(media) | |
else { | |
setting?.maxSelection?.let { | |
if (this.size < it) | |
this.add(media) | |
} ?: kotlin.run { | |
this.add(media) | |
} | |
} | |
multipleSelectMedia.onNext(this) | |
} | |
} else { | |
singleSelectMedia.onNext(media) | |
} | |
} | |
fun isSelect(media: Media): Boolean = multipleSelectMedia.value!!.contains(media) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment