Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89b8bab6 authored by George Lin's avatar George Lin Committed by Android (Google) Code Review
Browse files

Merge changes If406f423,Ie3278ae2 into main

* changes:
  Rename ShapeGrid to Grid
  Remove unused shape options related data flows
parents 7ac6dec2 7a732a02
Loading
Loading
Loading
Loading
+6 −9
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ constructor(
    private val previewUtils: PreviewUtils =
        PreviewUtils(context, authorityMetadataKey, Screen.HOME_SCREEN)

    override suspend fun getGridOptions(): List<GridOptionModel>? =
    override suspend fun getGridOptions(): List<GridOptionModel> =
        withContext(bgDispatcher) {
            if (previewUtils.supportsPreview()) {
                context.contentResolver
@@ -96,9 +96,9 @@ constructor(
                                list
                            }
                            .sortedByDescending { it.rows * it.cols }
                    }
                    } ?: emptyList()
            } else {
                null
                emptyList()
            }
        }

@@ -148,13 +148,10 @@ constructor(
            }
        }

    override fun applyShapeGridOption(shapeKey: String, gridKey: String): Int {
        return context.contentResolver.update(
    override fun applyGridOption(gridKey: String) {
        context.contentResolver.update(
            previewUtils.getUri(SHAPE_GRID),
            ContentValues().apply {
                put(COL_SHAPE_KEY, shapeKey)
                put(COL_GRID_KEY, gridKey)
            },
            ContentValues().apply { put(COL_GRID_KEY, gridKey) },
            null,
            null,
        )
+7 −3
Original line number Diff line number Diff line
@@ -19,12 +19,16 @@ package com.android.customization.model.grid
import android.graphics.drawable.Drawable

interface ShapeGridManager {

    suspend fun getGridOptions(): List<GridOptionModel>?
    /**
     * Get a list of grid options.
     *
     * @return It will return an empty list if there are no available grid options.
     */
    suspend fun getGridOptions(): List<GridOptionModel>

    suspend fun getShapeOptions(): List<ShapeOptionModel>?

    fun applyShapeGridOption(shapeKey: String, gridKey: String): Int
    fun applyGridOption(gridKey: String)

    fun getGridOptionDrawable(iconId: Int): Drawable?
}
+7 −15
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ package com.android.customization.picker.grid.data.repository
import android.graphics.drawable.Drawable
import com.android.customization.model.grid.GridOptionModel
import com.android.customization.model.grid.ShapeGridManager
import com.android.customization.model.grid.ShapeOptionModel
import com.android.wallpaper.picker.di.modules.BackgroundDispatcher
import javax.inject.Inject
import javax.inject.Singleton
@@ -30,12 +29,13 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Singleton
class ShapeGridRepository
class GridRepository2
@Inject
constructor(
    private val manager: ShapeGridManager,
@@ -43,32 +43,24 @@ constructor(
    @BackgroundDispatcher private val bgDispatcher: CoroutineDispatcher,
) {

    private val _shapeOptions = MutableStateFlow<List<ShapeOptionModel>?>(null)
    private val _gridOptions = MutableStateFlow<List<GridOptionModel>?>(null)

    init {
        bgScope.launch {
            _gridOptions.value = manager.getGridOptions()
            _shapeOptions.value = manager.getShapeOptions()
        }
        bgScope.launch { _gridOptions.value = manager.getGridOptions() }
    }

    val shapeOptions: StateFlow<List<ShapeOptionModel>?> = _shapeOptions.asStateFlow()

    val selectedShapeOption: Flow<ShapeOptionModel?> =
        shapeOptions.map { shapeOptions -> shapeOptions?.firstOrNull { it.isCurrent } }

    val gridOptions: StateFlow<List<GridOptionModel>?> = _gridOptions.asStateFlow()

    val selectedGridOption: Flow<GridOptionModel?> =
        gridOptions.map { gridOptions -> gridOptions?.firstOrNull { it.isCurrent } }

    suspend fun applySelectedOption(shapeKey: String, gridKey: String) =
    val isGridCustomizationAvailable = gridOptions.filterNotNull().map { it.size > 1 }

    suspend fun applySelectedOption(gridKey: String) =
        withContext(bgDispatcher) {
            manager.applyShapeGridOption(shapeKey, gridKey)
            manager.applyGridOption(gridKey)
            // After applying, we should query and update shape and grid options again.
            _gridOptions.value = manager.getGridOptions()
            _shapeOptions.value = manager.getShapeOptions()
        }

    fun getGridOptionDrawable(iconId: Int): Drawable? {
+3 −8
Original line number Diff line number Diff line
@@ -18,23 +18,18 @@
package com.android.customization.picker.grid.domain.interactor

import android.graphics.drawable.Drawable
import com.android.customization.picker.grid.data.repository.ShapeGridRepository
import com.android.customization.picker.grid.data.repository.GridRepository2
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ShapeGridInteractor @Inject constructor(private val repository: ShapeGridRepository) {

    val shapeOptions = repository.shapeOptions

    val selectedShapeOption = repository.selectedShapeOption
class GridInteractor2 @Inject constructor(private val repository: GridRepository2) {

    val gridOptions = repository.gridOptions

    val selectedGridOption = repository.selectedGridOption

    suspend fun applySelectedOption(shapeKey: String, gridKey: String) =
        repository.applySelectedOption(shapeKey, gridKey)
    suspend fun applySelectedOption(gridKey: String) = repository.applySelectedOption(gridKey)

    fun getGridOptionDrawable(iconId: Int): Drawable? = repository.getGridOptionDrawable(iconId)
}
+7 −111
Original line number Diff line number Diff line
@@ -21,14 +21,9 @@ import android.content.res.Resources
import android.graphics.drawable.Drawable
import com.android.customization.model.ResourceConstants
import com.android.customization.model.grid.GridOptionModel
import com.android.customization.model.grid.ShapeOptionModel
import com.android.customization.picker.grid.domain.interactor.ShapeGridInteractor
import com.android.customization.picker.grid.ui.viewmodel.ShapeIconViewModel
import com.android.customization.picker.grid.domain.interactor.GridInteractor2
import com.android.customization.widget.GridTileDrawable
import com.android.themepicker.R
import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon
import com.android.wallpaper.picker.common.text.ui.viewmodel.Text
import com.android.wallpaper.picker.customization.ui.viewmodel.FloatingToolbarTabViewModel
import com.android.wallpaper.picker.option.ui.viewmodel.OptionItemViewModel2
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
@@ -39,80 +34,19 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn

class ShapeGridPickerViewModel
class GridPickerViewModel
@AssistedInject
constructor(
    @ApplicationContext private val context: Context,
    private val interactor: ShapeGridInteractor,
    private val interactor: GridInteractor2,
    @Assisted private val viewModelScope: CoroutineScope,
) {

    enum class Tab {
        SHAPE,
        GRID,
    }

    //// Tabs
    private val _selectedTab = MutableStateFlow(Tab.SHAPE)
    val selectedTab: StateFlow<Tab> = _selectedTab.asStateFlow()
    val tabs: Flow<List<FloatingToolbarTabViewModel>> =
        _selectedTab.map {
            listOf(
                FloatingToolbarTabViewModel(
                    Icon.Resource(
                        res = R.drawable.ic_category_filled_24px,
                        contentDescription = Text.Resource(R.string.preview_name_shape),
                    ),
                    context.getString(R.string.preview_name_shape),
                    it == Tab.SHAPE,
                ) {
                    _selectedTab.value = Tab.SHAPE
                },
                FloatingToolbarTabViewModel(
                    Icon.Resource(
                        res = R.drawable.ic_apps_filled_24px,
                        contentDescription = Text.Resource(R.string.grid_layout),
                    ),
                    context.getString(R.string.grid_layout),
                    it == Tab.GRID,
                ) {
                    _selectedTab.value = Tab.GRID
                },
            )
        }

    //// Shape

    // The currently-set system shape option
    val selectedShapeKey =
        interactor.selectedShapeOption
            .filterNotNull()
            .map { it.key }
            .shareIn(scope = viewModelScope, started = SharingStarted.Lazily, replay = 1)
    private val overridingShapeKey = MutableStateFlow<String?>(null)
    // If the overriding key is null, use the currently-set system shape option
    val previewingShapeKey =
        combine(overridingShapeKey, selectedShapeKey) { overridingShapeOptionKey, selectedShapeKey
            ->
            overridingShapeOptionKey ?: selectedShapeKey
        }

    val shapeOptions: Flow<List<OptionItemViewModel2<ShapeIconViewModel>>> =
        interactor.shapeOptions
            .filterNotNull()
            .map { shapeOptions -> shapeOptions.map { toShapeOptionItemViewModel(it) } }
            .shareIn(scope = viewModelScope, started = SharingStarted.Lazily, replay = 1)

    //// Grid

    // The currently-set system grid option
    val selectedGridOption =
        interactor.selectedGridOption
@@ -134,19 +68,11 @@ constructor(
            .shareIn(scope = viewModelScope, started = SharingStarted.Lazily, replay = 1)

    val onApply: Flow<(suspend () -> Unit)?> =
        combine(overridingGridKey, selectedGridOption, overridingShapeKey, selectedShapeKey) {
            overridingGridKey,
            selectedGridOption,
            overridingShapeKey,
            selectedShapeKey ->
            if (
                (overridingGridKey != null && overridingGridKey != selectedGridOption.key.value) ||
                    (overridingShapeKey != null && overridingShapeKey != selectedShapeKey)
            ) {
        combine(overridingGridKey, selectedGridOption) { overridingGridKey, selectedGridOption ->
            if (overridingGridKey != null && overridingGridKey != selectedGridOption.key.value) {
                {
                    interactor.applySelectedOption(
                        overridingShapeKey ?: selectedShapeKey,
                        overridingGridKey ?: selectedGridOption.key.value,
                        overridingGridKey ?: selectedGridOption.key.value
                    )
                }
            } else {
@@ -155,37 +81,7 @@ constructor(
        }

    fun resetPreview() {
        overridingShapeKey.value = null
        overridingGridKey.value = null
        _selectedTab.value = Tab.SHAPE
    }

    private fun toShapeOptionItemViewModel(
        option: ShapeOptionModel
    ): OptionItemViewModel2<ShapeIconViewModel> {
        val isSelected =
            previewingShapeKey
                .map { it == option.key }
                .stateIn(
                    scope = viewModelScope,
                    started = SharingStarted.Lazily,
                    initialValue = false,
                )

        return OptionItemViewModel2(
            key = MutableStateFlow(option.key),
            payload = ShapeIconViewModel(option.key, option.path),
            text = Text.Loaded(option.title),
            isSelected = isSelected,
            onClicked =
                isSelected.map {
                    if (!it) {
                        { overridingShapeKey.value = option.key }
                    } else {
                        null
                    }
                },
        )
    }

    private fun toGridOptionItemViewModel(option: GridOptionModel): OptionItemViewModel2<Drawable> {
@@ -231,6 +127,6 @@ constructor(
    @ViewModelScoped
    @AssistedFactory
    interface Factory {
        fun create(viewModelScope: CoroutineScope): ShapeGridPickerViewModel
        fun create(viewModelScope: CoroutineScope): GridPickerViewModel
    }
}
Loading