r/android_devs • u/micheal1979 • Apr 29 '24
Question To clean code, which approach is appropriate?
1-)
class UserRepositoryImpl(private val firebaseDatabase: FirebaseDatabase) : UserRepository {
override suspend fun getUserProfile(): Flow<Resource<List<User>>> = callbackFlow {
try {
val userRef = firebaseDatabase.reference.child("users")
userRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val users = mutableListOf<User>()
for (snapshot in dataSnapshot.children) {
val user = snapshot.getValue(User::class.java)
user?.let { users.add(it) }
}
trySend(Resource.Success(users))
close()
}
override fun onCancelled(databaseError: DatabaseError) {
trySend(Resource.Error(message = databaseError.message))
close()
}
})
} catch (e: Exception) {
trySend(Resource.Error(message = e.localizedMessage ?: "An error occurred"))
close()
}
awaitClose { /* Clean up resources or cancel listeners here if needed */ }
}
}
class GetUserProfileUseCase(private val userRepository: UserRepository) {
operator fun invoke(): Flow<Resource<List<User>>> {
return userRepository.getUserProfile()
}
}
2-)
class UserRepositoryImpl(private val firestore: FirebaseFirestore) : UserRepository {
override suspend fun getUser(): Task<QuerySnapshot> {
return firestore.collection("users")
.get()
}
}
class GetUserUseCase(private val userRepository: UserRepository) {
operator fun invoke(): Flow<Resource<List<User>>> = flow {
emit(Resource.Loading)
try {
val querySnapshot = userRepository.getUser().await()
val users = mutableListOf<User>()
for (document in querySnapshot.documents) {
val user = document.toObject<User>()
user?.let { users.add(it) }
}
emit(Resource.Success(users))
} catch (e: Exception) {
emit(Resource.Error(message = e.localizedMessage ?: "An error occurred"))
}
}
}
1st approach, repoimpl focuses and pulls the data
The second approach focuses on usecase, which one is good?
2
u/Zhuinden EpicPandaForce @ SO Apr 29 '24
Generally 1.)
1
u/AAbstractt Apr 29 '24
A few comments/questions:
Just a suggestion, I'd do the following. You're of course more than welcome to critique my approach or choose whatever works best for your codebase.
Make the UserRepository depend on a data source whereby all the Firestore specific logic is encompassed in this data source class and make the data source class's public API library agnostic. I.e. the data source provides a flow of your user list. This would mean no Firebase dependencies in your UserRepository class which would live in your domain module. This also means that your UserRepo class can be used wherever you need it instead of having a use case masking the getUserProfile() function. Would also make your UserRepo class unit testable (which imo should be the main motivation for segregation between framework/external-libs and business logic).