This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: openjdk:8-jdk | |
variables: | |
ANDROID_COMPILE_SDK: "25" | |
ANDROID_BUILD_TOOLS: "25.0.2" | |
ANDROID_SDK_TOOLS: "25.2.5" | |
before_script: | |
# installing packages | |
- apt-get --quiet update --yes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017, The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface UserDao { | |
@Query("SELECT * FROM user ORDER BY lastName ASC") | |
public abstract LivePagedListProvider<Integer, User> usersByLastName(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel extends ViewModel { | |
public final LiveData<PagedList<User>> usersList; | |
public MyViewModel(UserDao userDao) { | |
usersList = userDao.usersByLastName().create( | |
/* initial load position */ 0, | |
new PagedList.Config.Builder() | |
.setPageSize(50) | |
.setPrefetchDistance(50) | |
.build()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyActivity extends AppCompatActivity { | |
@Override | |
public void onCreate(Bundle savedState) { | |
super.onCreate(savedState); | |
MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class); | |
RecyclerView recyclerView = findViewById(R.id.user_list); | |
UserAdapter<User> adapter = new UserAdapter(); | |
viewModel.usersList.observe(this, pagedList -> adapter.setList(pagedList)); | |
recyclerView.setAdapter(adapter); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAdapter extends PagedListAdapter<User, UserViewHolder> { | |
public UserAdapter() { | |
super(DIFF_CALLBACK); | |
} | |
@Override | |
public void onBindViewHolder(UserViewHolder holder, int position) { | |
User user = getItem(position); | |
if (user != null) { | |
holder.bindTo(user); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserRepository { | |
private Webservice webservice; | |
// ... | |
public LiveData<User> getUser(int userId) { | |
// This is not an optimal implementation, we'll fix it below | |
final MutableLiveData<User> data = new MutableLiveData<>(); | |
webservice.getUser(userId).enqueue(new Callback<User>() { | |
@Override | |
public void onResponse(Call<User> call, Response<User> response) { | |
// error case is left out for brevity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Webservice { | |
@GET("/users/{user}") | |
Call<User> getUser(@Path("user") String userId); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
project.ext.compileSdkVersion = 26 | |
project.ext.buildToolsVersion = "26.0.1" | |
repositories { | |
jcenter() | |
google() | |
} | |
dependencies { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.feature' | |
android { | |
buildToolsVersion '26.0.0' | |
compileSdkVersion 26 | |
dataBinding { | |
enabled = true | |
} |