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

Commit 7a06228d authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

Reset support for themed icons (2/3).

Fix: 267804366
Test: unit tests
Test: manually verified starting with themed icons on or off, reset
button shows when different from original value, button hides when the
same as the original value, resetting and confirming returns to the
original value

Change-Id: I6c6610265e2a279b6a8732d35e128482ac35396f
parent 17e8bbef
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ java_defaults {
        "SettingsLibSettingsTheme",
        "SystemUI-statsd",
        "styleprotoslite",
        "androidx.lifecycle_lifecycle-livedata-ktx",
        "androidx.lifecycle_lifecycle-runtime-ktx",
        "androidx.lifecycle_lifecycle-viewmodel-ktx",
        "androidx.recyclerview_recyclerview",
+26 −7
Original line number Diff line number Diff line
@@ -20,11 +20,13 @@ import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.android.customization.model.themedicon.domain.interactor.ThemedIconInteractor;
import com.android.customization.model.themedicon.domain.interactor.ThemedIconSnapshotRestorer;
import com.android.customization.picker.themedicon.ThemedIconSectionView;
import com.android.wallpaper.R;
import com.android.wallpaper.model.CustomizationSectionController;
import com.android.wallpaper.model.WorkspaceViewModel;

/** The {@link CustomizationSectionController} for themed icon section. */
public class ThemedIconSectionController implements
@@ -33,16 +35,26 @@ public class ThemedIconSectionController implements
    private static final String KEY_THEMED_ICON_ENABLED = "SAVED_THEMED_ICON_ENABLED";

    private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
    private final WorkspaceViewModel mWorkspaceViewModel;
    private final ThemedIconInteractor mInteractor;
    private final ThemedIconSnapshotRestorer mSnapshotRestorer;
    private final Observer<Boolean> mIsActivatedChangeObserver;

    private ThemedIconSectionView mThemedIconSectionView;
    private boolean mSavedThemedIconEnabled = false;


    public ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider,
            WorkspaceViewModel workspaceViewModel, @Nullable Bundle savedInstanceState) {
    public ThemedIconSectionController(
            ThemedIconSwitchProvider themedIconOptionsProvider,
            ThemedIconInteractor interactor,
            @Nullable Bundle savedInstanceState,
            ThemedIconSnapshotRestorer snapshotRestorer) {
        mThemedIconOptionsProvider = themedIconOptionsProvider;
        mWorkspaceViewModel = workspaceViewModel;
        mInteractor = interactor;
        mSnapshotRestorer = snapshotRestorer;
        mIsActivatedChangeObserver = isActivated -> {
            if (mThemedIconSectionView.isAttachedToWindow()) {
                mThemedIconSectionView.getSwitch().setChecked(isActivated);
            }
        };

        if (savedInstanceState != null) {
            mSavedThemedIconEnabled = savedInstanceState.getBoolean(
@@ -64,15 +76,22 @@ public class ThemedIconSectionController implements
        mThemedIconSectionView.getSwitch().setChecked(mSavedThemedIconEnabled);
        mThemedIconOptionsProvider.fetchThemedIconEnabled(
                enabled -> mThemedIconSectionView.getSwitch().setChecked(enabled));
        mInteractor.isActivatedAsLiveData().observeForever(mIsActivatedChangeObserver);
        return mThemedIconSectionView;
    }

    @Override
    public void release() {
        mInteractor.isActivatedAsLiveData().removeObserver(mIsActivatedChangeObserver);
    }

    private void onViewActivated(Context context, boolean viewActivated) {
        if (context == null) {
            return;
        }
        mThemedIconOptionsProvider.setThemedIconEnabled(viewActivated);
        mWorkspaceViewModel.getUpdateWorkspace().setValue(viewActivated);
        mInteractor.setActivated(viewActivated);
        mSnapshotRestorer.store(viewActivated);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ public class ThemedIconSwitchProvider {
     *
     * <p>The value would also be stored in SharedPreferences.
     */
    protected void setThemedIconEnabled(boolean enabled) {
    public void setThemedIconEnabled(boolean enabled) {
        mExecutorService.submit(() -> {
            ContentValues values = new ContentValues();
            values.put(COL_ICON_THEMED_VALUE, enabled);
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.customization.model.themedicon.data.repository

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class ThemeIconRepository {
    private val _isActivated = MutableStateFlow(false)
    val isActivated = _isActivated.asStateFlow()

    fun setActivated(isActivated: Boolean) {
        _isActivated.value = isActivated
    }
}
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.customization.model.themedicon.domain.interactor

import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import com.android.customization.model.themedicon.data.repository.ThemeIconRepository

class ThemedIconInteractor(
    private val repository: ThemeIconRepository,
) {
    val isActivated = repository.isActivated

    private var isActivatedAsLiveData: LiveData<Boolean>? = null

    fun isActivatedAsLiveData(): LiveData<Boolean> {
        return isActivatedAsLiveData ?: isActivated.asLiveData().also { isActivatedAsLiveData = it }
    }

    fun setActivated(isActivated: Boolean) {
        repository.setActivated(isActivated)
    }
}
Loading