View build.gradle
def room_version = "2.2.6" | |
implementation "androidx.room:room-runtime:$room_version" | |
annotationProcessor "androidx.room:room-compiler:$room_version" |
View ApplicationData.java
import android.content.Context; | |
import android.content.SharedPreferences; | |
public class ApplicationData { | |
private static final String APP_KEY = "android_course_key"; | |
// save a String value by key | |
public static void setStringValueInSharedPreferences(Context context, String key, String value) { | |
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY, | |
Context.MODE_PRIVATE); |
View MainActivity.java
// manifest add => <uses-permission android:name="android.permission.CAMERA" /> | |
import static android.Manifest.permission.CAMERA; | |
private static final int REQUEST_CODE_CAMERA = 23; | |
if (ContextCompat.checkSelfPermission(StylesActivity.this, CAMERA) != PackageManager.PERMISSION_GRANTED) { | |
// unhappy path | |
ActivityCompat.requestPermissions(StylesActivity.this, new String[]{CAMERA}, REQUEST_CODE_CAMERA); |
View MainActivity.java
private void setupSwipeToRefresh() { | |
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
// Make sure you call swipeRefreshLayout.setRefreshing(false) | |
// once the network request has completed successfully. | |
inbox(); | |
} | |
}); | |
// Configure the refreshing colors |
View EmailAdapter.java
// Clean all elements of the recycler | |
public void clear() { | |
emails.clear(); | |
notifyDataSetChanged(); | |
} | |
// Add a list of items | |
public void addAll(List<Email> list) { | |
emails.addAll(list); | |
notifyDataSetChanged(); |
View main_layout.xml
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/swipeContainer" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/recyclerViewEmails" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> |
View build.gradle
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' |
View MainActivity.java
private void setItemDecorator() { | |
RecyclerView.ItemDecoration itemDecoration = new | |
DividerItemDecoration(this, DividerItemDecoration.VERTICAL); | |
recyclerViewEmails.addItemDecoration(itemDecoration); | |
} |
View EmailDiffCallback.java
public class EmailDiffCallback extends DiffUtil.Callback { | |
private List<Email> oldList; | |
private List<Email> newList; | |
public EmailDiffCallback(List<Email> oldList, List<Email> newList) { | |
this.oldList = oldList; | |
this.newList = newList; | |
} |
View MainActivity.java
// NOT OK | |
void onNewEmailsArrivedNotRecommended(List<Email> newEmails) { | |
emailAdapter.setEmails(newEmails); | |
emailAdapter.notifyDataSetChanged(); | |
} | |
// OK | |
void onNewDataArrivedFastRendering(List<Email> newEmails) { | |
List<Email> oldEmails = emailAdapter.getEmails(); | |
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new EmailDiffCallback(oldEmails, newEmails)); |
NewerOlder