This file contains 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 MainActivity extends AppCompatActivity { | |
@Inject MyExample mMyExample; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
TextView dateTextView = findViewById(R.id.tvDate); |
This file contains 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 void onClick(DialogInterface dialog, int which) { | |
String name= String.valueOf(nameEditText.getText()); | |
long date = (new Date()).getTime(); | |
mTodosViewModel.addTodo(name, date); | |
} | |
... |
This file contains 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 void addTodo(String name, long date) { | |
Todo todo = new Todo(); | |
todo.name = name; | |
todo.date = date; | |
mDb.todoModel().insertTodo(todo); | |
} | |
... |
This file contains 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
... | |
@Entity | |
public class Todo { | |
@PrimaryKey(autoGenerate = true) | |
public int id; | |
public String name; | |
public long date; | |
This file contains 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 | |
public interface TodoDao { | |
@Query("SELECT * FROM Todo") | |
LiveData<List<Todo>> findAllTodos(); | |
@Insert(onConflict = REPLACE) | |
void insertTodo(Todo todo); | |
This file contains 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 final LiveData<List<Todo>> todos; | |
private AppDatabase mDb; | |
public TodosViewModel(Application application) { | |
super(application); | |
mDb = AppDatabase.getDatabase(getApplication()); | |
todos = mDb.todoModel().findAllTodos(); | |
} | |
... |
This file contains 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
... | |
mTodosViewModel.todos.observe(this, (todos -> { | |
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() { | |
... | |
}); | |
result.dispatchUpdatesTo(todosAdapter); | |
mTodos = todos; | |
})); | |
... |
This file contains 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 void addTodo(String name, long date) { | |
Todo todo = new Todo(); | |
todo.name = name; | |
todo.date = date; | |
new Thread(() -> { | |
mDb.todoModel().insertTodo(todo); | |
}).start(); | |
} |
This file contains 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
... | |
Room.databaseBuilder(context.getApplicationContext(), | |
AppDatabase.class, "com.larkintuckerllc.room.db") | |
.build(); | |
... |
This file contains 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
... | |
mTodosViewModel.todos.observe(this, (todos -> { | |
new Thread(() -> { | |
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() { | |
... | |
}); | |
self.runOnUiThread(() -> { | |
result.dispatchUpdatesTo(todosAdapter); | |
mTodos = todos; | |
mProgressDialog.hide(); |