1 /*
2  * Copyright (C) 2018 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.contact;
18 
19 import android.os.Bundle;
20 import android.view.View;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import androidx.fragment.app.Fragment;
25 import androidx.lifecycle.ViewModelProviders;
26 
27 import com.android.car.dialer.Constants;
28 import com.android.car.dialer.R;
29 import com.android.car.dialer.ui.common.DialerListBaseFragment;
30 import com.android.car.telephony.common.Contact;
31 
32 /**
33  * Contact Fragment.
34  */
35 public class ContactListFragment extends DialerListBaseFragment implements
36         ContactListAdapter.OnShowContactDetailListener {
37     private ContactListAdapter mContactListAdapter;
38 
newInstance()39     public static ContactListFragment newInstance() {
40         return new ContactListFragment();
41     }
42 
43     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)44     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
45         super.onViewCreated(view, savedInstanceState);
46         // Don't recreate the adapter if we already have one, so that the list items
47         // will display immediately upon the view being recreated. If they're not displayed
48         // immediately, we won't remember our scroll position.
49         if (mContactListAdapter == null) {
50             mContactListAdapter = new ContactListAdapter(
51                     getContext(), /* onShowContactDetailListener= */this);
52         }
53         getRecyclerView().setAdapter(mContactListAdapter);
54 
55         ContactListViewModel contactListViewModel = ViewModelProviders.of(this).get(
56                 ContactListViewModel.class);
57         contactListViewModel.getAllContacts().observe(this, contacts -> {
58             if (contacts.isLoading()) {
59                 showLoading();
60             } else if (contacts.getData() == null) {
61                 showEmpty(Constants.INVALID_RES_ID, R.string.contact_list_empty,
62                         R.string.available_after_sync);
63             } else {
64                 mContactListAdapter.setContactList(contacts.getData());
65                 showContent();
66             }
67         });
68     }
69 
70     @Override
onShowContactDetail(Contact contact)71     public void onShowContactDetail(Contact contact) {
72         Fragment contactDetailsFragment = ContactDetailsFragment.newInstance(contact);
73         pushContentFragment(contactDetailsFragment, ContactDetailsFragment.FRAGMENT_TAG);
74     }
75 }
76