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

Commit 5ee8b444 authored by Bruno Martins's avatar Bruno Martins
Browse files

fixup! SystemUI: Clock position customization

Clean up unnecessary stuff and run through ktfmt.

Change-Id: I8a08407580cb28deb1291932731a41ff09df63f5
parent e0daa268
Loading
Loading
Loading
Loading
+63 −66
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ import com.android.systemui.statusbar.policy.Clock
import javax.inject.Inject
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.update
@@ -101,10 +100,10 @@ constructor(
    }

    private data class ClockState(
        val visibilityModel: VisibilityModel,
        val denyListed: Boolean,
        val hideForHun: Boolean,
        val position: Int
        val position: Int,
        val visibilityModel: VisibilityModel,
    )

    override fun bind(
@@ -138,12 +137,13 @@ constructor(
            repeatOnLifecycle(Lifecycle.State.CREATED) {
                val context = view.context

                val clockState = MutableStateFlow(
                val clockState =
                    MutableStateFlow(
                        ClockState(
                        visibilityModel = VisibilityModel(View.GONE, true),
                            denyListed = false,
                            hideForHun = false,
                        position = context.contentResolver.readClockPosition()
                            position = context.contentResolver.readClockPosition(),
                            visibilityModel = VisibilityModel(View.GONE, true),
                        )
                    )

@@ -152,17 +152,25 @@ constructor(
                val statusBarClockUri: Uri =
                    LineageSettings.System.getUriFor(LineageSettings.System.STATUS_BAR_CLOCK)

                val contentObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
                val contentObserver =
                    object : ContentObserver(Handler(Looper.getMainLooper())) {
                        override fun onChange(selfChange: Boolean, uri: Uri?) {
                            clockState.update { current ->
                                when (uri) {
                                iconHideListUri -> current.copy(
                                    denyListed = StatusBarIconController.getIconHideList(context,
                                        Settings.Secure.getString(context.contentResolver,
                                            StatusBarIconController.ICON_HIDE_LIST)
                                    ).contains("clock")
                                    iconHideListUri ->
                                        current.copy(
                                            denyListed =
                                                StatusBarIconController.getIconHideList(
                                                        context,
                                                        Settings.Secure.getString(
                                                            context.contentResolver,
                                                            StatusBarIconController.ICON_HIDE_LIST,
                                                        ),
                                                    )
                                                    .contains("clock")
                                        )
                                statusBarClockUri -> current.copy(
                                    statusBarClockUri ->
                                        current.copy(
                                            position = context.contentResolver.readClockPosition()
                                        )
                                    else -> current
@@ -171,15 +179,16 @@ constructor(
                        }
                    }

                val urisToObserve = listOf(iconHideListUri, statusBarClockUri)
                urisToObserve.forEach { uri ->
                    context.contentResolver.registerContentObserver(
                    iconHideListUri, false, contentObserver, UserHandle.USER_ALL
                )
                context.contentResolver.registerContentObserver(
                    statusBarClockUri, false, contentObserver, UserHandle.USER_ALL
                        uri,
                        false,
                        contentObserver,
                        UserHandle.USER_ALL,
                    )

                contentObserver.onChange(false, iconHideListUri)
                contentObserver.onChange(false, statusBarClockUri)
                    contentObserver.onChange(false, uri)
                }

                // Ensure cleanup when lifecycle ends
                val job = coroutineContext[Job]
@@ -371,34 +380,32 @@ constructor(
                    launch {
                        combine(
                                viewModel.isClockVisible,
                            viewModel.hideStartSideContentForHeadsUp
                                viewModel.hideStartSideContentForHeadsUp,
                            ) { visibilityModel, hideForHun ->
                                visibilityModel to hideForHun
                        }.collect { (visibilityModel, hideForHun) ->
                            }
                            .collect { (visibilityModel, hideForHun) ->
                                clockState.update { current ->
                                    current.copy(
                                        visibilityModel = visibilityModel,
                                    hideForHun = hideForHun
                                        hideForHun = hideForHun,
                                    )
                                }
                            }
                    }

                    var activeClock: Clock = leftClock
                    activeClock.setIsActiveClock(true)
                    centerClock.setIsActiveClock(false)
                    rightClock.setIsActiveClock(false)

                    launch {
                        clockState.collect { state ->
                            // We only want to hide left clock for HUN
                            val hunBlocksClock = state.position == CLOCK_POSITION_LEFT
                                && state.hideForHun
                            val hunBlocksClock =
                                state.position == CLOCK_POSITION_LEFT && state.hideForHun

                            // Apply denylist on top of ViewModel visibility
                            val finalVisibility =
                                if (state.visibilityModel.visibility == View.VISIBLE &&
                                    !hunBlocksClock && !state.denyListed
                                if (
                                    state.visibilityModel.visibility == View.VISIBLE &&
                                        !hunBlocksClock &&
                                        !state.denyListed
                                ) {
                                    state.visibilityModel
                                } else {
@@ -406,7 +413,7 @@ constructor(
                                }

                            // Pick active clock view
                            val newActiveClock: Clock =
                            val activeClock: Clock =
                                when (state.position) {
                                    CLOCK_POSITION_CENTER -> centerClock ?: leftClock
                                    CLOCK_POSITION_RIGHT -> rightClock ?: leftClock
@@ -414,16 +421,6 @@ constructor(
                                    else -> leftClock
                                }

                            if (newActiveClock !== activeClock) {
                                // Deactivate previous clock
                                activeClock.setIsActiveClock(false)
                                activeClock.visibility = View.GONE

                                // Activate new one
                                activeClock = newActiveClock
                                activeClock.setIsActiveClock(true)
                            }

                            // Hide all clocks first
                            leftClock.visibility = View.GONE
                            centerClock.visibility = View.GONE
@@ -517,7 +514,7 @@ constructor(
            this,
            LineageSettings.System.STATUS_BAR_CLOCK,
            CLOCK_POSITION_LEFT,
            UserHandle.USER_CURRENT
            UserHandle.USER_CURRENT,
        )
    }