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