I am trying to add data to firestore. and also i want to do this using dagger. but i keep getting this error. can you help me please......
** Default FirebaseApp is not initialized in this process com.example.vvvv. Make sure to call FirebaseApp.initializeApp(Context) first. **
FirestoreREpository
class FirestoreRepository @Inject constructor(private val usersRef:
CollectionReference) {
fun saveSearchResults(userEntities: List<UserEntity>) {
usersRef.add(userEntities).addOnCompleteListener { task ->
when (task.isSuccessful) {
true -> Log.d(ContentValues.TAG, "added:success")
false -> Log.d(ContentValues.TAG, "added:unsuccess")
}
}
}
}
ViewModel
@HiltViewModel
class UserListViewModel @Inject constructor(private val repository: RoomRepository, private
val firestoreRepository: FirestoreRepository) :
ViewModel() {
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>>
get() = _users
var userData: MutableLiveData<List<User>> = MutableLiveData()
fun userSearch(term: String) {
viewModelScope.launch {
loadFromCache(term)
val getPropertiesDeferred =
GithubApi.retrofitService.searchUser(term)
try {
val userEntities: MutableList<UserEntity> = mutableListOf()
val result = getPropertiesDeferred.body()
_users.value = result?.users
result?.users?.forEach {
userEntities.add(
UserEntity(
term = term,
login = it.login,
avatar_url = it.avatar_url
)
)
}
clearSearchResults(term)
updateSearchResults(userEntities, term)
firestoreRepository.saveSearchResults(userEntities) //save
data with firebase
} catch (e: Exception) {
Log.e("userListErr", e.message.toString())
}
}
}
private fun updateSearchResults(userEntities: List<UserEntity>, term:
String) {
viewModelScope.launch(Dispatchers.IO) {
repository.insertSearchResults(userEntities)
loadFromCache(term)
}
}
private fun loadFromCache(term: String) {
viewModelScope.launch(Dispatchers.IO) {
val list = repository.getSearchResults(term)
userData.postValue(list)
}
}
private fun clearSearchResults(term: String) {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteSearchResults(term)
}
}
}
AppModule
@Module
@InstallIn(SingletonComponent::class)
object APPModule {
@Singleton
@Provides
fun getAppDB(context: Application): AppDatabase {
return AppDatabase.getAppDB(context)
}
@Singleton
@Provides
fun getDao(appDB: AppDatabase): AppDao {
return appDB.getDAO()
}
@Provides
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()
@Provides
fun provideUsersRef(db: FirebaseFirestore) = db.collection("users")
}