-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call test fun in coroutine in SyncForegroundService.kt
- Loading branch information
1 parent
839dc1b
commit 122b44c
Showing
3 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/src/main/java/by/alexandr7035/gitstat/data/DataSyncRepository.kt
This file contains 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 by.alexandr7035.gitstat.data | ||
|
||
import by.alexandr7035.gitstat.core.AppPreferences | ||
import by.alexandr7035.gitstat.core.TimeHelper | ||
import by.alexandr7035.gitstat.data.local.CacheDB | ||
import by.alexandr7035.gitstat.data.remote.mappers.* | ||
import com.apollographql.apollo3.ApolloClient | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class DataSyncRepository @Inject constructor( | ||
private val apolloClient: ApolloClient, | ||
private val db: CacheDB, | ||
private val timeHelper: TimeHelper, | ||
private val appPreferences: AppPreferences, | ||
|
||
private val profileMapper: UserRemoteToCacheMapper, | ||
private val repositoriesMapper: RepositoriesRemoteToCacheMapper, | ||
private val contributionsMapper: ContributionsDaysListRemoteToCacheMapper, | ||
private val ratioMapper: ContributionsRatioRemoteToCacheMapper, | ||
private val daysToRatesMapper: ContributionDaysToRatesMapper, | ||
) | ||
{ | ||
suspend fun test() { | ||
Timber.tag("DEBUG_SERVICE").d("service repo test") | ||
} | ||
|
||
} |
33 changes: 30 additions & 3 deletions
33
app/src/main/java/by/alexandr7035/gitstat/data/SyncForegroundService.kt
This file contains 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,35 +1,62 @@ | ||
package by.alexandr7035.gitstat.data | ||
|
||
import android.app.Notification | ||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.res.Configuration | ||
import android.os.IBinder | ||
import androidx.core.app.NotificationCompat | ||
import by.alexandr7035.gitstat.R | ||
import by.alexandr7035.gitstat.view.MainActivity | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class SyncForegroundService: Service() { | ||
|
||
@Inject lateinit var syncRepository: DataSyncRepository | ||
private var job: Job? = null | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
return null | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
Timber.tag("DEBUG_SERVICE").d("123") | ||
startForeground(System.currentTimeMillis().toInt(), getNotification()) | ||
|
||
job = CoroutineScope(Dispatchers.IO).launch { | ||
syncRepository.test() | ||
} | ||
|
||
return START_NOT_STICKY | ||
} | ||
|
||
private fun getNotification(): Notification { | ||
|
||
val notificationIntent = Intent(this, MainActivity::class.java) | ||
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0) | ||
|
||
val notification = NotificationCompat.Builder(this, getString(R.string.NOTIFICATION_CHANNEL_ID)) | ||
return NotificationCompat.Builder(this, getString(R.string.NOTIFICATION_CHANNEL_ID)) | ||
.setContentTitle(getString(R.string.sync_notification_title)) | ||
.setContentText(getString(R.string.sync_notification_text)) | ||
.setSmallIcon(R.drawable.ic_app_rounded) | ||
.setContentIntent(pendingIntent) | ||
.build() | ||
} | ||
|
||
startForeground(1, notification) | ||
|
||
return START_NOT_STICKY | ||
override fun onDestroy() { | ||
job?.cancel() | ||
job = null | ||
super.onDestroy() | ||
} | ||
|
||
} |
This file contains 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