diff --git a/bitapp/src/main/java/com/atech/bit/ui/fragments/home/util/GetHomeData.kt b/bitapp/src/main/java/com/atech/bit/ui/fragments/home/util/GetHomeData.kt index 543e674c..0d1423c1 100644 --- a/bitapp/src/main/java/com/atech/bit/ui/fragments/home/util/GetHomeData.kt +++ b/bitapp/src/main/java/com/atech/bit/ui/fragments/home/util/GetHomeData.kt @@ -21,6 +21,7 @@ import com.atech.theme.AdsUnit import com.atech.theme.CardHighlightModel import com.atech.theme.R import com.atech.theme.compareDifferenceInDays +import com.atech.theme.handler import com.atech.theme.isAPI33AndUp import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async @@ -29,6 +30,7 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.launch +import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import java.util.Calendar import java.util.Date @@ -47,7 +49,7 @@ class GetHomeData( list.add(getTopSetting(dataSetForHome.isOnline)) getSyllabus().also { list.addAll(it) } getHoliday().also { list.addAll(it) } - getEvent()?.also { + getEventWithCallback()?.also { list.addAll(it) } getCGPA().also { list.addAll(it) } @@ -205,34 +207,37 @@ class GetHomeData( } else emptyList() } - private suspend fun getEvent(): List? = coroutineScope { - withContext(Dispatchers.IO) { - suspendCoroutine { continuation -> + private suspend fun getEventWithCallback(): List? = coroutineScope { + withContext(Dispatchers.IO + handler) { + suspendCancellableCoroutine { cancellableContinuation -> val list = mutableListOf() - dataSetForHome.firebaseCases.eventWithAttach.invoke { events -> - events.filter { - Date( - System.currentTimeMillis() - ).compareDifferenceInDays(Date(it.created!!)) <= 1 - }.map { event -> - HomeViewModelExr.EventHomeModel( - event.title ?: "", - event.content ?: "", - event.society ?: "", - event.logo_link ?: "", - if (event.attach?.isNotEmpty() == true) event.attach!![0].link - ?: "" else "", - event.path ?: "", - event.created ?: 0L - ) - }.let { events1 -> - if (events1.isNotEmpty()) { - list.add(HomeItems.Title("Events")) - list.add(HomeItems.Event(events1)) - continuation.resumeWith(Result.success(list)) - } else continuation.resumeWith(Result.success(emptyList())) + dataSetForHome + .firebaseCases + .eventWithAttach + .invoke { events -> + events.filter { + Date( + System.currentTimeMillis() + ).compareDifferenceInDays(Date(it.created!!)) <= 1 + }.map { event -> + HomeViewModelExr.EventHomeModel( + event.title ?: "", + event.content ?: "", + event.society ?: "", + event.logo_link ?: "", + if (event.attach?.isNotEmpty() == true) event.attach!![0].link + ?: "" else "", + event.path ?: "", + event.created ?: 0L + ) + }.let { events1 -> + if (events1.isNotEmpty()) { + list.add(HomeItems.Title("Events")) + list.add(HomeItems.Event(events1)) + cancellableContinuation.resumeWith(Result.success(list)) + } else cancellableContinuation.resumeWith(Result.success(emptyList())) + } } - } } } } diff --git a/bitapp/src/main/java/com/atech/bit/ui/fragments/home/viewmodel/HomeViewModel.kt b/bitapp/src/main/java/com/atech/bit/ui/fragments/home/viewmodel/HomeViewModel.kt index 79971054..c9a9d7cc 100644 --- a/bitapp/src/main/java/com/atech/bit/ui/fragments/home/viewmodel/HomeViewModel.kt +++ b/bitapp/src/main/java/com/atech/bit/ui/fragments/home/viewmodel/HomeViewModel.kt @@ -25,6 +25,7 @@ import com.atech.core.utils.DEFAULT_QUERY import com.atech.core.utils.SYLLABUS_SOURCE_DATA import com.atech.core.utils.SharePrefKeys import com.atech.core.utils.fromJSON +import com.atech.core.utils.handler import com.atech.course.sem.adapter.OfflineSyllabusUIMapper import com.atech.course.sem.adapter.OnlineSyllabusUIMapper import com.atech.course.utils.SyllabusEnableModel @@ -85,7 +86,7 @@ class HomeViewModel @Inject constructor( var defPercentage = 7 @OptIn(ExperimentalCoroutinesApi::class) - fun getHomeData() = viewModelScope.async { + fun getHomeData() = viewModelScope.async(handler) { combine( dataStores.asFlow(), isOnline.combine(isPermissionGranted) { isOnline, isPermissionGranted -> diff --git a/core/src/main/java/com/atech/core/firebase/firestore/FirebaseCases.kt b/core/src/main/java/com/atech/core/firebase/firestore/FirebaseCases.kt index 07b5ed7c..2a0e4a45 100644 --- a/core/src/main/java/com/atech/core/firebase/firestore/FirebaseCases.kt +++ b/core/src/main/java/com/atech/core/firebase/firestore/FirebaseCases.kt @@ -1,6 +1,5 @@ package com.atech.core.firebase.firestore -import android.util.Log import com.atech.core.datastore.Cgpa import com.atech.core.firebase.auth.AttendanceUploadModel import com.atech.core.firebase.auth.UserData @@ -89,10 +88,7 @@ class EventWithAttach @Inject constructor( operator fun invoke( listener: (List) -> Unit ) { - db.collection(Db.Event.value).addSnapshotListener { value, error -> - if (error != null) { - listener(emptyList()) - } + db.collection(Db.Event.value).get().addOnSuccessListener { value -> if (value == null) { listener(emptyList()) } @@ -107,6 +103,8 @@ class EventWithAttach @Inject constructor( listener(emptyList()) } } + }.addOnFailureListener { + listener(emptyList()) } } }