Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright (C) 2017 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.room.integration.testapp;
     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.ViewModelProviders;
     25 import androidx.paging.PagedList;
     26 import androidx.recyclerview.widget.LinearLayoutManager;
     27 import androidx.recyclerview.widget.RecyclerView;
     28 import androidx.room.integration.testapp.database.Customer;
     29 import androidx.room.integration.testapp.database.LastNameAscCustomerDataSource;
     30 
     31 /**
     32  * Sample PagedList activity which uses Room.
     33  */
     34 public class RoomPagedListActivity extends AppCompatActivity {
     35 
     36     private RecyclerView mRecyclerView;
     37     private PagedListCustomerAdapter mAdapter;
     38 
     39     private static final String STRING_KEY = "STRING_KEY";
     40     private static final String INT_KEY = "INT_KEY";
     41 
     42     @Override
     43     protected void onCreate(final Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45         setContentView(R.layout.activity_recycler_view);
     46         final CustomerViewModel viewModel = ViewModelProviders.of(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         final Button button = findViewById(R.id.addButton);
     71         button.setOnClickListener(v -> viewModel.insertCustomer());
     72     }
     73 
     74     @Override
     75     protected void onSaveInstanceState(Bundle outState) {
     76         super.onSaveInstanceState(outState);
     77         PagedList<Customer> list = mAdapter.getCurrentList();
     78         if (list == null) {
     79             // Can't find anything to restore
     80             return;
     81         }
     82 
     83         LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
     84         final int targetPosition = layoutManager.findFirstVisibleItemPosition();
     85 
     86         if (useKeyedQuery()) {
     87             Customer customer = list.get(targetPosition);
     88             if (customer != null) {
     89                 String key = LastNameAscCustomerDataSource.getKeyStatic(customer);
     90                 outState.putString(STRING_KEY, key);
     91             }
     92         } else {
     93             // NOTE: in the general case, we can't just rely on RecyclerView/LinearLayoutManager to
     94             // preserve position, because of position offset which is present when using an
     95             // uncounted, non-keyed source).
     96             int absolutePosition = targetPosition + list.getPositionOffset();
     97             outState.putInt(INT_KEY, absolutePosition);
     98         }
     99     }
    100 
    101     protected boolean useKeyedQuery() {
    102         return false;
    103     }
    104 }
    105