From f8892eb09d1e9f025e5f634318751bbc5c3b7325 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 24 May 2025 18:55:17 -0300 Subject: [PATCH 1/7] build: add core-background-work module --- core-background-work/.gitignore | 1 + core-background-work/build.gradle.kts | 16 ++++++++++++++++ core-background-work/consumer-rules.pro | 0 core-background-work/proguard-rules.pro | 21 +++++++++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 39 insertions(+) create mode 100644 core-background-work/.gitignore create mode 100644 core-background-work/build.gradle.kts create mode 100644 core-background-work/consumer-rules.pro create mode 100644 core-background-work/proguard-rules.pro diff --git a/core-background-work/.gitignore b/core-background-work/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/core-background-work/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/core-background-work/build.gradle.kts b/core-background-work/build.gradle.kts new file mode 100644 index 00000000..152f9ccd --- /dev/null +++ b/core-background-work/build.gradle.kts @@ -0,0 +1,16 @@ +@file:Suppress("UnstableApiUsage") + +plugins { + id("com.streamplayer.kmp-library") +} + +kotlin { + sourceSets { + sourceSets { + commonMain.dependencies { + implementation(projects.coreShared) + implementation(projects.coreSharedUi) + } + } + } +} \ No newline at end of file diff --git a/core-background-work/consumer-rules.pro b/core-background-work/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/core-background-work/proguard-rules.pro b/core-background-work/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/core-background-work/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index b965577d..68208411 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -38,6 +38,7 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") include(":feature-news") include(":core-camera-gallery") include(":core-permission") +include(":core-background-work") kover { enableCoverage() From 6a059946b93dae8a887b10a8847a0cfda889ba6d Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 24 May 2025 20:49:42 -0300 Subject: [PATCH 2/7] feat: add work manager android --- composeApp/build.gradle.kts | 1 + .../presentation/CustomApplication.kt | 2 ++ .../di/AppModule.kt | 4 +-- core-background-work/build.gradle.kts | 10 ++++++-- .../core_background_work/worker/SyncWorker.kt | 25 +++++++++++++++++++ .../worker/WorkScheduler.kt | 21 ++++++++++++++++ .../core_background_work/SyncManager.kt | 10 ++++++++ .../core_background_work/di/SyncModule.kt | 10 ++++++++ gradle/libs.versions.toml | 4 +++ 9 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt create mode 100644 core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/WorkScheduler.kt create mode 100644 core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt create mode 100644 core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 6f25013b..c38b9252 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -27,6 +27,7 @@ kotlin { implementation(projects.coreNavigation) implementation(projects.coreNetworking) implementation(projects.coreLocalStorage) + implementation(projects.coreBackgroundWork) implementation(libs.navigation.compose) diff --git a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt index 8cbc1494..3aa85d57 100644 --- a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt +++ b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt @@ -1,6 +1,7 @@ package com.codandotv.streamplayerapp.presentation import android.app.Application +import com.codandotv.streamplayerapp.core_background_work.worker.WorkScheduler import com.codandotv.streamplayerapp.di.AppModule import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin @@ -13,5 +14,6 @@ class CustomApplication : Application() { androidContext(this@CustomApplication.applicationContext) modules(AppModule.list) } + WorkScheduler.scheduleSync(this) } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt index ee2abc42..faf0723b 100644 --- a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt +++ b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt @@ -1,6 +1,6 @@ package com.codandotv.streamplayerapp.di -import PermissionsModule +import com.codandotv.streamplayerapp.core_background_work.di.SyncModule import com.codandotv.streamplayerapp.core_local_storage.di.LocalStorageModule import com.codandotv.streamplayerapp.core_networking.di.NetworkModule import com.codandotv.streamplayerapp.core_shared.qualifier.QualifierDispatcherIO @@ -12,5 +12,5 @@ object AppModule { private val module = module { single(QualifierDispatcherIO) { Dispatchers.IO } } - val list = module + NetworkModule.module + LocalStorageModule.module + val list = module + NetworkModule.module + LocalStorageModule.module + SyncModule.module } \ No newline at end of file diff --git a/core-background-work/build.gradle.kts b/core-background-work/build.gradle.kts index 152f9ccd..080c942d 100644 --- a/core-background-work/build.gradle.kts +++ b/core-background-work/build.gradle.kts @@ -2,14 +2,20 @@ plugins { id("com.streamplayer.kmp-library") + id("com.google.devtools.ksp") } kotlin { sourceSets { sourceSets { + androidMain.dependencies { + implementation(libs.work.runtime) + } + commonMain.dependencies { - implementation(projects.coreShared) - implementation(projects.coreSharedUi) + implementation(libs.kotlin.stdlib) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.koin.core) } } } diff --git a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt new file mode 100644 index 00000000..c9620b30 --- /dev/null +++ b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt @@ -0,0 +1,25 @@ +package com.codandotv.streamplayerapp.core_background_work.worker + +import android.content.Context +import androidx.work.CoroutineWorker +import androidx.work.WorkerParameters +import com.codandotv.streamplayerapp.core_background_work.SyncManager +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject + +class SyncWorker( + context: Context, + params: WorkerParameters +) : CoroutineWorker(context, params), KoinComponent { + + private val syncManager: SyncManager by inject() + + override suspend fun doWork(): Result { + return try { + syncManager.syncData() + Result.success() + } catch (e: Exception) { + Result.retry() + } + } +} diff --git a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/WorkScheduler.kt b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/WorkScheduler.kt new file mode 100644 index 00000000..22bb8e72 --- /dev/null +++ b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/WorkScheduler.kt @@ -0,0 +1,21 @@ +package com.codandotv.streamplayerapp.core_background_work.worker + +import android.content.Context +import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.PeriodicWorkRequestBuilder +import androidx.work.WorkManager +import java.util.concurrent.TimeUnit + +object WorkScheduler { + fun scheduleSync(context: Context) { + val workRequest = PeriodicWorkRequestBuilder( + 15, TimeUnit.MINUTES + ).build() + + WorkManager.getInstance(context).enqueueUniquePeriodicWork( + "SyncWorker", + ExistingPeriodicWorkPolicy.KEEP, + workRequest + ) + } +} diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt new file mode 100644 index 00000000..02e90fd0 --- /dev/null +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt @@ -0,0 +1,10 @@ +package com.codandotv.streamplayerapp.core_background_work +import kotlinx.coroutines.delay + +class SyncManager { + suspend fun syncData() { + println("SyncManager: Sincronizando dados de teste...") + delay(2000) + println("SyncManager: Dados sincronizados com sucesso!") + } +} \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt new file mode 100644 index 00000000..78780770 --- /dev/null +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt @@ -0,0 +1,10 @@ +package com.codandotv.streamplayerapp.core_background_work.di + +import com.codandotv.streamplayerapp.core_background_work.SyncManager +import org.koin.dsl.module + +object SyncModule { + val module = module { + single { SyncManager() } + } +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ed404ca3..bf9bcdf8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -38,6 +38,8 @@ accompanist = "0.31.3-beta" activity-compose = "1.8.2" popcorngp = "3.1.2" +work-runtime = "2.10.1" + #Firebase google-services = "4.4.2" firebase-bom = "33.13.0" @@ -121,6 +123,8 @@ coroutines_test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines- popcorn_guineapig = { group = "io.github.codandotv", name = "popcornguineapig", version.ref = "popcorngp" } +work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref = "work-runtime" } + [bundles] test = ["junit", "mockk", "mockk_android", "viewmodel_test", "koin_test", "coroutines_test"] test_multiplatform = ["kotlin_test", "kotlin_test_common", "coroutines_test",] From 04ff40a5730db065ffd5a047b081a9844babfa28 Mon Sep 17 00:00:00 2001 From: junior Date: Sat, 24 May 2025 23:04:35 -0300 Subject: [PATCH 3/7] feat: add bg tasks iOS --- .../SyncBridge.kt | 25 ++++++++ .../core_background_work/SyncManager.kt | 4 +- iosApp/iosApp/AppDelegate.swift | 57 +++++++++++++++++++ iosApp/iosApp/Info.plist | 11 ++++ iosApp/iosApp/iOSApp.swift | 5 ++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/SyncBridge.kt create mode 100644 iosApp/iosApp/AppDelegate.swift diff --git a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/SyncBridge.kt b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/SyncBridge.kt new file mode 100644 index 00000000..0d3a785c --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/SyncBridge.kt @@ -0,0 +1,25 @@ +package com.codandotv.streamplayerapp + +import com.codandotv.streamplayerapp.core_background_work.SyncManager +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import org.koin.core.context.startKoin +import org.koin.dsl.module +import org.koin.mp.KoinPlatform.getKoin + +object SyncBridge { + suspend fun syncData() { + getKoin().get().syncData() + } + + fun syncData(completionHandler: () -> Unit) { + CoroutineScope(Dispatchers.Default).launch { + try { + syncData() + } finally { + completionHandler() + } + } + } +} diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt index 02e90fd0..c836d611 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt @@ -3,8 +3,8 @@ import kotlinx.coroutines.delay class SyncManager { suspend fun syncData() { - println("SyncManager: Sincronizando dados de teste...") + println("SyncManager: BGTestes Sincronizando dados de teste...") delay(2000) - println("SyncManager: Dados sincronizados com sucesso!") + println("SyncManager: BGTestes Dados sincronizados com sucesso!") } } \ No newline at end of file diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift new file mode 100644 index 00000000..3137a9d8 --- /dev/null +++ b/iosApp/iosApp/AppDelegate.swift @@ -0,0 +1,57 @@ +import UIKit +import BackgroundTasks +import streamplayerapp + +class AppDelegate: NSObject, UIApplicationDelegate { + func application(_ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { + + BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.codandotv.streamplayerapp", using: nil) { task in + self.handleAppRefresh(task: task as! BGAppRefreshTask) + } + print("BGTestes Task registrada!") + + scheduleAppRefresh() + return true + } + + func scheduleAppRefresh() { + let request = BGAppRefreshTaskRequest(identifier: "com.codandotv.streamplayerapp") +// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // 15 minutos + request.earliestBeginDate = Date() // agendar para agora + + + do { + try BGTaskScheduler.shared.submit(request) + print("BGTestes tarefa agendada com sucesso para agora: \(request.earliestBeginDate!)") + } catch { + print("BGTestes falha ao agendar tarefa: \(error.localizedDescription)") + } + + + } + + func handleAppRefresh(task: BGAppRefreshTask) { + print("BGTestes handleAppRefresh foi chamado!") + + scheduleAppRefresh() + + let queue = OperationQueue() + queue.maxConcurrentOperationCount = 1 + + let operation = BlockOperation { + print("BGTestes Executando operação de sync...") + SyncBridge.shared.syncData { + print("BGTestes Sincronizado no IOS") + task.setTaskCompleted(success: true) + } + } + + task.expirationHandler = { + print("BGTestes Tarefa expirada.") + queue.cancelAllOperations() + } + + queue.addOperation(operation) + } +} diff --git a/iosApp/iosApp/Info.plist b/iosApp/iosApp/Info.plist index b1ada5c0..6a1ff3fa 100644 --- a/iosApp/iosApp/Info.plist +++ b/iosApp/iosApp/Info.plist @@ -27,6 +27,17 @@ UIApplicationSupportsMultipleScenes + UIBackgroundModes + + remote-notification + processing + fetch + + + BGTaskSchedulerPermittedIdentifiers + + com.codandotv.streamplayerapp + UILaunchScreen UIRequiredDeviceCapabilities diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index c4472c1e..320f51b7 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -4,10 +4,15 @@ import FirebaseCore @main struct iOSApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate init() { KoinIosHelper().doInitKoin(lottieViewProvider: LottieViewProviderImpl()) FirebaseApp.configure() + + SyncBridge.shared.syncData { + print("BG Testes Data sync completed") + } } var body: some Scene { From 8d7e8c0a5a12c5b702c15f1539aa6a7c5eacd007 Mon Sep 17 00:00:00 2001 From: junior Date: Sun, 25 May 2025 12:00:54 -0300 Subject: [PATCH 4/7] refactor: switching BGAppRefreshTaskRequest to BGProcessingTask --- .../commonizedNativeDistributionLocation.txt | 2 +- iosApp/iosApp.xcodeproj/project.pbxproj | 8 ++++++-- iosApp/iosApp/AppDelegate.swift | 14 +++++++------- iosApp/iosApp/iOSApp.swift | 8 ++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/build/kotlin/commonizedNativeDistributionLocation.txt b/build/kotlin/commonizedNativeDistributionLocation.txt index aac56bbe..9f81b5aa 100644 --- a/build/kotlin/commonizedNativeDistributionLocation.txt +++ b/build/kotlin/commonizedNativeDistributionLocation.txt @@ -1 +1 @@ -/Users/pedroalvarez/.konan/kotlin-native-prebuilt-macos-aarch64-2.1.10/klib/commonized/2.1.10 \ No newline at end of file +/Users/junior/.konan/kotlin-native-prebuilt-macos-aarch64-2.1.10/klib/commonized/2.1.10 \ No newline at end of file diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index aa02a0c6..1dde3c3b 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -39,6 +39,7 @@ 5BAD97872D7CCDDA00D93987 /* Lottie.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BAD97862D7CCDDA00D93987 /* Lottie.swift */; }; 5BAD97892D7CCEA700D93987 /* Lottie in Frameworks */ = {isa = PBXBuildFile; productRef = 5BAD97882D7CCEA700D93987 /* Lottie */; }; 7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; }; + 7908F1992DE366F100D2455C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7908F1982DE366EC00D2455C /* AppDelegate.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -64,6 +65,7 @@ 7555FF7B242A565900829871 /* KotlinProject.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KotlinProject.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 7908F1982DE366EC00D2455C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; AB3632DC29227652001CCB65 /* Config.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = ""; }; /* End PBXFileReference section */ @@ -156,6 +158,7 @@ 7555FF7D242A565900829871 /* iosApp */ = { isa = PBXGroup; children = ( + 7908F1982DE366EC00D2455C /* AppDelegate.swift */, 3FD330BB2DD2CFC300633ECC /* GoogleService-Info.plist */, 5BAD97862D7CCDDA00D93987 /* Lottie.swift */, 5BA9B50C2DA887EE00EF12ED /* iosApp.xctestplan */, @@ -340,6 +343,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 7908F1992DE366F100D2455C /* AppDelegate.swift in Sources */, 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */, 7555FF83242A565900829871 /* ContentView.swift in Sources */, 5BAD97872D7CCDDA00D93987 /* Lottie.swift in Sources */, @@ -537,7 +541,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\""; - DEVELOPMENT_TEAM = "${TEAM_ID}"; + DEVELOPMENT_TEAM = 4U9U2835SF; ENABLE_PREVIEWS = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -565,7 +569,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\""; - DEVELOPMENT_TEAM = "${TEAM_ID}"; + DEVELOPMENT_TEAM = 4U9U2835SF; ENABLE_PREVIEWS = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift index 3137a9d8..2f9c0652 100644 --- a/iosApp/iosApp/AppDelegate.swift +++ b/iosApp/iosApp/AppDelegate.swift @@ -7,7 +7,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.codandotv.streamplayerapp", using: nil) { task in - self.handleAppRefresh(task: task as! BGAppRefreshTask) + self.handleAppRefresh(task: task as! BGProcessingTask) } print("BGTestes Task registrada!") @@ -16,14 +16,14 @@ class AppDelegate: NSObject, UIApplicationDelegate { } func scheduleAppRefresh() { - let request = BGAppRefreshTaskRequest(identifier: "com.codandotv.streamplayerapp") -// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // 15 minutos - request.earliestBeginDate = Date() // agendar para agora - + let request = BGProcessingTaskRequest(identifier: "com.codandotv.streamplayerapp") + request.requiresNetworkConnectivity = false + request.requiresExternalPower = false + request.earliestBeginDate = Date(timeIntervalSinceNow: 5 * 60) // 5 minutos do { try BGTaskScheduler.shared.submit(request) - print("BGTestes tarefa agendada com sucesso para agora: \(request.earliestBeginDate!)") + print("BGTestes tarefa agendada com sucesso para: \(request.earliestBeginDate!)") } catch { print("BGTestes falha ao agendar tarefa: \(error.localizedDescription)") } @@ -31,7 +31,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { } - func handleAppRefresh(task: BGAppRefreshTask) { + func handleAppRefresh(task: BGProcessingTask) { print("BGTestes handleAppRefresh foi chamado!") scheduleAppRefresh() diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index 320f51b7..59bf5cb8 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -5,16 +5,12 @@ import FirebaseCore @main struct iOSApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate - + init() { KoinIosHelper().doInitKoin(lottieViewProvider: LottieViewProviderImpl()) FirebaseApp.configure() - - SyncBridge.shared.syncData { - print("BG Testes Data sync completed") - } } - + var body: some Scene { WindowGroup { ContentView() From bbcff8b6413ebde9f89a3d7544536d1457d0f63c Mon Sep 17 00:00:00 2001 From: junior Date: Sun, 25 May 2025 20:35:36 -0300 Subject: [PATCH 5/7] feat: add notification on syncmanager --- .../java/extensions/KotlinMultiPlatformExt.kt | 2 + composeApp/build.gradle.kts | 1 + .../presentation/CustomApplication.kt | 13 ++++++ .../presentation/MainActivity.kt | 7 ++++ .../NotifierHelper.kt | 40 +++++++++++++++++++ core-background-work/build.gradle.kts | 17 ++++---- .../NotifierHelperAndroid.kt | 40 +++++++++++++++++++ .../core_background_work/SyncManager.kt | 1 + gradle/libs.versions.toml | 3 ++ .../xcshareddata/xcschemes/iosApp.xcscheme | 2 +- iosApp/iosApp/AppDelegate.swift | 9 +++++ 11 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt create mode 100644 core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt diff --git a/build-logic/src/main/java/extensions/KotlinMultiPlatformExt.kt b/build-logic/src/main/java/extensions/KotlinMultiPlatformExt.kt index bd263ef0..ae29329c 100644 --- a/build-logic/src/main/java/extensions/KotlinMultiPlatformExt.kt +++ b/build-logic/src/main/java/extensions/KotlinMultiPlatformExt.kt @@ -12,6 +12,8 @@ fun KotlinMultiplatformExtension.iosTarget() { iosTarget.binaries.framework { baseName = Config.appName isStatic = true + + export("io.github.mirzemehdi:kmpnotifier:1.5.1") } } } diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index c38b9252..bb2dfced 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -36,6 +36,7 @@ kotlin { implementation(compose.components.resources) implementation(libs.koin.core) + api(libs.kmpnotifier) } } } \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt index 3aa85d57..a3911710 100644 --- a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt +++ b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/CustomApplication.kt @@ -1,8 +1,11 @@ package com.codandotv.streamplayerapp.presentation import android.app.Application +import com.codandotv.streamplayerapp.core.shared.ui.R import com.codandotv.streamplayerapp.core_background_work.worker.WorkScheduler import com.codandotv.streamplayerapp.di.AppModule +import com.mmk.kmpnotifier.notification.NotifierManager +import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin @@ -15,5 +18,15 @@ class CustomApplication : Application() { modules(AppModule.list) } WorkScheduler.scheduleSync(this) + initializeNotification() + } + + fun initializeNotification() { + NotifierManager.initialize( + configuration = NotificationPlatformConfiguration.Android( + notificationIconResId = R.mipmap.ic_netflix, + showPushNotification = true, + ) + ) } } \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt index 83efb537..0ec1d3a9 100644 --- a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt +++ b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt @@ -6,13 +6,20 @@ import androidx.activity.compose.setContent import com.codandotv.streamplayerapp.StreamPlayerApp import com.google.firebase.Firebase import com.google.firebase.initialize +import com.mmk.kmpnotifier.permission.permissionUtil class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Firebase.initialize(this) + requestNotificationPermission() setContent { StreamPlayerApp() } } + + private fun requestNotificationPermission() { + val permissionUtil by permissionUtil() + permissionUtil.askNotificationPermission() + } } diff --git a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt new file mode 100644 index 00000000..f01e5d17 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt @@ -0,0 +1,40 @@ +package com.codandotv.streamplayerapp + + +import com.mmk.kmpnotifier.notification.NotificationImage +import com.mmk.kmpnotifier.notification.Notifier +import com.mmk.kmpnotifier.notification.NotifierManager +import kotlin.random.Random + +object NotifierHelper { + fun showSimpleNotification( + title: String = "Notificação Simples", + body: String = "Corpo da Notificação Simples", + ) { + val notifier = NotifierManager.getLocalNotifier() + notifier.notify { + id= Random.nextInt(0, Int.MAX_VALUE) + this.title = title + this.body = body + payloadData = mapOf( + Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", + "extraKey" to "randomValue" + ) + } + } + + + fun showTestNotificationRegister() { + val notifier = NotifierManager.getLocalNotifier() + notifier.notify { + id= Random.nextInt(0, Int.MAX_VALUE) + title = "Task disparada" + body = "Corpo da task disparada Body message from KMPNotifier" + payloadData = mapOf( + Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", + "extraKey" to "randomValue" + ) + image = NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") + } + } +} \ No newline at end of file diff --git a/core-background-work/build.gradle.kts b/core-background-work/build.gradle.kts index 080c942d..a3361696 100644 --- a/core-background-work/build.gradle.kts +++ b/core-background-work/build.gradle.kts @@ -7,16 +7,15 @@ plugins { kotlin { sourceSets { - sourceSets { - androidMain.dependencies { - implementation(libs.work.runtime) - } + androidMain.dependencies { + implementation(libs.work.runtime) + } - commonMain.dependencies { - implementation(libs.kotlin.stdlib) - implementation(libs.kotlinx.coroutines.core) - implementation(libs.koin.core) - } + commonMain.dependencies { + implementation(libs.kotlin.stdlib) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.koin.core) + api(libs.kmpnotifier) } } } \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt new file mode 100644 index 00000000..c94dcee8 --- /dev/null +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt @@ -0,0 +1,40 @@ +package com.codandotv.streamplayerapp.core_background_work + + +import com.mmk.kmpnotifier.notification.NotificationImage +import com.mmk.kmpnotifier.notification.Notifier +import com.mmk.kmpnotifier.notification.NotifierManager +import kotlin.random.Random + +object NotifierHelperAndroid { + fun showSimpleNotification( + title: String = "Notificação Simples", + body: String = "Corpo da Notificação Simples", + ) { + val notifier = NotifierManager.getLocalNotifier() + notifier.notify { + id= Random.nextInt(0, Int.MAX_VALUE) + this.title = title + this.body = body + payloadData = mapOf( + Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", + "extraKey" to "randomValue" + ) + } + } + + + fun showTestNotificationRegister() { + val notifier = NotifierManager.getLocalNotifier() + notifier.notify { + id= Random.nextInt(0, Int.MAX_VALUE) + title = "Task disparada" + body = "Corpo da task disparada Body message from KMPNotifier" + payloadData = mapOf( + Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", + "extraKey" to "randomValue" + ) + image = NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") + } + } +} \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt index c836d611..5ab873a0 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt @@ -3,6 +3,7 @@ import kotlinx.coroutines.delay class SyncManager { suspend fun syncData() { + NotifierHelperAndroid.showSimpleNotification("Sincronizando dados", "Sincronizando dados de teste...") println("SyncManager: BGTestes Sincronizando dados de teste...") delay(2000) println("SyncManager: BGTestes Dados sincronizados com sucesso!") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bf9bcdf8..6e0d9001 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -39,6 +39,7 @@ activity-compose = "1.8.2" popcorngp = "3.1.2" work-runtime = "2.10.1" +kmpnotifier = "1.5.1" #Firebase google-services = "4.4.2" @@ -124,6 +125,8 @@ coroutines_test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines- popcorn_guineapig = { group = "io.github.codandotv", name = "popcornguineapig", version.ref = "popcorngp" } work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref = "work-runtime" } +kmpnotifier = { module = "io.github.mirzemehdi:kmpnotifier", version.ref = "kmpnotifier" } + [bundles] test = ["junit", "mockk", "mockk_android", "viewmodel_test", "koin_test", "coroutines_test"] diff --git a/iosApp/iosApp.xcodeproj/xcshareddata/xcschemes/iosApp.xcscheme b/iosApp/iosApp.xcodeproj/xcshareddata/xcschemes/iosApp.xcscheme index 4019680b..e76a2fbd 100644 --- a/iosApp/iosApp.xcodeproj/xcshareddata/xcschemes/iosApp.xcscheme +++ b/iosApp/iosApp.xcodeproj/xcshareddata/xcschemes/iosApp.xcscheme @@ -43,7 +43,7 @@ Bool { + NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos( + showPushNotification: true, + askNotificationPermissionOnStart: true, + notificationSoundName: nil + ) + ) + // NotifierHelper.showSimpleNotification("Tarefa registrada") + + BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.codandotv.streamplayerapp", using: nil) { task in self.handleAppRefresh(task: task as! BGProcessingTask) } From 832159cc9ed8790d3c6a614aa506584042ed3ceb Mon Sep 17 00:00:00 2001 From: junior Date: Tue, 27 May 2025 19:51:39 -0300 Subject: [PATCH 6/7] feat: add db to notification --- .../presentation/MainActivity.kt | 26 +++++++++++++++++++ .../di/AppModule.kt | 3 ++- core-background-work/build.gradle.kts | 2 ++ .../core_background_work/worker/SyncWorker.kt | 1 + .../NotifierHelperAndroid.kt | 10 ++++--- .../core_background_work/SyncManager.kt | 24 +++++++++++++++-- .../core_background_work/di/SyncModule.kt | 2 +- 7 files changed, 60 insertions(+), 8 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt index 0ec1d3a9..2876a5af 100644 --- a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt +++ b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt @@ -3,12 +3,22 @@ package com.codandotv.streamplayerapp.presentation import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.lifecycle.lifecycleScope import com.codandotv.streamplayerapp.StreamPlayerApp +import com.codandotv.streamplayerapp.feature_list_streams.list.data.ListStreamRepository import com.google.firebase.Firebase import com.google.firebase.initialize import com.mmk.kmpnotifier.permission.permissionUtil +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import org.koin.android.ext.android.inject class MainActivity : ComponentActivity() { + + + private val repository: ListStreamRepository by inject() +// private val syncManager: SyncManager by inject() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Firebase.initialize(this) @@ -16,10 +26,26 @@ class MainActivity : ComponentActivity() { setContent { StreamPlayerApp() } + + try { + teste() + } catch (e: Exception) { + println("MainActivity: Erro ao testar SyncManager: ${e.message}") + } } private fun requestNotificationPermission() { val permissionUtil by permissionUtil() permissionUtil.askNotificationPermission() } + + + private fun teste() { + lifecycleScope.launch { + repository.topRatedStream().first().let { + println("MainActivity: Top Rated Stream: ${it.name}") + } +// syncManager.syncData() + } + } } diff --git a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt index faf0723b..8267f699 100644 --- a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt +++ b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/di/AppModule.kt @@ -4,6 +4,7 @@ import com.codandotv.streamplayerapp.core_background_work.di.SyncModule import com.codandotv.streamplayerapp.core_local_storage.di.LocalStorageModule import com.codandotv.streamplayerapp.core_networking.di.NetworkModule import com.codandotv.streamplayerapp.core_shared.qualifier.QualifierDispatcherIO +import com.codandotv.streamplayerapp.feature_list_streams.list.di.ListStreamModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO import org.koin.dsl.module @@ -12,5 +13,5 @@ object AppModule { private val module = module { single(QualifierDispatcherIO) { Dispatchers.IO } } - val list = module + NetworkModule.module + LocalStorageModule.module + SyncModule.module + val list = module + NetworkModule.module + LocalStorageModule.module + SyncModule.module + ListStreamModule.module } \ No newline at end of file diff --git a/core-background-work/build.gradle.kts b/core-background-work/build.gradle.kts index a3361696..b55e44b0 100644 --- a/core-background-work/build.gradle.kts +++ b/core-background-work/build.gradle.kts @@ -15,6 +15,8 @@ kotlin { implementation(libs.kotlin.stdlib) implementation(libs.kotlinx.coroutines.core) implementation(libs.koin.core) + implementation(projects.coreShared) + implementation(projects.featureListStreams) api(libs.kmpnotifier) } } diff --git a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt index c9620b30..be8ef488 100644 --- a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt +++ b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt @@ -12,6 +12,7 @@ class SyncWorker( params: WorkerParameters ) : CoroutineWorker(context, params), KoinComponent { + private val syncManager: SyncManager by inject() override suspend fun doWork(): Result { diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt index c94dcee8..585dee89 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt @@ -10,14 +10,15 @@ object NotifierHelperAndroid { fun showSimpleNotification( title: String = "Notificação Simples", body: String = "Corpo da Notificação Simples", + imageUrl: String = "" ) { val notifier = NotifierManager.getLocalNotifier() notifier.notify { - id= Random.nextInt(0, Int.MAX_VALUE) + id = Random.nextInt(0, Int.MAX_VALUE) this.title = title this.body = body payloadData = mapOf( - Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", + Notifier.KEY_URL to imageUrl, "extraKey" to "randomValue" ) } @@ -27,14 +28,15 @@ object NotifierHelperAndroid { fun showTestNotificationRegister() { val notifier = NotifierManager.getLocalNotifier() notifier.notify { - id= Random.nextInt(0, Int.MAX_VALUE) + id = Random.nextInt(0, Int.MAX_VALUE) title = "Task disparada" body = "Corpo da task disparada Body message from KMPNotifier" payloadData = mapOf( Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", "extraKey" to "randomValue" ) - image = NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") + image = + NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") } } } \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt index 5ab873a0..4d137d4d 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt @@ -1,9 +1,29 @@ package com.codandotv.streamplayerapp.core_background_work + +import com.codandotv.streamplayerapp.feature_list_streams.list.data.ListStreamRepository +import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.Stream import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.first -class SyncManager { +class SyncManager( + private val repository: ListStreamRepository +) { suspend fun syncData() { - NotifierHelperAndroid.showSimpleNotification("Sincronizando dados", "Sincronizando dados de teste...") + + val titlle: Stream = repository.topRatedStream().first() + + val titleName = titlle.name + val titleDescription = titlle.description + val messageTitle = "$titleName -Já disponível no app!" + val messageBody = "Confira a sinopse: $titleDescription" + val imageUrl = "https://example.com/image.jpg" + + NotifierHelperAndroid.showSimpleNotification( + title = messageTitle, + body = messageBody, + imageUrl = imageUrl + ) + println("SyncManager: BGTestes Sincronizando dados de teste...") delay(2000) println("SyncManager: BGTestes Dados sincronizados com sucesso!") diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt index 78780770..867efc05 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt @@ -5,6 +5,6 @@ import org.koin.dsl.module object SyncModule { val module = module { - single { SyncManager() } + single { SyncManager(get()) } } } \ No newline at end of file From ad64861f5d77982d1fa2c9c2c1a65dc52cc321ff Mon Sep 17 00:00:00 2001 From: junior Date: Wed, 28 May 2025 20:15:15 -0300 Subject: [PATCH 7/7] refactor: cleaning sync files --- .../presentation/MainActivity.kt | 25 ----------- .../NotifierHelper.kt | 40 ------------------ .../core_background_work/worker/SyncWorker.kt | 2 +- .../core_background_work/NotifierHelper.kt | 26 ++++++++++++ .../NotifierHelperAndroid.kt | 42 ------------------- .../core_background_work/SyncManager.kt | 17 ++++---- iosApp/iosApp/AppDelegate.swift | 23 +++++----- 7 files changed, 44 insertions(+), 131 deletions(-) delete mode 100644 composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt create mode 100644 core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelper.kt delete mode 100644 core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt diff --git a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt index 2876a5af..40c2663c 100644 --- a/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt +++ b/composeApp/src/androidMain/kotlin/com.codandotv.streamplayerapp/presentation/MainActivity.kt @@ -3,22 +3,13 @@ package com.codandotv.streamplayerapp.presentation import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent -import androidx.lifecycle.lifecycleScope import com.codandotv.streamplayerapp.StreamPlayerApp -import com.codandotv.streamplayerapp.feature_list_streams.list.data.ListStreamRepository import com.google.firebase.Firebase import com.google.firebase.initialize import com.mmk.kmpnotifier.permission.permissionUtil -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject class MainActivity : ComponentActivity() { - - private val repository: ListStreamRepository by inject() -// private val syncManager: SyncManager by inject() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Firebase.initialize(this) @@ -26,26 +17,10 @@ class MainActivity : ComponentActivity() { setContent { StreamPlayerApp() } - - try { - teste() - } catch (e: Exception) { - println("MainActivity: Erro ao testar SyncManager: ${e.message}") - } } private fun requestNotificationPermission() { val permissionUtil by permissionUtil() permissionUtil.askNotificationPermission() } - - - private fun teste() { - lifecycleScope.launch { - repository.topRatedStream().first().let { - println("MainActivity: Top Rated Stream: ${it.name}") - } -// syncManager.syncData() - } - } } diff --git a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt b/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt deleted file mode 100644 index f01e5d17..00000000 --- a/composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/NotifierHelper.kt +++ /dev/null @@ -1,40 +0,0 @@ -package com.codandotv.streamplayerapp - - -import com.mmk.kmpnotifier.notification.NotificationImage -import com.mmk.kmpnotifier.notification.Notifier -import com.mmk.kmpnotifier.notification.NotifierManager -import kotlin.random.Random - -object NotifierHelper { - fun showSimpleNotification( - title: String = "Notificação Simples", - body: String = "Corpo da Notificação Simples", - ) { - val notifier = NotifierManager.getLocalNotifier() - notifier.notify { - id= Random.nextInt(0, Int.MAX_VALUE) - this.title = title - this.body = body - payloadData = mapOf( - Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", - "extraKey" to "randomValue" - ) - } - } - - - fun showTestNotificationRegister() { - val notifier = NotifierManager.getLocalNotifier() - notifier.notify { - id= Random.nextInt(0, Int.MAX_VALUE) - title = "Task disparada" - body = "Corpo da task disparada Body message from KMPNotifier" - payloadData = mapOf( - Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", - "extraKey" to "randomValue" - ) - image = NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") - } - } -} \ No newline at end of file diff --git a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt index be8ef488..59fe012b 100644 --- a/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt +++ b/core-background-work/src/androidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt @@ -12,7 +12,6 @@ class SyncWorker( params: WorkerParameters ) : CoroutineWorker(context, params), KoinComponent { - private val syncManager: SyncManager by inject() override suspend fun doWork(): Result { @@ -20,6 +19,7 @@ class SyncWorker( syncManager.syncData() Result.success() } catch (e: Exception) { + println("Erro no SyncWorker ${e.message}") Result.retry() } } diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelper.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelper.kt new file mode 100644 index 00000000..08ca362d --- /dev/null +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelper.kt @@ -0,0 +1,26 @@ +package com.codandotv.streamplayerapp.core_background_work + +import com.mmk.kmpnotifier.notification.NotificationImage +import com.mmk.kmpnotifier.notification.Notifier +import com.mmk.kmpnotifier.notification.NotifierManager +import kotlin.random.Random + +object NotifierHelper { + fun showSimpleNotification( + title: String = "Notificação Simples", + body: String = "Corpo da Notificação Simples", + imageUrl: String = "https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b" + ) { + val notifier = NotifierManager.getLocalNotifier() + notifier.notify { + id = Random.Default.nextInt(0, Int.MAX_VALUE) + this.title = title + this.body = body + payloadData = mapOf( + Notifier.Companion.KEY_URL to imageUrl, + "extraKey" to "randomValue" + ) + image = NotificationImage.Url(imageUrl) + } + } +} \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt deleted file mode 100644 index 585dee89..00000000 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelperAndroid.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.codandotv.streamplayerapp.core_background_work - - -import com.mmk.kmpnotifier.notification.NotificationImage -import com.mmk.kmpnotifier.notification.Notifier -import com.mmk.kmpnotifier.notification.NotifierManager -import kotlin.random.Random - -object NotifierHelperAndroid { - fun showSimpleNotification( - title: String = "Notificação Simples", - body: String = "Corpo da Notificação Simples", - imageUrl: String = "" - ) { - val notifier = NotifierManager.getLocalNotifier() - notifier.notify { - id = Random.nextInt(0, Int.MAX_VALUE) - this.title = title - this.body = body - payloadData = mapOf( - Notifier.KEY_URL to imageUrl, - "extraKey" to "randomValue" - ) - } - } - - - fun showTestNotificationRegister() { - val notifier = NotifierManager.getLocalNotifier() - notifier.notify { - id = Random.nextInt(0, Int.MAX_VALUE) - title = "Task disparada" - body = "Corpo da task disparada Body message from KMPNotifier" - payloadData = mapOf( - Notifier.KEY_URL to "https://github.com/mirzemehdi/KMPNotifier/", - "extraKey" to "randomValue" - ) - image = - NotificationImage.Url("https://github.com/user-attachments/assets/a0f38159-b31d-4a47-97a7-cc230e15d30b") - } - } -} \ No newline at end of file diff --git a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt index 4d137d4d..a43aa783 100644 --- a/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt +++ b/core-background-work/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt @@ -10,22 +10,19 @@ class SyncManager( ) { suspend fun syncData() { - val titlle: Stream = repository.topRatedStream().first() + val title: Stream = repository.topRatedStream().first() + val messageTitle = "${title.name} -Já disponível no app!" + val messageBody = "Confira a sinopse: ${title.description}" + val imageUrl = title.posterPathUrl - val titleName = titlle.name - val titleDescription = titlle.description - val messageTitle = "$titleName -Já disponível no app!" - val messageBody = "Confira a sinopse: $titleDescription" - val imageUrl = "https://example.com/image.jpg" - - NotifierHelperAndroid.showSimpleNotification( + NotifierHelper.showSimpleNotification( title = messageTitle, body = messageBody, imageUrl = imageUrl ) - println("SyncManager: BGTestes Sincronizando dados de teste...") + println("SyncManager: Fazendo alguma tarefa de sincronização...") delay(2000) - println("SyncManager: BGTestes Dados sincronizados com sucesso!") + println("SyncManager: Sincronização concluída!") } } \ No newline at end of file diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift index eda814f0..64dc6b73 100644 --- a/iosApp/iosApp/AppDelegate.swift +++ b/iosApp/iosApp/AppDelegate.swift @@ -12,19 +12,18 @@ class AppDelegate: NSObject, UIApplicationDelegate { notificationSoundName: nil ) ) - // NotifierHelper.showSimpleNotification("Tarefa registrada") BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.codandotv.streamplayerapp", using: nil) { task in - self.handleAppRefresh(task: task as! BGProcessingTask) + self.handleSyncWork(task: task as! BGProcessingTask) } - print("BGTestes Task registrada!") + print("Tarefa registrada!") - scheduleAppRefresh() + scheduleSyncWork() return true } - func scheduleAppRefresh() { + func scheduleSyncWork() { let request = BGProcessingTaskRequest(identifier: "com.codandotv.streamplayerapp") request.requiresNetworkConnectivity = false request.requiresExternalPower = false @@ -32,32 +31,30 @@ class AppDelegate: NSObject, UIApplicationDelegate { do { try BGTaskScheduler.shared.submit(request) - print("BGTestes tarefa agendada com sucesso para: \(request.earliestBeginDate!)") + print("Tarefa agendada com sucesso para pelo menos: \(request.earliestBeginDate!)") } catch { - print("BGTestes falha ao agendar tarefa: \(error.localizedDescription)") + print("Falha ao agendar tarefa: \(error.localizedDescription)") } } - func handleAppRefresh(task: BGProcessingTask) { - print("BGTestes handleAppRefresh foi chamado!") - + func handleSyncWork(task: BGProcessingTask) { scheduleAppRefresh() let queue = OperationQueue() queue.maxConcurrentOperationCount = 1 let operation = BlockOperation { - print("BGTestes Executando operação de sync...") + print("Tarefa executando operação de sync...") SyncBridge.shared.syncData { - print("BGTestes Sincronizado no IOS") + print("Tarefa do rodando no iOS") task.setTaskCompleted(success: true) } } task.expirationHandler = { - print("BGTestes Tarefa expirada.") + print("Tarefa expirada.") queue.cancelAllOperations() }