Skip to content

Commit

Permalink
Get rid of old SyncRepository.kt class
Browse files Browse the repository at this point in the history
Related #155
  • Loading branch information
alexandr7035 committed Nov 8, 2021
1 parent ffb2efe commit 3808cc4
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ enum class DataSyncStatus {
PENDING_CONTRIBUTIONS,
SUCCESS,
FAILED_NETWORK,
// TODO delete old
FAILED_NETWORK_WITH_CACHE,
FAILED_NETWORK_WITH_NO_CACHE,
AUTHORIZATION_ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ class DataSyncRepository @Inject constructor(
val contributionRates = fetchContributionRates(contributionDays)

// Write cache
appPreferences.lastSuccessCacheSyncDate = 0
// Be patient with livedata. This call causes updates with null
// So wrap code in observers wuth null check
db.clearAllTables()
clearCache()

Timber.tag("DEBUG_TAG").d("profile $profile")
db.getUserDao().insertUser(profile)
Expand Down Expand Up @@ -200,4 +197,23 @@ class DataSyncRepository @Inject constructor(
return appPreferences.lastSuccessCacheSyncDate != 0L
}

fun checkIfTokenSaved(): Boolean {
return appPreferences.token != null
}

fun clearCache() {
// Be patient with livedata. This call causes updates with null
// So wrap code in observers with null check
db.clearAllTables()
appPreferences.lastSuccessCacheSyncDate = 0
}

fun clearToken() {
appPreferences.token = null
}

fun getLastCacheSyncDateText(): String {
return timeHelper.getFullFromUnixDate(appPreferences.lastSuccessCacheSyncDate)
}

}
238 changes: 0 additions & 238 deletions app/src/main/java/by/alexandr7035/gitstat/data/SyncRepository.kt

This file was deleted.

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

@Provides
@Singleton
fun provideSyncRepository(
apolloClient: ApolloClient,
db: CacheDB,
userDao: UserDao,
contributionsDao: ContributionsDao,
repositoriesDao: RepositoriesDao,
profileMapper: UserRemoteToCacheMapper,
repositoriesMapper: RepositoriesRemoteToCacheMapper,
contributionsMapper: ContributionsDaysListRemoteToCacheMapper,
daysToRatesMapper: ContributionDaysToRatesMapper,
ratioMapper: ContributionsRatioRemoteToCacheMapper,
timeHelper: TimeHelper,
appPreferences: AppPreferences): SyncRepository {
return SyncRepository(apolloClient, db, userDao, repositoriesDao, contributionsDao, profileMapper, repositoriesMapper, contributionsMapper, ratioMapper, daysToRatesMapper, timeHelper, appPreferences)
}

@Provides
@Singleton
fun provideAuthRepository(apolloClient: ApolloClient, appPreferences: AppPreferences): AuthRepository {
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/by/alexandr7035/gitstat/view/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package by.alexandr7035.gitstat.view
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import by.alexandr7035.gitstat.data.DataSyncRepository
import by.alexandr7035.gitstat.data.SyncRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(private val syncRepository: SyncRepository, private val dataSyncRepository: DataSyncRepository): ViewModel() {
class MainViewModel @Inject constructor(private val dataSyncRepository: DataSyncRepository): ViewModel() {

fun checkIfTokenSaved(): Boolean {
return syncRepository.checkIfTokenSaved()
return dataSyncRepository.checkIfTokenSaved()
}

fun checkIfCacheExists(): Boolean {
Expand All @@ -22,15 +21,15 @@ class MainViewModel @Inject constructor(private val syncRepository: SyncReposito

fun clearCache() {
viewModelScope.launch(Dispatchers.IO) {
syncRepository.clearCache()
dataSyncRepository.clearCache()
}
}

fun getCacheSyncDate(): String {
return syncRepository.getLastCacheSyncDateText()
return dataSyncRepository.getLastCacheSyncDateText()
}

fun clearToken() {
syncRepository.clearToken()
dataSyncRepository.clearToken()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.navGraphViewModels
import by.alexandr7035.gitstat.R
import by.alexandr7035.gitstat.databinding.FragmentSyncFailedNoCacheBinding
import by.alexandr7035.gitstat.databinding.FragmentSyncFailedNetworkBinding

class SyncFailedNoCacheFragment : Fragment() {
class SyncFailedNetworkFragment : Fragment() {

private var binding: FragmentSyncFailedNoCacheBinding? = null
private var binding: FragmentSyncFailedNetworkBinding? = null
private val viewModel by navGraphViewModels<SyncViewModel>(R.id.syncGraph) { defaultViewModelProviderFactory }

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
// Inflate the layout for this fragment
binding = FragmentSyncFailedNoCacheBinding.inflate(inflater, container, false)
binding = FragmentSyncFailedNetworkBinding.inflate(inflater, container, false)
return binding!!.root
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class SyncHostFragment : Fragment() {
.commit()
}

DataSyncStatus.FAILED_NETWORK_WITH_NO_CACHE -> {
DataSyncStatus.FAILED_NETWORK -> {
requireActivity().supportFragmentManager.beginTransaction()
.replace(R.id.containerView, SyncFailedNoCacheFragment())
.replace(R.id.containerView, SyncFailedNetworkFragment())
.commit()
}

Expand Down
Loading

0 comments on commit 3808cc4

Please sign in to comment.