From 32c22bb6e566b1ccbe172b5d1a4615c84b69bff0 Mon Sep 17 00:00:00 2001 From: Evgenii Kozlov Date: Thu, 23 Mar 2023 14:20:08 +0100 Subject: [PATCH] DROID-1109 Widgets | Enhancement | Render space icon (#3039) --- .../anytypeio/anytype/ui/home/HomeScreen.kt | 47 +++++++++++++++++-- .../anytype/ui/home/HomeScreenFragment.kt | 1 + .../presentation/home/HomeScreenViewModel.kt | 12 +++-- .../anytype/presentation/spaces/SpaceIcon.kt | 1 + 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreen.kt b/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreen.kt index 7f12edd87a..f2a5858a6a 100644 --- a/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreen.kt +++ b/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreen.kt @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed @@ -42,11 +43,14 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import coil.compose.rememberAsyncImagePainter import com.anytypeio.anytype.R import com.anytypeio.anytype.core_models.ObjectWrapper import com.anytypeio.anytype.core_ui.extensions.throttledClick import com.anytypeio.anytype.core_ui.foundation.noRippleClickable +import com.anytypeio.anytype.emojifier.Emojifier import com.anytypeio.anytype.presentation.home.InteractionMode +import com.anytypeio.anytype.presentation.spaces.SpaceIcon import com.anytypeio.anytype.presentation.widgets.DropDownMenuAction import com.anytypeio.anytype.presentation.widgets.FromIndex import com.anytypeio.anytype.presentation.widgets.ToIndex @@ -66,9 +70,11 @@ import org.burnoutcrew.reorderable.ReorderableItem import org.burnoutcrew.reorderable.detectReorderAfterLongPress import org.burnoutcrew.reorderable.rememberReorderableLazyListState import org.burnoutcrew.reorderable.reorderable +import timber.log.Timber @Composable fun HomeScreen( + spaceIcon: SpaceIcon, mode: InteractionMode, widgets: List, onExpand: (TreePath) -> Unit, @@ -138,6 +144,7 @@ fun HomeScreen( exit = fadeOut() + slideOutVertically { it } ) { HomeScreenBottomToolbar( + spaceIcon = spaceIcon, onSearchClicked = throttledClick(onSearchClicked), onCreateNewObjectClicked = throttledClick(onCreateNewObjectClicked), onSpaceClicked = throttledClick(onSpaceClicked), @@ -496,6 +503,7 @@ fun HomeScreenButton( @Composable fun HomeScreenBottomToolbar( + spaceIcon: SpaceIcon, modifier: Modifier, onSearchClicked: () -> Unit, onCreateNewObjectClicked: () -> Unit, @@ -540,11 +548,40 @@ fun HomeScreenBottomToolbar( .fillMaxSize() .noRippleClickable { onSpaceClicked() } ) { - Image( - painter = painterResource(id = R.drawable.ic_home_widget_space), - contentDescription = "Space icon", - modifier = Modifier.align(Alignment.Center) - ) + Timber.d("Binding icon: $spaceIcon") + when(spaceIcon) { + is SpaceIcon.Emoji -> { + Image( + painter = rememberAsyncImagePainter( + model = Emojifier.uri(spaceIcon.unicode), + error = painterResource(id = R.drawable.ic_home_widget_space) + ), + contentDescription = "Emoji space icon", + modifier = Modifier + .size(24.dp) + .align(Alignment.Center) + ) + } + is SpaceIcon.Image -> { + Image( + painter = rememberAsyncImagePainter( + model = spaceIcon.url, + error = painterResource(id = R.drawable.ic_home_widget_space) + ), + contentDescription = "Custom image space icon", + modifier = Modifier + .size(24.dp) + .align(Alignment.Center) + ) + } + else -> { + Image( + painter = painterResource(id = R.drawable.ic_home_widget_space), + contentDescription = "Placeholder space icon", + modifier = Modifier.align(Alignment.Center) + ) + } + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreenFragment.kt b/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreenFragment.kt index 94cae2a1af..eda16d4f8c 100644 --- a/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreenFragment.kt +++ b/app/src/main/java/com/anytypeio/anytype/ui/home/HomeScreenFragment.kt @@ -60,6 +60,7 @@ class HomeScreenFragment : BaseComposeFragment() { ) ) { HomeScreen( + spaceIcon = vm.icon.collectAsState().value, widgets = vm.views.collectAsState().value, mode = vm.mode.collectAsState().value, onExpand = { path -> vm.onExpand(path) }, diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/home/HomeScreenViewModel.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/home/HomeScreenViewModel.kt index 2c9eabe802..6b42e055d2 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/home/HomeScreenViewModel.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/home/HomeScreenViewModel.kt @@ -37,7 +37,6 @@ import com.anytypeio.anytype.domain.page.CreateObject import com.anytypeio.anytype.domain.widgets.CreateWidget import com.anytypeio.anytype.domain.widgets.DeleteWidget import com.anytypeio.anytype.domain.widgets.UpdateWidget -import com.anytypeio.anytype.presentation.common.ViewState import com.anytypeio.anytype.presentation.home.Command.ChangeWidgetType.Companion.UNDEFINED_LAYOUT_CODE import com.anytypeio.anytype.presentation.navigation.NavigationViewModel import com.anytypeio.anytype.presentation.search.Subscriptions @@ -118,7 +117,7 @@ class HomeScreenViewModel( // Bundled widget containing archived objects private val bin = WidgetView.Bin(Subscriptions.SUBSCRIPTION_ARCHIVED) - val icon = MutableStateFlow>(ViewState.Loading) + val icon = MutableStateFlow(SpaceIcon.Loading) init { val config = configStorage.get() @@ -261,7 +260,7 @@ class HomeScreenViewModel( ) ).map { result -> val obj = result.firstOrNull() - ViewState.Success(obj?.spaceIcon(urlBuilder) ?: SpaceIcon.Placeholder) + obj?.spaceIcon(urlBuilder) ?: SpaceIcon.Placeholder }.flowOn(appCoroutineDispatchers.io).collect { icon.value = it } @@ -784,6 +783,13 @@ class HomeScreenViewModel( } } + override fun onCleared() { + super.onCleared() + viewModelScope.launch { + unsubscriber.unsubscribe(listOf(HOME_SCREEN_SPACE_OBJECT_SUBSCRIPTION)) + } + } + sealed class Navigation { data class OpenObject(val ctx: Id) : Navigation() data class OpenSet(val ctx: Id) : Navigation() diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/spaces/SpaceIcon.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/spaces/SpaceIcon.kt index 46b65112eb..766aa788d0 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/spaces/SpaceIcon.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/spaces/SpaceIcon.kt @@ -5,6 +5,7 @@ import com.anytypeio.anytype.core_models.Url import com.anytypeio.anytype.domain.misc.UrlBuilder sealed class SpaceIcon { + object Loading : SpaceIcon() object Placeholder : SpaceIcon() data class Emoji(val unicode: String) : SpaceIcon() data class Image(val url: Url) : SpaceIcon()