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.car.dialer.ui.search;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 
25 import com.android.car.dialer.livedata.SharedPreferencesLiveData;
26 import com.android.car.dialer.ui.common.ContactResultsLiveData;
27 import com.android.car.dialer.ui.common.DialerListViewModel;
28 
29 import java.util.List;
30 
31 import javax.inject.Inject;
32 
33 import dagger.hilt.android.lifecycle.HiltViewModel;
34 import dagger.hilt.android.qualifiers.ApplicationContext;
35 
36 /**
37  * {link AndroidViewModel} used for search functionality.
38  */
39 @HiltViewModel
40 public class ContactResultsViewModel extends DialerListViewModel {
41 
42     private final ContactResultsLiveData mContactSearchResultsLiveData;
43     private final MutableLiveData<String> mSearchQueryLiveData;
44 
45     @Inject
ContactResultsViewModel(@pplicationContext Context context, SharedPreferencesLiveData.Factory sharedPreferencesFactory, ContactResultsLiveData.Factory contactResultsLiveDataFactory)46     public ContactResultsViewModel(@ApplicationContext Context context,
47             SharedPreferencesLiveData.Factory sharedPreferencesFactory,
48             ContactResultsLiveData.Factory contactResultsLiveDataFactory) {
49         super(context, sharedPreferencesFactory);
50         mSearchQueryLiveData = new MutableLiveData<>();
51         mContactSearchResultsLiveData = createContactSearchResultsLiveData(
52                 contactResultsLiveDataFactory);
53     }
54 
55     /** Creates the {@link ContactResultsLiveData} representing the contact search results. */
createContactSearchResultsLiveData( ContactResultsLiveData.Factory factory)56     public ContactResultsLiveData createContactSearchResultsLiveData(
57             ContactResultsLiveData.Factory factory) {
58         return factory.create(
59                 getSearchQueryLiveData(),
60                 getSharedPreferencesLiveData(),
61                 /* showOnlyOneEntry = */true);
62     }
63 
64     /**
65      * Sets search query.
66      */
setSearchQuery(String searchQuery)67     public void setSearchQuery(String searchQuery) {
68         if (TextUtils.equals(mSearchQueryLiveData.getValue(), searchQuery)) {
69             return;
70         }
71 
72         mSearchQueryLiveData.setValue(searchQuery);
73     }
74 
75     /**
76      * Returns live data of search results.
77      */
getContactSearchResults()78     public LiveData<List<ContactResultsLiveData.ContactResultListItem>> getContactSearchResults() {
79         return mContactSearchResultsLiveData;
80     }
81 
82     /**
83      * Returns search query.
84      */
getSearchQuery()85     public String getSearchQuery() {
86         return mSearchQueryLiveData.getValue();
87     }
88 
89     /**
90      * Returns Search Query LiveData.
91      */
getSearchQueryLiveData()92     public MutableLiveData<String> getSearchQueryLiveData() {
93         return mSearchQueryLiveData;
94     }
95 }
96