1 /*
2  * Copyright 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 androidx.paging.integration.testapp.room;
18 
19 import android.os.Bundle;
20 import android.widget.Button;
21 
22 import androidx.appcompat.app.AppCompatActivity;
23 import androidx.lifecycle.LiveData;
24 import androidx.lifecycle.ViewModelProvider;
25 import androidx.paging.PagedList;
26 import androidx.paging.integration.testapp.R;
27 import androidx.recyclerview.widget.LinearLayoutManager;
28 import androidx.recyclerview.widget.RecyclerView;
29 
30 /**
31  * Sample PagedList activity which uses Room.
32  */
33 public class RoomPagedListActivity extends AppCompatActivity {
34 
35     private RecyclerView mRecyclerView;
36     private PagedListCustomerAdapter mAdapter;
37 
38     private static final String STRING_KEY = "STRING_KEY";
39     private static final String INT_KEY = "INT_KEY";
40 
41     @Override
onCreate(final Bundle savedInstanceState)42     protected void onCreate(final Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         setContentView(R.layout.activity_room_recycler_view);
45         // TODO use by viewModels() once this class switches to Kotlin
46         final CustomerViewModel viewModel = new ViewModelProvider(this)
47                 .get(CustomerViewModel.class);
48 
49         mRecyclerView = findViewById(R.id.recyclerview);
50         mAdapter = new PagedListCustomerAdapter();
51         mRecyclerView.setAdapter(mAdapter);
52 
53         LiveData<PagedList<Customer>> livePagedList;
54         if (useKeyedQuery()) {
55             String key = null;
56             if (savedInstanceState != null) {
57                 key = savedInstanceState.getString(STRING_KEY);
58                 mAdapter.setScrollToKey(key);
59             }
60             livePagedList = viewModel.getLivePagedList(key);
61         } else {
62             int position = 0;
63             if (savedInstanceState != null) {
64                 position = savedInstanceState.getInt(INT_KEY);
65                 mAdapter.setScrollToPosition(position);
66             }
67             livePagedList = viewModel.getLivePagedList(position);
68         }
69         livePagedList.observe(this, items -> mAdapter.submitList(items));
70 
71         final Button addButton = findViewById(R.id.addButton);
72         addButton.setOnClickListener(v -> viewModel.insertCustomer());
73 
74         final Button clearButton = findViewById(R.id.clearButton);
75         clearButton.setOnClickListener(v -> viewModel.clearAllCustomers());
76     }
77 
78     @Override
onSaveInstanceState(Bundle outState)79     protected void onSaveInstanceState(Bundle outState) {
80         super.onSaveInstanceState(outState);
81         PagedList<Customer> list = mAdapter.getCurrentList();
82         if (list == null) {
83             // Can't find anything to restore
84             return;
85         }
86 
87         LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
88         final int targetPosition = layoutManager.findFirstVisibleItemPosition();
89 
90         if (useKeyedQuery()) {
91             Customer customer = list.get(targetPosition);
92             if (customer != null) {
93                 String key = LastNameAscCustomerDataSource.getKeyStatic(customer);
94                 outState.putString(STRING_KEY, key);
95             }
96         } else {
97             // NOTE: in the general case, we can't just rely on RecyclerView/LinearLayoutManager to
98             // preserve position, because of position offset which is present when using an
99             // uncounted, non-keyed source).
100             int absolutePosition = targetPosition + list.getPositionOffset();
101             outState.putInt(INT_KEY, absolutePosition);
102         }
103     }
104 
useKeyedQuery()105     protected boolean useKeyedQuery() {
106         return false;
107     }
108 }
109