1 /*
<lambda>null2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.settingslib.spa.framework.util
18
19 import android.os.Bundle
20 import androidx.compose.runtime.Composable
21 import androidx.core.os.bundleOf
22 import com.android.settingslib.spa.framework.common.LOG_DATA_SWITCH_STATUS
23 import com.android.settingslib.spa.framework.common.LocalEntryDataProvider
24 import com.android.settingslib.spa.framework.common.LogCategory
25 import com.android.settingslib.spa.framework.common.LogEvent
26 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
27
28 @Composable
29 fun logEntryEvent(): (event: LogEvent, extraData: Bundle) -> Unit {
30 val entryId = LocalEntryDataProvider.current.entryId ?: return { _, _ -> }
31 val arguments = LocalEntryDataProvider.current.arguments
32 return { event, extraData ->
33 SpaEnvironmentFactory.instance.logger.event(
34 entryId, event, category = LogCategory.VIEW, extraData = extraData.apply {
35 if (arguments != null) putAll(arguments)
36 }
37 )
38 }
39 }
40
41 @Composable
wrapOnClickWithLognull42 fun wrapOnClickWithLog(onClick: (() -> Unit)?): (() -> Unit)? {
43 if (onClick == null) return null
44 val logEvent = logEntryEvent()
45 return {
46 logEvent(LogEvent.ENTRY_CLICK, bundleOf())
47 onClick()
48 }
49 }
50
51 @Composable
wrapOnSwitchWithLognull52 fun wrapOnSwitchWithLog(onSwitch: ((checked: Boolean) -> Unit)?): ((checked: Boolean) -> Unit)? {
53 if (onSwitch == null) return null
54 val logEvent = logEntryEvent()
55 return {
56 logEvent(LogEvent.ENTRY_SWITCH, bundleOf(LOG_DATA_SWITCH_STATUS to it))
57 onSwitch(it)
58 }
59 }
60