Skip to content

Commit

Permalink
Call test fun in coroutine in SyncForegroundService.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr7035 committed Nov 7, 2021
1 parent 839dc1b commit 122b44c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
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")
}

}
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()
}

}
18 changes: 18 additions & 0 deletions app/src/main/java/by/alexandr7035/gitstat/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ object AppModule {
return ContributionsRepository(dao, timeHelper, appPreferences)
}


@Provides
@Singleton
fun provideDataSyncRepository(
apolloClient: ApolloClient,
db: CacheDB,
timeHelper: TimeHelper,
appPreferences: AppPreferences,

profileMapper: UserRemoteToCacheMapper,
repositoriesMapper: RepositoriesRemoteToCacheMapper,
contributionsMapper: ContributionsDaysListRemoteToCacheMapper,
ratioMapper: ContributionsRatioRemoteToCacheMapper,
daysToRatesMapper: ContributionDaysToRatesMapper
): DataSyncRepository {
return DataSyncRepository(apolloClient, db, timeHelper, appPreferences, profileMapper, repositoriesMapper, contributionsMapper, ratioMapper, daysToRatesMapper)
}

/////////////////////////////////////
// Mappers
/////////////////////////////////////
Expand Down

0 comments on commit 122b44c

Please sign in to comment.