diff --git a/app/src/main/java/by/alexandr7035/gitstat/data/DataSyncRepository.kt b/app/src/main/java/by/alexandr7035/gitstat/data/DataSyncRepository.kt new file mode 100644 index 00000000..f0ee5e33 --- /dev/null +++ b/app/src/main/java/by/alexandr7035/gitstat/data/DataSyncRepository.kt @@ -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") + } + +} \ No newline at end of file diff --git a/app/src/main/java/by/alexandr7035/gitstat/data/SyncForegroundService.kt b/app/src/main/java/by/alexandr7035/gitstat/data/SyncForegroundService.kt index 310c397f..82597e47 100644 --- a/app/src/main/java/by/alexandr7035/gitstat/data/SyncForegroundService.kt +++ b/app/src/main/java/by/alexandr7035/gitstat/data/SyncForegroundService.kt @@ -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() } } \ No newline at end of file diff --git a/app/src/main/java/by/alexandr7035/gitstat/di/AppModule.kt b/app/src/main/java/by/alexandr7035/gitstat/di/AppModule.kt index dcccd133..25e7e479 100644 --- a/app/src/main/java/by/alexandr7035/gitstat/di/AppModule.kt +++ b/app/src/main/java/by/alexandr7035/gitstat/di/AppModule.kt @@ -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 /////////////////////////////////////