Skip to content

Commit

Permalink
Merge pull request #80 from anilibria/fix/TR-274-async-data
Browse files Browse the repository at this point in the history
Fix/tr 274 async data
  • Loading branch information
RadiationX authored Dec 18, 2022
2 parents 32e17d9 + e92ea2e commit 514f498
Show file tree
Hide file tree
Showing 61 changed files with 1,138 additions and 785 deletions.
2 changes: 1 addition & 1 deletion app-mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
minSdkVersion app_min_sdk_version
targetSdkVersion app_target_sdk_version
versionCode 61
versionName "2.8.0_RC7"
versionName "2.8.0_RC8"
buildConfigField "String", 'BUILD_DATE', '"' + getDateTime() + '"'
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.google.firebase.messaging.RemoteMessage
import com.squareup.moshi.Moshi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import ru.radiationx.shared.ktx.android.getCompatColor
import ru.radiationx.anilibria.navigation.Screens
import ru.radiationx.anilibria.ui.activities.main.IntentActivity
import ru.radiationx.anilibria.ui.activities.main.MainActivity
Expand All @@ -26,6 +25,8 @@ import ru.radiationx.data.entity.mapper.toDomain
import ru.radiationx.data.entity.response.config.ApiConfigResponse
import ru.radiationx.quill.get
import ru.radiationx.shared.ktx.android.asMutableFlag
import ru.radiationx.shared.ktx.android.getCompatColor
import ru.radiationx.shared.ktx.coRunCatching
import timber.log.Timber

