android – How to transfer the selected day from the calendar with the selected day to the text?

I’m using a third party calendar library. I tried to do it through the current date, but it didn’t work out. Maybe someone knows how to transfer data from a function to another function? I need the date that the user has selected in the calendar to display as text, but I don’t know how to do it (

Gradle

 val androidMain by getting {
        dependencies {
            implementation ("com.himanshoe:kalendar:1.0.0-RC5")

            implementation("androidx.core:core-ktx:1.8.0")
            implementation("androidx.appcompat:appcompat:1.4.2")

            implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.5.0")
            implementation("androidx.activity:activity-compose:1.5.0")

            implementation("androidx.compose.ui:ui:1.2.0-rc03")
            implementation("androidx.compose.material:material:1.2.0-rc03")
            implementation("androidx.compose.ui:ui-tooling-preview:1.2.0-rc03")
            implementation("androidx.compose.material:material-icons-extended:1.2.0-rc03")

            implementation("com.google.accompanist:accompanist-systemuicontroller:0.24.13-rc")

            implementation("io.coil-kt:coil-compose:2.1.0")
            implementation("io.coil-kt:coil-gif:2.1.0")

            implementation("io.insert-koin:koin-core:3.2.0")
            implementation("io.insert-koin:koin-androidx-compose:3.2.0")

            implementation("io.github.alexgladkov:odyssey-core:1.0.0-beta12")
            implementation("io.github.alexgladkov:odyssey-compose:1.0.0-beta12")
        }

DatePicker

import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.himanshoe.kalendar.common.KalendarSelector
import com.himanshoe.kalendar.common.KalendarStyle
import com.himanshoe.kalendar.ui.Kalendar
import com.himanshoe.kalendar.ui.KalendarType
import java.time.LocalDate


@Composable
fun DatePicker(modifier: Modifier = Modifier, onDaySelected: (LocalDate) -> Unit) {
    Box(modifier = modifier) {
        Kalendar(
            kalendarType = KalendarType.Firey(),
            kalendarStyle = KalendarStyle(
                kalendarBackgroundColor = Color.White,
                kalendarColor = Color.White,
                kalendarSelector = KalendarSelector.Circle(
                    selectedColor = Color.Black,
                    eventTextColor = Color.Black,
                    todayColor = Color.White,
                    selectedTextColor = Color.White
                ),
                elevation = 0.dp
            ),
            onCurrentDayClick = { day, _ ->
                onDaySelected(day)
            })
    }
}

SelectedDate

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.happy.R
import java.time.LocalDate

@Composable
fun SelectedDate(
    date: LocalDate,
    modifier: Modifier = Modifier,
    onDateClick: () -> Unit
) {
    CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.disabled) {
        Row(
            modifier = modifier.clickable { onDateClick() },
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = date.toString(),
                style = MaterialTheme.typography.body1
            )
            Spacer(modifier = Modifier.width(8.dp))
            Icon(
                painter = painterResource(id = R.drawable.ic_calendar_outline_24),
                contentDescription = null
            )
        }
    }
}

DashboardUIState

import com.happy.screens.dashboard.presentation.models.TaskUi
import java.time.LocalDate

@Immutable
data class DashboardUiState(
    val currentDate:LocalDate = LocalDate.now(),
    val dayOfTheWeek: String = "",
    val taskList: List<TaskUi> = emptyList()
) {
    companion object {
        val Empty = DashboardUiState()
    }
}

@Immutable
sealed class DashboardUiEvent {
    object OnAddTask : DashboardUiEvent()
    class OnTaskClick(val id: Int) : DashboardUiEvent()
}

@Immutable
sealed class DashboardUiEffect {
    object NavigateToTaskCreation : DashboardUiEffect()
    class NavigateToTaskDetails(val id: Int) : DashboardUiEffect()
}

DashboardViewModel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.time.LocalDate

class DashboardViewModel : ViewModel() {

    private val _state = MutableStateFlow(DashboardUiState.Empty)
    val state = _state.asStateFlow()

    private val _effect = MutableSharedFlow<DashboardUiEffect>()
    val effect = _effect.asSharedFlow()

