Skip to content

Commit c146e01

Browse files
authored
Merge pull request android#394 from subhasha1/dev-todo-mvp-kotlin
Minor refactors
2 parents a477b05 + e54ae82 commit c146e01

File tree

9 files changed

+40
-39
lines changed

9 files changed

+40
-39
lines changed

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskActivity.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ class AddEditTaskActivity : AppCompatActivity() {
4848
replaceFragmentInActivity(it, R.id.contentFrame)
4949
}
5050

51-
var shouldLoadDataFromRepo = true
51+
val shouldLoadDataFromRepo =
52+
// Prevent the presenter from loading data from the repository if this is a config change.
53+
// Data might not have loaded when the config change happen, so we saved the state.
54+
savedInstanceState?.getBoolean(SHOULD_LOAD_DATA_FROM_REPO_KEY) ?: true
5255

53-
// Prevent the presenter from loading data from the repository if this is a config change.
54-
if (savedInstanceState != null) {
55-
// Data might not have loaded when the config change happen, so we saved the state.
56-
shouldLoadDataFromRepo = savedInstanceState.getBoolean(SHOULD_LOAD_DATA_FROM_REPO_KEY)
57-
}
5856

5957
// Create the presenter
6058
addEditTaskPresenter = AddEditTaskPresenter(taskId,

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class AddEditTaskFragment : Fragment(), AddEditTaskContract.View {
6060
savedInstanceState: Bundle?): View? {
6161
val root = inflater.inflate(R.layout.addtask_frag, container, false)
6262
with(root) {
63-
title = findViewById<TextView>(R.id.add_task_title)
64-
description = findViewById<TextView>(R.id.add_task_description)
63+
title = findViewById(R.id.add_task_title)
64+
description = findViewById(R.id.add_task_description)
6565
}
6666
setHasOptionsMenu(true)
6767
return root

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.example.android.architecture.blueprints.todoapp.data.source.TasksData
2828
*
2929
* @param addTaskView the add/edit view
3030
*
31-
* @param shouldLoadDataFromRepo whether data needs to be loaded or not (for config changes)
31+
* @param isDataMissing whether data needs to be loaded or not (for config changes)
3232
*/
3333
class AddEditTaskPresenter(
3434
private val taskId: String?,

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/TasksRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TasksRepository(
4848
* available first.
4949
*
5050
*
51-
* Note: [LoadTasksCallback.onDataNotAvailable] is fired if all data sources fail to
51+
* Note: [TasksDataSource.LoadTasksCallback.onDataNotAvailable] is fired if all data sources fail to
5252
* get the data.
5353
*/
5454
override fun getTasks(callback: TasksDataSource.LoadTasksCallback) {
@@ -128,7 +128,7 @@ class TasksRepository(
128128
* uses the network data source. This is done to simplify the sample.
129129
*
130130
*
131-
* Note: [GetTaskCallback.onDataNotAvailable] is fired if both data sources fail to
131+
* Note: [TasksDataSource.GetTaskCallback.onDataNotAvailable] is fired if both data sources fail to
132132
* get the data.
133133
*/
134134
override fun getTask(taskId: String, callback: TasksDataSource.GetTaskCallback) {

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/local/TasksLocalDataSource.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ class TasksLocalDataSource private constructor(context: Context) : TasksDataSour
3333
private val dbHelper: TasksDbHelper = TasksDbHelper(context)
3434

3535
/**
36-
* Note: [LoadTasksCallback.onDataNotAvailable] is fired if the database doesn't exist
36+
* Note: [TasksDataSource.LoadTasksCallback.onDataNotAvailable] is fired if the database doesn't exist
3737
* or the table is empty.
3838
*/
3939
override fun getTasks(callback: TasksDataSource.LoadTasksCallback) {
40-
val tasks = ArrayList<Task>()
4140
val db = dbHelper.readableDatabase
4241

4342
val projection = arrayOf(COLUMN_NAME_ENTRY_ID, COLUMN_NAME_TITLE,
@@ -46,6 +45,7 @@ class TasksLocalDataSource private constructor(context: Context) : TasksDataSour
4645
val cursor = db.query(
4746
TABLE_NAME, projection, null, null, null, null, null)
4847

48+
val tasks = ArrayList<Task>()
4949
with(cursor) {
5050
while (moveToNext()) {
5151
val itemId = getString(getColumnIndexOrThrow(COLUMN_NAME_ENTRY_ID))
@@ -56,19 +56,20 @@ class TasksLocalDataSource private constructor(context: Context) : TasksDataSour
5656
}
5757
tasks.add(task)
5858
}
59-
if (tasks.isNotEmpty()) {
60-
callback.onTasksLoaded(tasks)
61-
} else {
62-
// This will be called if the table is new or just empty.
63-
callback.onDataNotAvailable()
64-
}
6559
close()
6660
}
6761
db.close()
62+
63+
if (tasks.isNotEmpty()) {
64+
callback.onTasksLoaded(tasks)
65+
} else {
66+
// This will be called if the table is new or just empty.
67+
callback.onDataNotAvailable()
68+
}
6869
}
6970

7071
/**
71-
* Note: [GetTaskCallback.onDataNotAvailable] is fired if the [Task] isn't
72+
* Note: [TasksDataSource.GetTaskCallback.onDataNotAvailable] is fired if the [Task] isn't
7273
* found.
7374
*/
7475
override fun getTask(taskId: String, callback: TasksDataSource.GetTaskCallback) {
@@ -80,22 +81,23 @@ class TasksLocalDataSource private constructor(context: Context) : TasksDataSour
8081
val cursor = db.query(
8182
TABLE_NAME, projection, "$COLUMN_NAME_ENTRY_ID LIKE ?", arrayOf(taskId), null,
8283
null, null)
83-
84+
var task: Task? = null
8485
with(cursor) {
8586
if (moveToFirst()) {
8687
val itemId = getString(getColumnIndexOrThrow(COLUMN_NAME_ENTRY_ID))
8788
val title = getString(getColumnIndexOrThrow(COLUMN_NAME_TITLE))
8889
val description = getString(getColumnIndexOrThrow(COLUMN_NAME_DESCRIPTION))
89-
val task = Task(title, description, itemId).apply {
90+
task = Task(title, description, itemId).apply {
9091
isCompleted = getInt(getColumnIndexOrThrow(COLUMN_NAME_COMPLETED)) == 1
9192
}
92-
callback.onTaskLoaded(task)
93-
} else {
94-
callback.onDataNotAvailable()
9593
}
9694
close()
9795
}
9896
db.close()
97+
98+
task?.also { callback.onTaskLoaded(it) }
99+
// This will be called if the table is new or just empty.
100+
?: callback.onDataNotAvailable()
99101
}
100102

101103
override fun saveTask(task: Task) {

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/remote/TasksRemoteDataSource.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object TasksRemoteDataSource : TasksDataSource {
4040
}
4141

4242
/**
43-
* Note: [LoadTasksCallback.onDataNotAvailable] is never fired. In a real remote data
43+
* Note: [TasksDataSource.LoadTasksCallback.onDataNotAvailable] is never fired. In a real remote data
4444
* source implementation, this would be fired if the server can't be contacted or the server
4545
* returns an error.
4646
*/
@@ -53,7 +53,7 @@ object TasksRemoteDataSource : TasksDataSource {
5353
}
5454

5555
/**
56-
* Note: [GetTaskCallback.onDataNotAvailable] is never fired. In a real remote data
56+
* Note: [TasksDataSource.GetTaskCallback.onDataNotAvailable] is never fired. In a real remote data
5757
* source implementation, this would be fired if the server can't be contacted or the server
5858
* returns an error.
5959
*/

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class StatisticsFragment : Fragment(), StatisticsContract.View {
3939
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
4040
savedInstanceState: Bundle?): View? {
4141
val root = inflater.inflate(R.layout.statistics_frag, container, false)
42-
statisticsTV = root.findViewById<TextView>(R.id.statistics)
42+
statisticsTV = root.findViewById(R.id.statistics)
4343
return root
4444
}
4545

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class TaskDetailFragment : Fragment(), TaskDetailContract.View {
6060
val root = inflater.inflate(R.layout.taskdetail_frag, container, false)
6161
setHasOptionsMenu(true)
6262
with(root) {
63-
detailTitle = findViewById<TextView>(R.id.task_detail_title)
64-
detailDescription = findViewById<TextView>(R.id.task_detail_description)
65-
detailCompleteStatus = findViewById<CheckBox>(R.id.task_detail_complete)
63+
detailTitle = findViewById(R.id.task_detail_title)
64+
detailDescription = findViewById(R.id.task_detail_description)
65+
detailCompleteStatus = findViewById(R.id.task_detail_complete)
6666
}
6767

6868
// Set up floating action button

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksFragment.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TasksFragment : Fragment(), TasksContract.View {
7676
}
7777
}
7878

79-
private val listAdapter = TasksAdapter(ArrayList<Task>(0), itemListener)
79+
private val listAdapter = TasksAdapter(ArrayList(0), itemListener)
8080

8181
override fun onResume() {
8282
super.onResume()
@@ -107,13 +107,13 @@ class TasksFragment : Fragment(), TasksContract.View {
107107
setOnRefreshListener { presenter.loadTasks(false) }
108108
}
109109

110-
filteringLabelView = findViewById<TextView>(R.id.filteringLabel)
111-
tasksView = findViewById<LinearLayout>(R.id.tasksLL)
110+
filteringLabelView = findViewById(R.id.filteringLabel)
111+
tasksView = findViewById(R.id.tasksLL)
112112

113113
// Set up no tasks view
114114
noTasksView = findViewById(R.id.noTasks)
115-
noTaskIcon = findViewById<ImageView>(R.id.noTasksIcon)
116-
noTaskMainView = findViewById<TextView>(R.id.noTasksMain)
115+
noTaskIcon = findViewById(R.id.noTasksIcon)
116+
noTaskMainView = findViewById(R.id.noTasksMain)
117117
noTaskAddView = (findViewById<TextView>(R.id.noTasksAdd)).also {
118118
it.setOnClickListener { showAddTask() }
119119
}
@@ -193,7 +193,7 @@ class TasksFragment : Fragment(), TasksContract.View {
193193
noTasksView.visibility = View.VISIBLE
194194

195195
noTaskMainView.text = mainText
196-
noTaskIcon.setImageDrawable(resources.getDrawable(iconRes))
196+
noTaskIcon.setImageResource(iconRes)
197197
noTaskAddView.visibility = if (showAddView) View.VISIBLE else View.GONE
198198
}
199199

@@ -270,10 +270,10 @@ class TasksFragment : Fragment(), TasksContract.View {
270270
with(rowView.findViewById<CheckBox>(R.id.complete)) {
271271
// Active/completed task UI
272272
isChecked = task.isCompleted
273-
rowView.setBackgroundDrawable(viewGroup.context.resources.getDrawable(
273+
val rowViewBackground =
274274
if (task.isCompleted) R.drawable.list_completed_touch_feedback
275275
else R.drawable.touch_feedback
276-
))
276+
rowView.setBackgroundResource(rowViewBackground)
277277
setOnClickListener {
278278
if (!task.isCompleted) {
279279
itemListener.onCompleteTaskClick(task)
@@ -297,6 +297,7 @@ class TasksFragment : Fragment(), TasksContract.View {
297297
}
298298

299299
companion object {
300+
300301
fun newInstance() = TasksFragment()
301302
}
302303

0 commit comments

Comments
 (0)