forked from CodandoTV/Netflix-Android
-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/background work #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8892eb
build: add core-background-work module
guehigecu1970 6a05994
feat: add work manager android
guehigecu1970 04ff40a
feat: add bg tasks iOS
guehigecu1970 8d7e8c0
refactor: switching BGAppRefreshTaskRequest to BGProcessingTask
guehigecu1970 bbcff8b
feat: add notification on syncmanager
guehigecu1970 832159c
feat: add db to notification
guehigecu1970 ad64861
refactor: cleaning sync files
guehigecu1970 0859232
Merge branch 'master' into feature/background-work-tasks
git-jr a663a0f
Merge branch 'master' into feature/background-work
git-jr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| /Users/pedroalvarez/.konan/kotlin-native-prebuilt-macos-aarch64-2.1.10/klib/commonized/2.1.10 | ||
| /Users/junior/.konan/kotlin-native-prebuilt-macos-aarch64-2.1.10/klib/commonized/2.1.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
composeApp/src/commonMain/kotlin/com.codandotv.streamplayerapp/SyncBridge.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<SyncManager>().syncData() | ||
| } | ||
|
|
||
| fun syncData(completionHandler: () -> Unit) { | ||
| CoroutineScope(Dispatchers.Default).launch { | ||
| try { | ||
| syncData() | ||
| } finally { | ||
| completionHandler() | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| @file:Suppress("UnstableApiUsage") | ||
|
|
||
| plugins { | ||
| id("com.streamplayer.kmp-library") | ||
| id("com.google.devtools.ksp") | ||
| } | ||
|
|
||
| kotlin { | ||
| sourceSets { | ||
| androidMain.dependencies { | ||
| implementation(libs.work.runtime) | ||
| } | ||
|
|
||
| commonMain.dependencies { | ||
| implementation(libs.kotlin.stdlib) | ||
| implementation(libs.kotlinx.coroutines.core) | ||
| implementation(libs.koin.core) | ||
| implementation(projects.coreShared) | ||
| implementation(projects.featureListStreams) | ||
| api(libs.kmpnotifier) | ||
| } | ||
| } | ||
| } | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
26 changes: 26 additions & 0 deletions
26
...ndroidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/SyncWorker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| 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) { | ||
| println("Erro no SyncWorker ${e.message}") | ||
| Result.retry() | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
...oidMain/kotlin/com/codandotv/streamplayerapp/core_background_work/worker/WorkScheduler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<SyncWorker>( | ||
| 15, TimeUnit.MINUTES | ||
| ).build() | ||
|
|
||
| WorkManager.getInstance(context).enqueueUniquePeriodicWork( | ||
| "SyncWorker", | ||
| ExistingPeriodicWorkPolicy.KEEP, | ||
| workRequest | ||
| ) | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...rc/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/NotifierHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...k/src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/SyncManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 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( | ||
| private val repository: ListStreamRepository | ||
| ) { | ||
| suspend fun syncData() { | ||
|
|
||
| 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 | ||
|
|
||
| NotifierHelper.showSimpleNotification( | ||
| title = messageTitle, | ||
| body = messageBody, | ||
| imageUrl = imageUrl | ||
| ) | ||
|
|
||
| println("SyncManager: Fazendo alguma tarefa de sincronização...") | ||
| delay(2000) | ||
| println("SyncManager: Sincronização concluída!") | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...src/commonMain/kotlin/com/codandotv/streamplayerapp/core_background_work/di/SyncModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be easier we have You can achieve that by using: expect fun provideSyncManagerInstance(): SyncManager
val module = module { single { provideSyncManagerInstance() }In the androidMain, you can define an actual (the same on iOS). |
||
| val module = module { | ||
| single { SyncManager(get()) } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#question
Can we use
implementationinstead ofapihere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to access the notifications lib in different modules (background, general and in the IOS APP), hence the use of the API