Android Kotlin führt zwei Coroutinen hintereinander ausAndroid

Forum für diejenigen, die für Android programmieren
Guest
 Android Kotlin führt zwei Coroutinen hintereinander aus

Post by Guest »

Ich versuche, zwei Coroutine-Starts hintereinander auszuführen. Wie kann ich das archivieren?
Die ersten Versuche, eine Dauer zu erhalten. Wenn das erfolgreich ist, sollte die Erfassung beendet werden und der zweite Start in meiner Coroutine sollte ausgeführt werden.
Ich weiß nicht, ob das der richtige Weg ist. Aber ich muss zuerst die Dauer ermitteln und dann den zweiten Teil verarbeiten.
Jeder Teil allein funktioniert. aber nicht zusammen, denn sammeln blockiert. Ich habe auch versucht, return@collect auszuführen, aber es funktioniert nicht

Code: Select all

@Singleton
class AddDayReminderNotification(
private val notificationUseCases: NotificationUseCases,
private val dayUseCases: DayUseCases,
private val weekUseCases: WeekUseCases,
private val settingsUseCases: SettingsUseCases
) {
suspend fun addNotification(context: Context?, notification: Notification) {

var neededDuration: Duration = Duration.ZERO
val dayOfWeekIndex = getDayOfWeekLocalized(notification.date)

coroutineScope {

val dayDuration = launch {
weekUseCases.getAllWeeks()
.collect { weekResponse ->
when (weekResponse) {
is Response.Failure -> {
coroutineContext.cancel()
}

is Response.Loading -> {}
is Response.Success -> {
val week =
weekResponse.data?.firstOrNull {
it.week == getWeekOfYearByLocalDate(
notification.date
) && it.year == notification.date.year
}
if (week != null) {
val stringValue = week.weekDailyDurations

val splitString = stringValue.split(":")

val intList: MutableList = mutableListOf()
if (splitString.count() == 7) {

splitString.forEach {
try {
intList.add(it.toInt())
} catch (e: NumberFormatException) {
println("Error: $it is not a valid integer")
coroutineContext.cancel()
}
}
}
neededDuration = intList[dayOfWeekIndex].minutes
} else {
settingsUseCases.getSetting(SettingsIndex.WeekDailyDurations.getIndex())
.collect { response ->
when (response) {
is Response.Failure -> {
coroutineContext.cancel()
}

is Response.Loading -> {}
is Response.Success -> {
val stringValue = response.data?.stringValue

if (stringValue.isNullOrEmpty()) {
coroutineContext.cancel()
} else {

val splitString = stringValue.split(":")
val intList: MutableList  =
mutableListOf()
if (splitString.count() == 7) {

splitString.forEach {
try {
intList.add(it.toInt())
} catch (e: NumberFormatException) {
println("Error: $it is not a valid integer")
coroutineContext.cancel()
}
}
}
neededDuration = intList[dayOfWeekIndex].minutes
}
}
}
}
}
}
}
}
}

val notificationCreator = launch {
if (neededDuration != Duration.ZERO) {
dayUseCases.getAllDays()
.collect { dayResponse ->
when (dayResponse) {
is Response.Failure -> {
coroutineContext.cancel()
}

is Response.Loading -> {}
is Response.Success -> {
if (dayResponse.data != null) {
val foundDay =
dayResponse.data.firstOrNull { it.date == notification.date && it.isActive }
if (foundDay == null) {
notificationUseCases.getAllNotifications()
.collect { notificationResponse ->
when (notificationResponse) {
is Response.Failure -> {
coroutineContext.cancel()
}

is Response.Loading -> {}
is Response.Success -> {
if (notificationResponse.data != null) {
val dayReminderNotification =
notificationResponse.data.firstOrNull { it.date == notification.date &&  it.notificationType == NotificationType.DayUnchangedReminder.value }
if (dayReminderNotification == null) {
notificationUseCases.upsertNotification(
notification
)
context?.let {
DayReminderNotificationService(
it
)
}
?.showNotification(
notification.title + neededDuration.toFloatHours(),
notification.description,
notification.notificationType
)
}
coroutineContext.cancel()
}
}
}
}
}
coroutineContext.cancel()
}
}
}
}
} else {
coroutineContext.cancel()
}
}
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post