Skip to content

Commit e0cf3f0

Browse files
committed
Also fixes the mock ServiceLocator
1 parent 515e82c commit e0cf3f0

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

app/src/mock/java/com/example/android/architecture/blueprints/todoapp/ServiceLocator.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.annotation.VisibleForTesting
2020
import androidx.room.Room
2121
import com.example.android.architecture.blueprints.todoapp.data.FakeTasksRemoteDataSource
2222
import com.example.android.architecture.blueprints.todoapp.data.source.DefaultTasksRepository
23+
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
2324
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
2425
import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksLocalDataSource
2526
import com.example.android.architecture.blueprints.todoapp.data.source.local.ToDoDatabase
@@ -44,16 +45,21 @@ object ServiceLocator {
4445
}
4546

4647
private fun createTasksRepository(context: Context): TasksRepository {
47-
database = Room.databaseBuilder(
48+
return DefaultTasksRepository(FakeTasksRemoteDataSource, createTaskLocalDataSource(context))
49+
}
50+
51+
private fun createTaskLocalDataSource(context: Context): TasksDataSource {
52+
val database = database ?: createDataBase(context)
53+
return TasksLocalDataSource(database.taskDao())
54+
}
55+
56+
private fun createDataBase(context: Context): ToDoDatabase {
57+
val result = Room.databaseBuilder(
4858
context.applicationContext,
4959
ToDoDatabase::class.java, "Tasks.db"
50-
)
51-
.build()
52-
53-
return DefaultTasksRepository(
54-
FakeTasksRemoteDataSource,
55-
TasksLocalDataSource(database!!.taskDao())
56-
)
60+
).build()
61+
database = result
62+
return result
5763
}
5864

5965
@VisibleForTesting
@@ -67,6 +73,7 @@ object ServiceLocator {
6773
clearAllTables()
6874
close()
6975
}
76+
database = null
7077
tasksRepository = null
7178
}
7279
}

app/src/prod/java/com/example/android/architecture/blueprints/todoapp/ServiceLocator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ object ServiceLocator {
7474
clearAllTables()
7575
close()
7676
}
77+
database = null
7778
tasksRepository = null
7879
}
7980
}

0 commit comments

Comments
 (0)