Optimize widget callbacks

This commit is contained in:
Rafał Kobyłko 2023-10-21 21:50:04 +02:00
parent a2987d888f
commit 97b546007b
4 changed files with 23 additions and 12 deletions

View File

@ -1,6 +1,6 @@
package com.twofasapp.common.domain package com.twofasapp.common.domain
interface WidgetCallbacks { interface WidgetCallbacks {
fun onServiceChanged() suspend fun onServiceChanged()
fun onServiceDeleted(serviceId: Long) suspend fun onServiceDeleted(serviceId: Long)
} }

View File

@ -129,7 +129,6 @@ internal class ServicesRepositoryImpl(
} }
widgetCallbacks.onServiceChanged() widgetCallbacks.onServiceChanged()
} }
} }

View File

@ -1,18 +1,23 @@
package com.twofasapp.feature.widget.di package com.twofasapp.feature.widget.di
import com.twofasapp.common.domain.WidgetCallbacks
import com.twofasapp.common.di.KoinModule import com.twofasapp.common.di.KoinModule
import com.twofasapp.common.domain.WidgetCallbacks
import com.twofasapp.feature.widget.sync.WidgetCallbacksImpl import com.twofasapp.feature.widget.sync.WidgetCallbacksImpl
import com.twofasapp.feature.widget.ui.settings.WidgetSettingsViewModel import com.twofasapp.feature.widget.ui.settings.WidgetSettingsViewModel
import org.koin.android.ext.koin.androidApplication
import org.koin.androidx.viewmodel.dsl.viewModelOf import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module import org.koin.dsl.module
import javax.inject.Provider
class WidgetModule : KoinModule { class WidgetModule : KoinModule {
override fun provide(): Module = module { override fun provide(): Module = module {
singleOf(::WidgetCallbacksImpl) { bind<WidgetCallbacks>() } single<WidgetCallbacks> {
WidgetCallbacksImpl(
context = androidApplication(),
widgetsRepository = Provider { get() }
)
}
viewModelOf(::WidgetSettingsViewModel) viewModelOf(::WidgetSettingsViewModel)
} }

View File

@ -2,16 +2,23 @@ package com.twofasapp.feature.widget.sync
import android.app.Application import android.app.Application
import com.twofasapp.common.domain.WidgetCallbacks import com.twofasapp.common.domain.WidgetCallbacks
import com.twofasapp.data.services.WidgetsRepository
import javax.inject.Provider
class WidgetCallbacksImpl( class WidgetCallbacksImpl(
private val context: Application private val context: Application,
private val widgetsRepository: Provider<WidgetsRepository>,
) : WidgetCallbacks { ) : WidgetCallbacks {
override fun onServiceChanged() { override suspend fun onServiceChanged() {
if (widgetsRepository.get().getWidgets().list.isNotEmpty()) {
WidgetsUpdateWork.dispatch(context) WidgetsUpdateWork.dispatch(context)
} }
}
override fun onServiceDeleted(serviceId: Long) { override suspend fun onServiceDeleted(serviceId: Long) {
if (widgetsRepository.get().getWidgets().list.isNotEmpty()) {
WidgetsUpdateWork.dispatch(context) WidgetsUpdateWork.dispatch(context)
} }
}
} }