    init {
        _state.update { it.copy(currentDate = LocalDate.now(), dayOfTheWeek = "Сегодня") }
//        val exampleList = listOf(
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 1, unicode = "uD83DuDD25", isDone = false),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true),
//            TaskUi(id = 0, unicode = "uD83DuDD25", isDone = true)
//        )
//        _state.update { it.copy(taskList = exampleList) }
    }

    fun sendEvent(event: DashboardUiEvent) {
        when (event) {
            DashboardUiEvent.OnAddTask -> {
                viewModelScope.launch {
                    _effect.emit(DashboardUiEffect.NavigateToTaskCreation)
                }
            }
            is DashboardUiEvent.OnTaskClick -> {
                viewModelScope.launch {
                    _effect.emit(DashboardUiEffect.NavigateToTaskDetails(event.id))
                }
            }
        }
    }
}

DashboardScreen

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.happy.core.navigation.Screens
import com.happy.core.ui.rememberStateWithLifecycle
import com.happy.screens.calendar.DatePicker
import com.happy.screens.calendar.DayOfTheWeekText
import com.happy.screens.calendar.SelectedDate
import com.happy.screens.dashboard.presentation.components.AddTaskButton
import com.happy.screens.dashboard.presentation.components.EmptyTaskMessage
import com.happy.screens.dashboard.presentation.components.ProfileImage
import com.happy.screens.dashboard.presentation.components.TaskItem
import org.koin.androidx.compose.getViewModel
import kotlinx.coroutines.launch
import ru.alexgladkov.odyssey.compose.extensions.present
import ru.alexgladkov.odyssey.compose.local.LocalRootController
import ru.alexgladkov.odyssey.compose.navigation.modal_navigation.ModalSheetConfiguration
import java.time.LocalDate

@Composable
fun DashboardScreen(
) {
    DashboardScreen(
        viewModel = getViewModel(),
        onTaskCreationClick = {},
        onTaskClick = {}
    )
}

@Composable
private fun DashboardScreen(
    viewModel: DashboardViewModel,
    onTaskCreationClick: () -> Unit,
    onTaskClick: (Int) -> Unit,
) {
    val uiState by rememberStateWithLifecycle(viewModel.state)
    val systemUiController = rememberSystemUiController()
    val coroutineScope = rememberCoroutineScope()
    val rootController = LocalRootController.current
    val modalController = rootController.findModalController()
    val modalSheetConfiguration = ModalSheetConfiguration(
        maxHeight = 0.7f,
        cornerRadius = 20,
        closeOnSwipe = true
    )
    var date:LocalDate =LocalDate.now()

    LaunchedEffect(Unit) {
        viewModel.effect.collect { effect ->
            when (effect) {
                DashboardUiEffect.NavigateToTaskCreation -> onTaskCreationClick()
                is DashboardUiEffect.NavigateToTaskDetails -> onTaskClick(effect.id)
            }
        }
    }

    SideEffect {
        systemUiController.setSystemBarsColor(color = Color.Transparent, darkIcons = true)
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .systemBarsPadding()
    ) {
        Column(modifier = Modifier.fillMaxSize()) {
            Row(
                modifier = Modifier
                    .padding(horizontal = 24.dp)
                    .padding(top = 16.dp)
                    .fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                SelectedDate(
                    date = uiState.currentDate
                ) {
                    coroutineScope.launch {
                        modalController.present(modalSheetConfiguration, content = {
                            DatePicker {
                                modalController.popBackStack(animate = true)
                            }
                        })
                    }
                }
                ProfileImage(isAuthorized = false)
            }
            DayOfTheWeekText(
                day = uiState.dayOfTheWeek,
                modifier = Modifier.padding(start = 24.dp, top = 12.dp)
            )
            if (uiState.taskList.isEmpty()) {
                EmptyTaskMessage(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(bottom = 148.dp)
                        .padding(horizontal = 34.dp)
                )
            } else {
                LazyVerticalGrid(
                    columns = GridCells.Fixed(3),
                    modifier = Modifier.fillMaxSize(),
                    contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp),
                    verticalArrangement = Arrangement.spacedBy(44.dp),
                    horizontalArrangement = Arrangement.spacedBy(84.dp),
                ) {
                    items(uiState.taskList) { task ->
                        TaskItem(
                            task = task,
                            modifier = Modifier
                                .size(60.dp)
                                .clickable {
                                    rootController.launch(Screens.TaskList.name)
                                }
                        )
                    }
                }
            }
        }
        AddTaskButton(
            onClick = { viewModel.sendEvent(DashboardUiEvent.OnAddTask) },
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 16.dp)
        )
    }
}

DashboardScreen

Leave a Comment