<commit_before>package com.antonio.samir.meteoritelandingsspots.service.business

import android.location.Location
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.paging.DataSource
import com.antonio.samir.meteoritelandingsspots.service.business.model.Meteorite
import com.antonio.samir.meteoritelandingsspots.service.repository.local.MeteoriteRepositoryInterface
import com.antonio.samir.meteoritelandingsspots.util.DefaultDispatcherProvider
import com.antonio.samir.meteoritelandingsspots.util.DispatcherProvider
import com.antonio.samir.meteoritelandingsspots.util.GPSTrackerInterface
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicBoolean

class MeteoriteNasaService(
        private val meteoriteRepository: MeteoriteRepositoryInterface,
        private val addressService: AddressServiceInterface,
        private val gpsTracker: GPSTrackerInterface,
        private val dispatchers: DispatcherProvider = DefaultDispatcherProvider()
) : MeteoriteServiceInterface {

    private val isUpdateRequired = AtomicBoolean(false)

    private val addressServiceStarted = AtomicBoolean(false)

    private var mediatorLiveData: MediatorLiveData<List<Meteorite>> = MediatorLiveData()

    override var location: Location? = null

    private val meteoritesByName = meteoriteRepository.meteoriteOrdered(null, null)

    override suspend fun loadMeteorites(filter: String?): DataSource.Factory<Int, Meteorite> = withContext(dispatchers.default()) {

        GlobalScope.launch(dispatchers.unconfined()) {
            val meteoritesCount = meteoriteRepository.getMeteoritesCount()
            if (meteoritesCount < 1000 && !isUpdateRequired.getAndSet(true)) {
                //If it is empty so load the data from internet
                recoverFromNetwork()
            }
        }

        if (!addressServiceStarted.getAndSet(true)) {
            addressService.recoveryAddress()
        }

//        if (!gpsTracker.isLocationServiceStarted() && gpsTracker.isGPSEnabled()) {
//            updateLocation()
//        }
<commit_msg>feat: start address recovery only after network is done<commit_after>package com.antonio.samir.meteoritelandingsspots.service.business

import android.location.Location
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.paging.DataSource
import com.antonio.samir.meteoritelandingsspots.service.business.model.Meteorite
import com.antonio.samir.meteoritelandingsspots.service.repository.local.MeteoriteRepositoryInterface
import com.antonio.samir.meteoritelandingsspots.util.DefaultDispatcherProvider
import com.antonio.samir.meteoritelandingsspots.util.DispatcherProvider
import com.antonio.samir.meteoritelandingsspots.util.GPSTrackerInterface
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicBoolean

class MeteoriteNasaService(
        private val meteoriteRepository: MeteoriteRepositoryInterface,
        private val addressService: AddressServiceInterface,
        private val gpsTracker: GPSTrackerInterface,
        private val dispatchers: DispatcherProvider = DefaultDispatcherProvider()
) : MeteoriteServiceInterface {

    private val isUpdateRequired = AtomicBoolean(false)

    private val addressServiceStarted = AtomicBoolean(false)

    override var location: Location? = null

    override suspend fun loadMeteorites(filter: String?): DataSource.Factory<Int, Meteorite> = withContext(dispatchers.default()) {

        GlobalScope.launch(dispatchers.unconfined()) {
            val meteoritesCount = meteoriteRepository.getMeteoritesCount()
            if (meteoritesCount < 1000 && !isUpdateRequired.getAndSet(true)) {
                //If it is empty so load the data from internet
                recoverFromNetwork()
            }
            if (!addressServiceStarted.getAndSet(true)) {
                addressService.recoveryAddress()
            }
        }


//        if (!gpsTracker.isLocationServiceStarted() && gpsTracker.isGPSEnabled()) {
//            updateLocation()
//        }

{'fraction_non_alphanumeric': 0.07501150483202945, 'fraction_numerical': 0.0018407731247123793, 'mean_word_length': 4.288321167883212, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235656', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1153, 'n_words': 214}