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.common;
18 
19 import android.annotation.StringRes;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import androidx.annotation.DrawableRes;
26 import androidx.annotation.LayoutRes;
27 import androidx.annotation.NonNull;
28 import androidx.recyclerview.widget.LinearLayoutManager;
29 import androidx.recyclerview.widget.RecyclerView;
30 
31 import com.android.car.dialer.R;
32 import com.android.car.dialer.widget.LoadingFrameLayout;
33 import com.android.car.ui.recyclerview.CarUiRecyclerView;
34 
35 /**
36  * Base fragment that inflates a {@link RecyclerView}. It handles the top offset for first row item
37  * so the list can scroll underneath the top bar.
38  */
39 public class DialerListBaseFragment extends DialerBaseFragment {
40 
41     private LoadingFrameLayout mLoadingFrameLayout;
42     private CarUiRecyclerView mRecyclerView;
43 
44     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)45     public View onCreateView(LayoutInflater inflater, ViewGroup container,
46             Bundle savedInstanceState) {
47         View view = inflater.inflate(getLayoutResource(), container, false);
48         mLoadingFrameLayout = view.findViewById(R.id.loading_frame_layout);
49         mRecyclerView = view.findViewById(R.id.list_view);
50         mRecyclerView.setLayoutManager(createLayoutManager());
51         return view;
52     }
53 
54     /**
55      * Layout resource for this fragment. It must contains a RecyclerView with id list_view.
56      */
57     @LayoutRes
getLayoutResource()58     protected int getLayoutResource() {
59         return R.layout.loading_list_fragment;
60     }
61 
62     /**
63      * Creates the layout manager for the recycler view. Default is a {@link LinearLayoutManager}.
64      * Child inheriting from this fragment can override to create a different layout manager.
65      */
66     @NonNull
createLayoutManager()67     protected RecyclerView.LayoutManager createLayoutManager() {
68         return new LinearLayoutManager(getContext());
69     }
70 
71     /**
72      * Returns the {@link RecyclerView} instance.
73      */
74     @NonNull
getRecyclerView()75     protected CarUiRecyclerView getRecyclerView() {
76         return mRecyclerView;
77     }
78 
79     /**
80      * Shows loading spinner when the data is still loading.
81      */
showLoading()82     protected void showLoading() {
83         mLoadingFrameLayout.showLoading();
84     }
85 
86     /**
87      * Shows content when data is loaded and the content is not empty.
88      */
showContent()89     protected void showContent() {
90         mLoadingFrameLayout.showContent();
91     }
92 
93     /**
94      * Shows the empty view with icon, message and secondary message.
95      */
showEmpty(@rawableRes int iconResId, @StringRes int messageResId, @StringRes int secondaryMessageResId)96     protected void showEmpty(@DrawableRes int iconResId, @StringRes int messageResId,
97             @StringRes int secondaryMessageResId) {
98         mLoadingFrameLayout.showEmpty(iconResId, messageResId, secondaryMessageResId);
99     }
100 
101     /**
102      * Shows the empty view with icon, message, secondary message and action button.
103      */
showEmpty(@rawableRes int iconResId, @StringRes int messageResId, @StringRes int secondaryMessageResId, @StringRes int actionButtonTextResId, View.OnClickListener actionButtonOnClickListener, boolean showActionButton)104     protected void showEmpty(@DrawableRes int iconResId, @StringRes int messageResId,
105             @StringRes int secondaryMessageResId,
106             @StringRes int actionButtonTextResId, View.OnClickListener actionButtonOnClickListener,
107             boolean showActionButton) {
108         mLoadingFrameLayout.showEmpty(iconResId, messageResId, secondaryMessageResId,
109                 actionButtonTextResId, actionButtonOnClickListener, showActionButton);
110     }
111 
112     @Override
onToolbarHeightChange(int toolbarHeight)113     public void onToolbarHeightChange(int toolbarHeight) {
114         int listTopPadding = getContext().getResources().getDimensionPixelSize(
115                 R.dimen.list_top_padding);
116         mRecyclerView.setPaddingRelative(
117                 mRecyclerView.getPaddingStart(),
118                 toolbarHeight + listTopPadding,
119                 mRecyclerView.getPaddingEnd(),
120                 mRecyclerView.getPaddingBottom());
121     }
122 }
123