class NotificationService : FirebaseMessagingService() {
Expand Down Expand Up @@ -71,21 +72,21 @@ class NotificationService : FirebaseMessagingService() {


if (data.type == CUSTOM_TYPE_CONFIG) {
try {
val apiConfig = get<ApiConfig>()
val apiConfigStorage = get<ApiConfigStorage>()
val moshi = get<Moshi>()
GlobalScope.launch {
coRunCatching {
val apiConfig = get<ApiConfig>()
val apiConfigStorage = get<ApiConfigStorage>()
val moshi = get<Moshi>()

GlobalScope.launch {
apiConfig.updateNeedConfig(true)
}

val payload = data.payload.orEmpty()
val configResponse = payload.fetchResponse<ApiConfigResponse>(moshi)
apiConfigStorage.save(configResponse)
apiConfig.setConfig(configResponse.toDomain())
} catch (ex: Exception) {
Timber.e(ex)
val payload = data.payload.orEmpty()
val configResponse = payload.fetchResponse<ApiConfigResponse>(moshi)
apiConfigStorage.save(configResponse)
apiConfig.setConfig(configResponse.toDomain())
}.onFailure {
Timber.e(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.source.MediaSourceEventListener
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import org.michaelbel.bottomsheet.BottomSheet
import ru.radiationx.anilibria.R
import ru.radiationx.anilibria.apptheme.AppThemeController
Expand Down Expand Up @@ -514,13 +515,15 @@ class MyPlayerActivity : BaseActivity(R.layout.activity_myplayer) {
if (position < 0) {
return
}
releaseInteractor.putEpisode(
getEpisode().access.copy(
seek = position,
lastAccess = System.currentTimeMillis(),
isViewed = true
)
val newAccess = getEpisode().access.copy(
seek = position,
lastAccess = System.currentTimeMillis(),
isViewed = true
)
// todo TR-274 run in scope outside lifecycle
runBlocking {
releaseInteractor.putEpisode(newAccess)
}
}

private fun getNextEpisode(): Episode? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import ru.radiationx.anilibria.navigation.Screens
import ru.radiationx.data.analytics.AnalyticsConstants
import ru.radiationx.data.analytics.features.*
Expand Down Expand Up @@ -73,11 +74,16 @@ class MainViewModel(

private fun initMain() {
firstLaunch = false
if (authRepository.getAuthState() == AuthState.NO_AUTH) {
authMainAnalytics.open(AnalyticsConstants.screen_main)
router.navigateTo(Screens.Auth())

// todo TR-274 move in scope after refactor screen
runBlocking {
if (authRepository.getAuthState() == AuthState.NO_AUTH) {
authMainAnalytics.open(AnalyticsConstants.screen_main)
router.navigateTo(Screens.Auth())
}
}


selectTab(defaultScreen)
authRepository
.observeAuthState()
Expand All @@ -103,7 +109,8 @@ class MainViewModel(
router.exit()
}

fun getAuthState() = authRepository.getAuthState()
// todo TR-274 refactor screen with no-getter viewmodel
fun getAuthState() = runBlocking { authRepository.getAuthState() }

fun selectTab(screenKey: String) {
_state.update { it.copy(selectedTab = screenKey) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ class AuthViewModel(
}

fun skip() {
authMainAnalytics.skipClick()
authRepository.setAuthSkipped(true)
router.finishChain()
viewModelScope.launch {
authMainAnalytics.skipClick()
authRepository.setAuthSkipped(true)
router.finishChain()
}
}

fun registrationClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.radiationx.anilibria.ui.fragments.history
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import ru.radiationx.anilibria.model.ReleaseItemState
import ru.radiationx.anilibria.model.toState
import ru.radiationx.anilibria.navigation.Screens
Expand Down Expand Up @@ -101,8 +102,10 @@ class HistoryViewModel(

fun onDeleteClick(item: ReleaseItemState) {
val releaseItem = findRelease(item.id) ?: return
historyAnalytics.releaseDeleteClick()
historyRepository.removeRelease(releaseItem.id)
viewModelScope.launch {
historyAnalytics.releaseDeleteClick()
historyRepository.removeRelease(releaseItem.id)
}
}

fun onCopyClick(item: ReleaseItemState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import ru.radiationx.data.analytics.features.*
import ru.radiationx.data.entity.common.AuthState
import ru.radiationx.data.entity.domain.other.LinkMenuItem
import ru.radiationx.data.entity.domain.other.OtherMenuItem
import ru.radiationx.data.entity.domain.other.ProfileItem
import ru.radiationx.data.repository.AuthRepository
import ru.radiationx.data.repository.MenuRepository
import ru.radiationx.shared.ktx.EventFlow
Expand Down Expand Up @@ -58,7 +57,6 @@ class OtherViewModel(
private val _otpEvent = EventFlow<Unit>()
val otpEvent = _otpEvent.observe()

private var currentProfileItem: ProfileItem? = authRepository.getUser()
private var currentLinkMenuItems = mutableListOf<LinkMenuItem>()
private var linksMap = mutableMapOf<Int, LinkMenuItem>()

Expand Down Expand Up @@ -104,13 +102,15 @@ class OtherViewModel(
}

fun onProfileClick() {
if (authRepository.getAuthState() == AuthState.AUTH) {
otherAnalytics.profileClick()
return
viewModelScope.launch {
if (authRepository.getAuthState() == AuthState.AUTH) {
otherAnalytics.profileClick()
return@launch
}
otherAnalytics.loginClick()
authMainAnalytics.open(AnalyticsConstants.screen_other)
router.navigateTo(Screens.Auth())
}
otherAnalytics.loginClick()
authMainAnalytics.open(AnalyticsConstants.screen_other)
router.navigateTo(Screens.Auth())
}

fun signOut() {
Expand Down Expand Up @@ -174,7 +174,6 @@ class OtherViewModel(
authRepository
.observeUser()
.onEach {
currentProfileItem = it
updateMenuItems()
}
.launchIn(viewModelScope)
Expand All @@ -200,29 +199,31 @@ class OtherViewModel(
}

private fun updateMenuItems() {
// Для фильтрации, если вдруг понадобится добавить
val profileMenu = profileMenu.toMutableList()
val mainMenu = allMainMenu.toMutableList()
val systemMenu = allSystemMenu.toMutableList()
val linkMenu = allLinkMenu.toMutableList()

if (authRepository.getAuthState() != AuthState.AUTH) {
profileMenu.removeAll { it.id == MENU_OTP_CODE }
}
viewModelScope.launch {
// Для фильтрации, если вдруг понадобится добавить
val profileMenu = profileMenu.toMutableList()
val mainMenu = allMainMenu.toMutableList()
val systemMenu = allSystemMenu.toMutableList()
val linkMenu = allLinkMenu.toMutableList()

if (authRepository.getAuthState() != AuthState.AUTH) {
profileMenu.removeAll { it.id == MENU_OTP_CODE }
}

val profileState = currentProfileItem.toState()
val profileMenuState = profileMenu.map { it.toState() }
val menuState = listOf(mainMenu, systemMenu, linkMenu)
.filter { it.isNotEmpty() }
.map { itemsGroup ->
itemsGroup.map { it.toState() }
val profileState = authRepository.getUser().toState()
val profileMenuState = profileMenu.map { it.toState() }
val menuState = listOf(mainMenu, systemMenu, linkMenu)
.filter { it.isNotEmpty() }
.map { itemsGroup ->
itemsGroup.map { it.toState() }
}
_state.update {
it.copy(
profile = profileState,
profileMenuItems = profileMenuState,
menuItems = menuState
)
}
_state.update {
it.copy(
profile = profileState,
profileMenuItems = profileMenuState,
menuItems = menuState
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,26 @@ class ReleaseInfoViewModel(
}

fun markEpisodeViewed(episode: Episode) {
releaseInteractor.putEpisode(
episode.access.copy(
isViewed = true,
lastAccess = System.currentTimeMillis()
viewModelScope.launch {
releaseInteractor.putEpisode(
episode.access.copy(
isViewed = true,
lastAccess = System.currentTimeMillis()
)
)
)
}
}

fun markEpisodeUnviewed(episode: Episode) {
releaseAnalytics.historyResetEpisode()
releaseInteractor.putEpisode(
episode.access.copy(
isViewed = false,
lastAccess = 0
viewModelScope.launch {
releaseAnalytics.historyResetEpisode()
releaseInteractor.putEpisode(
episode.access.copy(
isViewed = false,
lastAccess = 0
)
)
)
}
}

fun onEpisodeTabClick(tabTag: String) {
Expand Down Expand Up @@ -364,20 +368,19 @@ class ReleaseInfoViewModel(
}

fun onClickFav() {
if (authRepository.getAuthState() != AuthState.AUTH) {
showUnauthAction.set(Unit)
return
}
val releaseId = currentData?.id ?: return
val favInfo = currentData?.favoriteInfo ?: return

if (favInfo.isAdded) {
releaseAnalytics.favoriteRemove(releaseId.id)
} else {
releaseAnalytics.favoriteAdd(releaseId.id)
}

viewModelScope.launch {
if (authRepository.getAuthState() != AuthState.AUTH) {
showUnauthAction.set(Unit)
return@launch
}

if (favInfo.isAdded) {
releaseAnalytics.favoriteRemove(releaseId.id)
} else {
releaseAnalytics.favoriteAdd(releaseId.id)
}
updateModifiers {
it.copy(favoriteRefreshing = true)
}
Expand Down Expand Up @@ -491,19 +494,23 @@ class ReleaseInfoViewModel(
}

fun onResetEpisodesHistoryClick() {
releaseAnalytics.historyReset()
currentData?.also {
releaseInteractor.resetEpisodesHistory(it.id)
viewModelScope.launch {
releaseAnalytics.historyReset()
currentData?.also {
releaseInteractor.resetEpisodesHistory(it.id)
}
}
}

fun onCheckAllEpisodesHistoryClick() {
releaseAnalytics.historyViewAll()
currentData?.also {
val accesses = it.episodes.map {
it.access.copy(isViewed = true)
viewModelScope.launch {
releaseAnalytics.historyViewAll()
currentData?.also {
val accesses = it.episodes.map {
it.access.copy(isViewed = true)
}
releaseInteractor.putEpisodes(accesses)
}
releaseInteractor.putEpisodes(accesses)
}
}

Expand Down
2 changes: 1 addition & 1 deletion app-tv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion tv_min_sdk_version
targetSdkVersion app_target_sdk_version
versionCode 3
versionName "1.0.0_RC5"
versionName "1.1.0_RC1"
}

buildTypes {
Expand Down
Loading

0 comments on commit 514f498

Please sign in to comment.