egorikftp/Lady-happy-Android

26.08.21 Load config from firebase

github-actions opened this issue · 0 comments

26.08.21 Load config from firebase


import com.egoriku.ladyhappy.postcreator.domain.predefined.PredefinedData
import com.egoriku.ladyhappy.postcreator.domain.usecase.PublishPostUseCase
import com.egoriku.ladyhappy.postcreator.domain.usecase.UploadImagesUseCase
import com.egoriku.ladyhappy.postcreator.presentation.state.DialogEvent
import com.egoriku.ladyhappy.postcreator.presentation.state.Effect
import com.egoriku.ladyhappy.postcreator.presentation.state.PostState
import com.egoriku.ladyhappy.postcreator.presentation.state.ScreenState
import com.egoriku.ladyhappy.postcreator.presentation.state.ScreenState.Uploading.Stage
import com.google.firebase.Timestamp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*

const val MAX_IMAGES_SIZE = 10

class PostViewModel(
    application: Application,
    private val uploadImagesUseCase: UploadImagesUseCase,
    private val publishPostUseCase: PublishPostUseCase,
) : AndroidViewModel(application) {

    private val _uiState = MutableStateFlow<ScreenState>(ScreenState.None)
    val uiState = _uiState.asStateFlow()

    private val _postState = MutableStateFlow(PostState())
    val postState = _postState.asStateFlow()

    private val _publishButtonAvailability = MutableStateFlow(false)
    val publishButtonAvailability = _publishButtonAvailability.asStateFlow()

    private val _dialogEffects = MutableSharedFlow<DialogEvent>()
    val dialogEffects = _dialogEffects.asSharedFlow()

    private val _effects = MutableSharedFlow<Effect>()
    val effects = _effects.asSharedFlow()

    private val _currentPostState
        get() = _postState.value

    private val subCategoryId: Int
        get() = requireNotNull(
            _currentPostState.chooserState
                .filterIsInstance<ChooserType.SubCategory>()
                .firstOrNull()
                ?.categoryId
        )

    init {
        viewModelScope.launch {
            _uiState.emit(ScreenState.Loading)
            // TODO: 26.08.21 Load config from firebase
            _uiState.emit(ScreenState.CreatePost)
        }
    }

    fun processEffect(effect: Effect) {
        viewModelScope.launch {
            _effects.emit(effect)
        }
    }

    fun openDialog(type: ChooserType) {
        val dialogEvent = when (type) {
            is ChooserType.Category -> DialogEvent.Category(
                data = PredefinedData.getCategoriesNames()
            )
            is ChooserType.SubCategory -> DialogEvent.SubCategory(
                data = PredefinedData.getSubCategoriesNames(subCategoryId)
            )
            is ChooserType.Color -> DialogEvent.Color
            is ChooserType.CreationDate -> DialogEvent.Date
        }

        viewModelScope.launch {
            _dialogEffects.emit(dialogEvent)
        }
    }

    fun processImageResult(list: List<Uri>) {
        val images = _currentPostState.imagesSection.images
        val newImages = list.map { ImageItem(uri = it) }

        _postState.value = _currentPostState.copy(
            imagesSection = ImageSection(images + newImages)
        )


1871318460819d14f96bf237fd5a6a6b44598b8e