1 /*
2  * Copyright (C) 2019 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.systemui.statusbar.notification.people
18 
19 /** Boundary between a View and data pipeline, as seen by the pipeline. */
20 interface DataListener<in T> {
onDataChangednull21     fun onDataChanged(data: T)
22 }
23 
24 /** Convert all data using the given [mapper] before invoking this [DataListener]. */
25 fun <S, T> DataListener<T>.contraMap(mapper: (S) -> T): DataListener<S> = object : DataListener<S> {
26     override fun onDataChanged(data: S) = onDataChanged(mapper(data))
27 }
28 
29 /** Boundary between a View and data pipeline, as seen by the View. */
30 interface DataSource<out T> {
registerListenernull31     fun registerListener(listener: DataListener<T>): Subscription
32 }
33 
34 /** Represents a registration with a [DataSource]. */
35 interface Subscription {
36     /** Removes the previously registered [DataListener] from the [DataSource] */
37     fun unsubscribe()
38 }
39 
40 /** Transform all data coming out of this [DataSource] using the given [mapper]. */
mapnull41 fun <S, T> DataSource<S>.map(mapper: (S) -> T): DataSource<T> = object : DataSource<T> {
42     override fun registerListener(listener: DataListener<T>) =
43             registerListener(listener.contraMap(mapper))
44 }