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.app.Application;
20 
21 import androidx.annotation.WorkerThread;
22 import androidx.arch.core.executor.ArchTaskExecutor;
23 import androidx.lifecycle.AndroidViewModel;
24 import androidx.lifecycle.LiveData;
25 import androidx.paging.DataSource;
26 import androidx.paging.LivePagedListBuilder;
27 import androidx.paging.PagedList;
28 import androidx.paging.RxPagedListBuilder;
29 import androidx.room.Room;
30 import androidx.room.integration.testapp.database.Customer;
31 import androidx.room.integration.testapp.database.LastNameAscCustomerDataSource;
32 import androidx.room.integration.testapp.database.SampleDatabase;
33 
34 import java.util.UUID;
35 
36 import io.reactivex.BackpressureStrategy;
37 import io.reactivex.Flowable;
38 
39 /**
40  * Sample database-backed view model of Customers
41  */
42 public class CustomerViewModel extends AndroidViewModel {
43     private SampleDatabase mDatabase;
44     private LiveData<PagedList<Customer>> mLiveCustomerList;
45 
CustomerViewModel(Application application)46     public CustomerViewModel(Application application) {
47         super(application);
48         createDb();
49     }
50 
createDb()51     private void createDb() {
52         mDatabase = Room.databaseBuilder(this.getApplication(),
53                 SampleDatabase.class, "customerDatabase").build();
54 
55         ArchTaskExecutor.getInstance().executeOnDiskIO(() -> {
56             // fill with some simple data
57             int customerCount = mDatabase.getCustomerDao().countCustomers();
58             if (customerCount == 0) {
59                 Customer[] initialCustomers = new Customer[10];
60                 for (int i = 0; i < 10; i++) {
61                     initialCustomers[i] = createCustomer();
62                 }
63                 mDatabase.getCustomerDao().insertAll(initialCustomers);
64             }
65 
66         });
67     }
68 
69     @WorkerThread
createCustomer()70     private Customer createCustomer() {
71         Customer customer = new Customer();
72         customer.setName(UUID.randomUUID().toString());
73         customer.setLastName(UUID.randomUUID().toString());
74         return customer;
75     }
76 
insertCustomer()77     void insertCustomer() {
78         ArchTaskExecutor.getInstance().executeOnDiskIO(
79                 () -> mDatabase.getCustomerDao().insert(createCustomer()));
80     }
81 
clearAllCustomers()82     void clearAllCustomers() {
83         ArchTaskExecutor.getInstance().executeOnDiskIO(
84                 () -> mDatabase.getCustomerDao().removeAll());
85     }
86 
getLivePagedList( K initialLoadKey, DataSource.Factory<K, Customer> dataSourceFactory)87     private static <K> LiveData<PagedList<Customer>> getLivePagedList(
88             K initialLoadKey, DataSource.Factory<K, Customer> dataSourceFactory) {
89         PagedList.Config config = new PagedList.Config.Builder()
90                 .setPageSize(10)
91                 .setEnablePlaceholders(false)
92                 .build();
93         return new LivePagedListBuilder<>(dataSourceFactory, config)
94                 .setInitialLoadKey(initialLoadKey)
95                 .build();
96     }
97 
getPagedListFlowable( DataSource.Factory<K, Customer> dataSourceFactory)98     private static <K> Flowable<PagedList<Customer>> getPagedListFlowable(
99             DataSource.Factory<K, Customer> dataSourceFactory) {
100         PagedList.Config config = new PagedList.Config.Builder()
101                 .setPageSize(10)
102                 .setEnablePlaceholders(false)
103                 .build();
104         return new RxPagedListBuilder<>(dataSourceFactory, config)
105                 .buildFlowable(BackpressureStrategy.LATEST);
106     }
107 
getPagedListFlowable()108     Flowable<PagedList<Customer>> getPagedListFlowable() {
109         return getPagedListFlowable(mDatabase.getCustomerDao().loadPagedAgeOrder());
110     }
111 
getLivePagedList(int position)112     LiveData<PagedList<Customer>> getLivePagedList(int position) {
113         if (mLiveCustomerList == null) {
114             mLiveCustomerList =
115                     getLivePagedList(position, mDatabase.getCustomerDao().loadPagedAgeOrder());
116         }
117         return mLiveCustomerList;
118     }
119 
getLivePagedList(String key)120     LiveData<PagedList<Customer>> getLivePagedList(String key) {
121         if (mLiveCustomerList == null) {
122             mLiveCustomerList =
123                     getLivePagedList(key, LastNameAscCustomerDataSource.factory(mDatabase));
124         }
125         return mLiveCustomerList;
126     }
127 }
128