Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26.08.21 Load config from firebase #111

Open
github-actions bot opened this issue Sep 17, 2021 · 0 comments
Open

26.08.21 Load config from firebase #111

github-actions bot opened this issue Sep 17, 2021 · 0 comments
Labels

Comments

@github-actions
Copy link

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

@github-actions github-actions bot added the todo label Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants