Skip to content

Commit

Permalink
Cleaned code and fixed bug with CoroutineScope
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Imperato committed Feb 22, 2021
1 parent 6fea89f commit 39c8467
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/com/macoev/roomsample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.time.Duration

class MainActivity : AppCompatActivity() {

val model: UserViewModel by viewModels()
private val model: UserViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -38,5 +38,4 @@ class MainActivity : AppCompatActivity() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import kotlinx.coroutines.Job

interface Repository {
fun getAllUsers(): LiveData<List<User>>

fun insert(vararg users: User): Job?

fun delete(user: User): Job?

fun findBy(name: String): LiveData<List<User>>

fun findByIds(vararg ids: Int): LiveData<List<User>>

fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.macoev.roomsample.data.UserDao
import kotlinx.coroutines.*

class UserRepository(private val dao: UserDao) : Repository {
private val scope get() = CoroutineScope(Job() + Dispatchers.IO)
private val scope = CoroutineScope(Job() + Dispatchers.IO)

override fun getAllUsers() = dao.getAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import com.macoev.roomsample.data.User
import com.macoev.roomsample.data.repository.Repository

class UserViewModel(application: Application) : AndroidViewModel(application) {

private var repository: Repository = RepositoryLocator.get(application)

var adapter: UserAdapter = UserAdapter(repository)

fun getAllUsers() = repository.getAllUsers()
Expand All @@ -22,5 +24,4 @@ class UserViewModel(application: Application) : AndroidViewModel(application) {
fun findBy(name: String) = repository.findBy(name)

fun findById(vararg ids: Int) = repository.findByIds(*ids)

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UserViewModelTest {
}

@After
fun removeAllUser_dataLoaded() = runBlockingTest {
fun removeAllUser() = runBlockingTest {
userViewModel.deleteAll()
}
}

0 comments on commit 39c8467

Please sign in to comment.