1 /* 2 * 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.systemui.complication; 18 19 import androidx.lifecycle.LiveData; 20 import androidx.lifecycle.Transformations; 21 import androidx.lifecycle.ViewModel; 22 23 import java.util.Collection; 24 import java.util.stream.Collectors; 25 26 import javax.inject.Inject; 27 28 /** 29 * {@link ComplicationCollectionViewModel} is an abstraction for observing and accessing 30 * {@link ComplicationViewModel} for registered {@link Complication}. 31 */ 32 public class ComplicationCollectionViewModel extends ViewModel { 33 private final LiveData<Collection<ComplicationViewModel>> mComplications; 34 private final ComplicationViewModelTransformer mTransformer; 35 36 /** 37 * Injectable constructor for {@link ComplicationCollectionViewModel}. Note that this cannot 38 * be implicitly injected. Clients must bind scoped instance values through the corresponding 39 * dagger subcomponent. 40 */ 41 @Inject ComplicationCollectionViewModel( ComplicationCollectionLiveData complications, ComplicationViewModelTransformer transformer)42 public ComplicationCollectionViewModel( 43 ComplicationCollectionLiveData complications, 44 ComplicationViewModelTransformer transformer) { 45 mComplications = Transformations.map(complications, collection -> convert(collection)); 46 mTransformer = transformer; 47 } 48 convert(Collection<Complication> complications)49 private Collection<ComplicationViewModel> convert(Collection<Complication> complications) { 50 return complications 51 .stream() 52 .map(complication -> mTransformer.getViewModel(complication)) 53 .collect(Collectors.toSet()); 54 } 55 56 /** 57 * Returns {@link LiveData} for the collection of {@link Complication} represented as 58 * {@link ComplicationViewModel}. 59 */ getComplications()60 public LiveData<Collection<ComplicationViewModel>> getComplications() { 61 return mComplications; 62 } 63 } 64