Compare commits
23 Commits
ai-bullshit
..
temp
| Author | SHA1 | Date | |
|---|---|---|---|
| 66deae6fc3 | |||
| 96ee5ea45e | |||
| dc5b4b3fb0 | |||
| a1278f7558 | |||
| 6b91d388a2 | |||
| 8c053905ce | |||
| 2381d4ca0a | |||
| 2daab8d0f7 | |||
| 10453287a7 | |||
| fc3b3cfcb3 | |||
| f11b8dc6f4 | |||
| 167f980f29 | |||
| d428af4ac4 | |||
| 63bae014c5 | |||
| ad54477d11 | |||
| c8bd485724 | |||
| 26a0630393 | |||
| 3d153df79c | |||
| 9061a39407 | |||
| d8c8820b32 | |||
| abfe25d051 | |||
| 574b230b26 | |||
| b31c0f30c5 |
@@ -0,0 +1,56 @@
|
|||||||
|
name: Android CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
env:
|
||||||
|
KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
|
||||||
|
RELEASE_SIGN_KEY_ALIAS: ${{ secrets.RELEASE_SIGN_KEY_ALIAS }}
|
||||||
|
RELEASE_SIGN_KEY_PASSWORD: ${{ secrets.RELEASE_SIGN_KEY_PASSWORD }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
android:
|
||||||
|
runs-on: android-jdk21
|
||||||
|
name: Build artifacts
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Make Gradle executable
|
||||||
|
run: chmod +x ./gradlew
|
||||||
|
|
||||||
|
- name: Build and sign release APK
|
||||||
|
run: ./gradlew assembleRelease
|
||||||
|
|
||||||
|
- name: Find generated release APK name
|
||||||
|
id: find_apk_release
|
||||||
|
run: |
|
||||||
|
APK_PATH=$(find app/build/outputs/apk/release -name "*.apk" | head -n 1)
|
||||||
|
echo "APK_PATH=$APK_PATH" >> $GITEA_ENV
|
||||||
|
echo "APK_NAME=$(basename $APK_PATH)" >> $GITEA_ENV
|
||||||
|
|
||||||
|
- name: Upload APK with original name
|
||||||
|
uses: christopherhx/gitea-upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: ${{ env.APK_NAME }}
|
||||||
|
path: ${{ env.APK_PATH }}
|
||||||
|
|
||||||
|
- name: Build and sign debug APK
|
||||||
|
run: ./gradlew assembleDebug
|
||||||
|
|
||||||
|
- name: Find generated debug APK name
|
||||||
|
id: find_apk_debug
|
||||||
|
run: |
|
||||||
|
APK_PATH=$(find app/build/outputs/apk/debug -name "*.apk" | head -n 1)
|
||||||
|
echo "APK_PATH=$APK_PATH" >> $GITEA_ENV
|
||||||
|
echo "APK_NAME=$(basename $APK_PATH)" >> $GITEA_ENV
|
||||||
|
|
||||||
|
- name: Upload APK with original name
|
||||||
|
uses: christopherhx/gitea-upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: ${{ env.APK_NAME }}
|
||||||
|
path: ${{ env.APK_PATH }}
|
||||||
@@ -15,3 +15,5 @@ build/
|
|||||||
local.properties
|
local.properties
|
||||||
.idea
|
.idea
|
||||||
/.kotlin
|
/.kotlin
|
||||||
|
.hotswan/
|
||||||
|
.java-version
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ Unofficial messenger for russian social network VKontakte
|
|||||||
- [ ] Forwarded messages
|
- [ ] Forwarded messages
|
||||||
- [ ] Wall post
|
- [ ] Wall post
|
||||||
- [ ] Comment in wall post
|
- [ ] Comment in wall post
|
||||||
|
- [ ] Comment in channel
|
||||||
- [ ] Poll
|
- [ ] Poll
|
||||||
- [ ] TODO
|
- [ ] TODO
|
||||||
- [x] Send messages
|
- [x] Send messages
|
||||||
|
|||||||
@@ -1,131 +0,0 @@
|
|||||||
# Fast Messenger Tech Debt Audit
|
|
||||||
|
|
||||||
## Critical
|
|
||||||
|
|
||||||
### `core/network/src/main/kotlin/dev/meloda/fast/network/interceptor/Error14HandlingInterceptor.kt`
|
|
||||||
- captcha flow is built around `wait/notify`, a raw executor, and shared mutable state.
|
|
||||||
- risk: deadlocks, leaked jobs, hard-to-reproduce auth hangs.
|
|
||||||
- fix: rewrite as suspend-based flow with timeout and explicit cancellation.
|
|
||||||
|
|
||||||
### `core/domain/src/main/kotlin/dev/meloda/fast/domain/LongPollEventParser.kt`
|
|
||||||
- file mixes parsing, dispatching, IO loading, and concurrency orchestration.
|
|
||||||
- risk: regressions when VK event format changes.
|
|
||||||
- fix: split by event family and add parser tests.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/MessagesHistoryViewModelImpl.kt`
|
|
||||||
- view model is doing too much: loaders, navigation, selection, long poll hooks, read peers, dialog flow.
|
|
||||||
- risk: brittle state transitions and untestable branching.
|
|
||||||
- fix: extract coordinators/handlers per concern.
|
|
||||||
|
|
||||||
### `app/src/main/kotlin/dev/meloda/fast/MainViewModel.kt`
|
|
||||||
- root bootstrap handles auth, locale, long poll, permissions, profile, and start destination in one class.
|
|
||||||
- risk: startup bugs and hidden coupling between flows.
|
|
||||||
- fix: move startup/permission orchestration into dedicated controllers.
|
|
||||||
|
|
||||||
### `app/src/main/kotlin/dev/meloda/fast/presentation/RootScreen.kt`
|
|
||||||
- root composable owns too many top-level flows and dialogs.
|
|
||||||
- risk: UI orchestration drift and hard-to-read navigation logic.
|
|
||||||
- fix: split dialogs, bootstrap, and navigation concern into smaller composables.
|
|
||||||
|
|
||||||
## High
|
|
||||||
|
|
||||||
### `core/domain/src/main/kotlin/dev/meloda/fast/domain/LongPollEventParser.kt`
|
|
||||||
- uses many `Log.d` calls and large `when` branches.
|
|
||||||
- fix: reduce logging noise and add structured tracing only where needed.
|
|
||||||
|
|
||||||
### `core/network/src/main/kotlin/dev/meloda/fast/network/ResponseConverterFactory.kt`
|
|
||||||
- double-parsing response bodies and only logging malformed payloads.
|
|
||||||
- risk: opaque failures and harder debugging.
|
|
||||||
- fix: normalize error conversion and surface typed failures.
|
|
||||||
|
|
||||||
### `feature/profile/src/main/kotlin/dev/meloda/fast/profile/ProfileViewModel.kt`
|
|
||||||
- `loadAccountInfo()` has an empty error branch.
|
|
||||||
- risk: profile can fail silently.
|
|
||||||
- fix: set `baseError` and show fallback UI.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/presentation/attachments/Attachments.kt`
|
|
||||||
- attachment preview logic still sits in UI layer and mixes fallback behavior.
|
|
||||||
- risk: silent drops of unsupported attachments.
|
|
||||||
- fix: move preview mapping to domain/ui-model layer.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/presentation/Link.kt`
|
|
||||||
- title handling is nullable and duplicated with preview logic.
|
|
||||||
- fix: create a small UI model for link rendering.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/presentation/File.kt`
|
|
||||||
- preview extraction is inline and branches on raw model internals.
|
|
||||||
- fix: extract to mapper/UI model.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/presentation/MessagesList.kt`
|
|
||||||
- message-item interaction is dense and repeated.
|
|
||||||
- fix: normalize scroll/reply handlers and reduce nested callbacks.
|
|
||||||
|
|
||||||
## Medium
|
|
||||||
|
|
||||||
### `core/data/src/main/kotlin/dev/meloda/fast/data/VkUsersMap.kt`
|
|
||||||
- lookup helpers were using unsafe assumptions on external data.
|
|
||||||
- status: already improved, but should stay covered by tests.
|
|
||||||
|
|
||||||
### `core/data/src/main/kotlin/dev/meloda/fast/data/VkGroupsMap.kt`
|
|
||||||
- same risk profile as users map.
|
|
||||||
- status: already improved, still needs tests.
|
|
||||||
|
|
||||||
### `core/domain/src/main/kotlin/dev/meloda/fast/domain/OAuthUseCaseImpl.kt`
|
|
||||||
- auth success mapping used forced unwraps on server data.
|
|
||||||
- status: already improved, but auth contract should be validated.
|
|
||||||
|
|
||||||
### `app/src/main/kotlin/dev/meloda/fast/presentation/MainActivity.kt`
|
|
||||||
- service lifecycle now respects config changes, but app exit semantics should be documented.
|
|
||||||
- fix: keep explicit separation of app-close vs config-change behavior.
|
|
||||||
|
|
||||||
### `build-logic/convention/src/main/kotlin/dev/meloda/fast/KotlinAndroid.kt`
|
|
||||||
- build-tools are pinned to the local environment.
|
|
||||||
- risk: portable builds may drift between machines.
|
|
||||||
- fix: prefer SDK-managed consistency or document required SDK version.
|
|
||||||
|
|
||||||
### `app/src/main/kotlin/dev/meloda/fast/presentation/RootErrorDialog.kt`
|
|
||||||
- currently hardcodes English strings for some errors.
|
|
||||||
- fix: localize all texts through resources.
|
|
||||||
|
|
||||||
### `app/src/main/kotlin/dev/meloda/fast/presentation/RootScreen.kt`
|
|
||||||
- root-level error dialog is better, but some orchestration still remains in the root composable.
|
|
||||||
- fix: split into smaller root flows over time.
|
|
||||||
|
|
||||||
## Low
|
|
||||||
|
|
||||||
### `core/ui/src/main/kotlin/dev/meloda/fast/ui/components/AnimatedDots.kt`
|
|
||||||
- marked TODO rewrite.
|
|
||||||
- fix when touching related loading UI.
|
|
||||||
|
|
||||||
### `core/ui/src/main/kotlin/dev/meloda/fast/ui/theme/AppTheme.kt`
|
|
||||||
- color picker TODO suggests unfinished theme customization.
|
|
||||||
|
|
||||||
### `core/model/src/main/kotlin/dev/meloda/fast/model/api/data/LongPollUpdates.kt`
|
|
||||||
- `List<List<Any>>` is a weakly typed API boundary.
|
|
||||||
- fix: introduce explicit event DTOs.
|
|
||||||
|
|
||||||
### `core/model/src/main/kotlin/dev/meloda/fast/model/api/domain/VkMessage.kt`
|
|
||||||
- attachment persistence is still a TODO area.
|
|
||||||
|
|
||||||
### `core/model/src/main/kotlin/dev/meloda/fast/model/database/VkMessageEntity.kt`
|
|
||||||
- attachment storage/restoration is unresolved.
|
|
||||||
|
|
||||||
### `core/common/src/main/kotlin/dev/meloda/fast/common/model/LongPollState.kt`
|
|
||||||
- Android 15 support TODO.
|
|
||||||
|
|
||||||
### `feature/messageshistory/src/main/kotlin/dev/meloda/fast/messageshistory/MessagesHistoryViewModelImpl.kt`
|
|
||||||
- large commented-out legacy block should be removed once upload flow is reimplemented.
|
|
||||||
|
|
||||||
### `feature/auth/src/main/kotlin/dev/meloda/fast/auth/login/LoginViewModel.kt`
|
|
||||||
- debug/auth token acquisition TODO indicates unfinished auth path.
|
|
||||||
|
|
||||||
## What To Do First
|
|
||||||
1. Rewrite captcha interceptor.
|
|
||||||
2. Split `LongPollEventParser`.
|
|
||||||
3. Extract `MessagesHistoryViewModelImpl` orchestration.
|
|
||||||
4. Localize `RootErrorDialog` strings.
|
|
||||||
5. Add tests for auth, long poll parsing, and attachment mapping.
|
|
||||||
|
|
||||||
## Note
|
|
||||||
- Current code is already better on crash-prone nullable handling and service lifecycle.
|
|
||||||
- Remaining work is mostly structural and testability-focused.
|
|
||||||
@@ -47,7 +47,7 @@ android {
|
|||||||
applicationIdSuffix = ".debug"
|
applicationIdSuffix = ".debug"
|
||||||
}
|
}
|
||||||
named("release") {
|
named("release") {
|
||||||
signingConfig = signingConfigs.getByName("release")
|
signingConfig = signingConfigs.getByName("debugSigning")
|
||||||
|
|
||||||
isMinifyEnabled = true
|
isMinifyEnabled = true
|
||||||
isShrinkResources = true
|
isShrinkResources = true
|
||||||
@@ -79,9 +79,6 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.acra.email)
|
|
||||||
implementation(libs.acra.dialog)
|
|
||||||
|
|
||||||
implementation(projects.feature.auth)
|
implementation(projects.feature.auth)
|
||||||
|
|
||||||
implementation(projects.feature.chatmaterials)
|
implementation(projects.feature.chatmaterials)
|
||||||
@@ -95,6 +92,7 @@ dependencies {
|
|||||||
implementation(projects.feature.photoviewer)
|
implementation(projects.feature.photoviewer)
|
||||||
implementation(projects.feature.createchat)
|
implementation(projects.feature.createchat)
|
||||||
|
|
||||||
|
implementation(projects.core.logger)
|
||||||
implementation(projects.core.common)
|
implementation(projects.core.common)
|
||||||
implementation(projects.core.ui)
|
implementation(projects.core.ui)
|
||||||
implementation(projects.core.data)
|
implementation(projects.core.data)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:tools="http://schemas.android.com/tools"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||||
@@ -37,6 +38,12 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name="dev.meloda.fast.presentation.CrashActivity"
|
||||||
|
android:exported="false"
|
||||||
|
android:process=":error_handler"
|
||||||
|
android:theme="@style/CrashDialogTheme" />
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name="dev.meloda.fast.service.longpolling.LongPollingService"
|
android:name="dev.meloda.fast.service.longpolling.LongPollingService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
|
|||||||
@@ -22,94 +22,52 @@ import dev.meloda.fast.datastore.AppSettings
|
|||||||
import dev.meloda.fast.datastore.UserSettings
|
import dev.meloda.fast.datastore.UserSettings
|
||||||
import dev.meloda.fast.domain.GetCurrentAccountUseCase
|
import dev.meloda.fast.domain.GetCurrentAccountUseCase
|
||||||
import dev.meloda.fast.domain.LoadUserByIdUseCase
|
import dev.meloda.fast.domain.LoadUserByIdUseCase
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import dev.meloda.fast.model.BaseError
|
import dev.meloda.fast.model.BaseError
|
||||||
import dev.meloda.fast.model.api.domain.VkUser
|
import dev.meloda.fast.model.api.domain.VkUser
|
||||||
import dev.meloda.fast.navigation.Main
|
import dev.meloda.fast.navigation.Main
|
||||||
import dev.meloda.fast.settings.navigation.Settings
|
import dev.meloda.fast.settings.navigation.Settings
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
interface MainViewModel {
|
class MainViewModel(
|
||||||
|
|
||||||
val startDestination: StateFlow<Any?>
|
|
||||||
val isNeedToReplaceWithAuth: StateFlow<Boolean>
|
|
||||||
val currentUser: StateFlow<VkUser?>
|
|
||||||
val baseError: StateFlow<BaseError?>
|
|
||||||
|
|
||||||
val isNeedToShowNotificationsDeniedDialog: StateFlow<Boolean>
|
|
||||||
val isNeedToShowNotificationsRationaleDialog: StateFlow<Boolean>
|
|
||||||
val isNeedToCheckNotificationsPermission: StateFlow<Boolean>
|
|
||||||
val isNeedToRequestNotifications: StateFlow<Boolean>
|
|
||||||
|
|
||||||
fun onError(error: BaseError)
|
|
||||||
fun onErrorConsumed()
|
|
||||||
|
|
||||||
fun onNavigatedToAuth()
|
|
||||||
|
|
||||||
fun onAppResumed(intent: Intent)
|
|
||||||
|
|
||||||
@OptIn(ExperimentalPermissionsApi::class)
|
|
||||||
fun onPermissionCheckStatus(status: PermissionStatus)
|
|
||||||
fun onPermissionsRequested()
|
|
||||||
|
|
||||||
fun onNotificationsDeniedDialogConfirmClicked()
|
|
||||||
fun onNotificationsDeniedDialogCancelClicked()
|
|
||||||
fun onNotificationsDeniedDialogDismissed()
|
|
||||||
fun onNotificationsRationaleDialogDismissed()
|
|
||||||
fun onNotificationsRationaleDialogCancelClicked()
|
|
||||||
|
|
||||||
fun onUserAuthenticated()
|
|
||||||
}
|
|
||||||
|
|
||||||
class MainViewModelImpl(
|
|
||||||
private val getCurrentAccountUseCase: GetCurrentAccountUseCase,
|
private val getCurrentAccountUseCase: GetCurrentAccountUseCase,
|
||||||
private val loadUserByIdUseCase: LoadUserByIdUseCase,
|
private val loadUserByIdUseCase: LoadUserByIdUseCase,
|
||||||
private val userSettings: UserSettings,
|
private val userSettings: UserSettings,
|
||||||
private val longPollController: LongPollController
|
private val longPollController: LongPollController,
|
||||||
) : MainViewModel, ViewModel() {
|
private val logger: FastLogger
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
override val startDestination = MutableStateFlow<Any?>(null)
|
val startDestination = MutableStateFlow<Any?>(null)
|
||||||
override val isNeedToReplaceWithAuth = MutableStateFlow(false)
|
val isNeedToReplaceWithAuth = MutableStateFlow(false)
|
||||||
override val currentUser = MutableStateFlow<VkUser?>(null)
|
val currentUser = MutableStateFlow<VkUser?>(null)
|
||||||
override val baseError = MutableStateFlow<BaseError?>(null)
|
|
||||||
|
|
||||||
override val isNeedToShowNotificationsDeniedDialog = MutableStateFlow(false)
|
val isNeedToShowNotificationsDeniedDialog = MutableStateFlow(false)
|
||||||
override val isNeedToShowNotificationsRationaleDialog = MutableStateFlow(false)
|
val isNeedToShowNotificationsRationaleDialog = MutableStateFlow(false)
|
||||||
override val isNeedToCheckNotificationsPermission = MutableStateFlow(false)
|
val isNeedToCheckNotificationsPermission = MutableStateFlow(false)
|
||||||
override val isNeedToRequestNotifications = MutableStateFlow(false)
|
val isNeedToRequestNotifications = MutableStateFlow(false)
|
||||||
|
|
||||||
private var openNotificationsSettings = false
|
private var openNotificationsSettings = false
|
||||||
private var openAppSettings = false
|
private var openAppSettings = false
|
||||||
|
|
||||||
init {
|
fun onError(error: BaseError) {
|
||||||
listenLongPollState()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onError(error: BaseError) {
|
|
||||||
when (error) {
|
when (error) {
|
||||||
BaseError.SessionExpired,
|
BaseError.SessionExpired,
|
||||||
BaseError.AccountBlocked -> {
|
BaseError.AccountBlocked -> {
|
||||||
isNeedToReplaceWithAuth.update { true }
|
isNeedToReplaceWithAuth.update { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> Unit // TODO: 21-Mar-25, Danil Nikolaev: show error in ui
|
||||||
baseError.update { error }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onErrorConsumed() {
|
fun onNavigatedToAuth() {
|
||||||
baseError.update { null }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onNavigatedToAuth() {
|
|
||||||
isNeedToReplaceWithAuth.update { false }
|
isNeedToReplaceWithAuth.update { false }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onAppResumed(intent: Intent) {
|
fun onAppResumed(intent: Intent) {
|
||||||
openNotificationsSettings =
|
openNotificationsSettings =
|
||||||
intent.hasCategory(NotificationCompat.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
|
intent.hasCategory(NotificationCompat.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
|
||||||
openAppSettings =
|
openAppSettings =
|
||||||
@@ -136,7 +94,7 @@ class MainViewModelImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExperimentalPermissionsApi
|
@ExperimentalPermissionsApi
|
||||||
override fun onPermissionCheckStatus(status: PermissionStatus) {
|
fun onPermissionCheckStatus(status: PermissionStatus) {
|
||||||
isNeedToCheckNotificationsPermission.update { false }
|
isNeedToCheckNotificationsPermission.update { false }
|
||||||
|
|
||||||
when (status) {
|
when (status) {
|
||||||
@@ -158,33 +116,33 @@ class MainViewModelImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPermissionsRequested() {
|
fun onPermissionsRequested() {
|
||||||
isNeedToRequestNotifications.update { false }
|
isNeedToRequestNotifications.update { false }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationsDeniedDialogConfirmClicked() {
|
fun onNotificationsDeniedDialogConfirmClicked() {
|
||||||
isNeedToRequestNotifications.update { true }
|
isNeedToRequestNotifications.update { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationsDeniedDialogCancelClicked() {
|
fun onNotificationsDeniedDialogCancelClicked() {
|
||||||
isNeedToShowNotificationsDeniedDialog.update { false }
|
isNeedToShowNotificationsDeniedDialog.update { false }
|
||||||
disableBackgroundLongPoll()
|
disableBackgroundLongPoll()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationsDeniedDialogDismissed() {
|
fun onNotificationsDeniedDialogDismissed() {
|
||||||
isNeedToShowNotificationsDeniedDialog.update { false }
|
isNeedToShowNotificationsDeniedDialog.update { false }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationsRationaleDialogDismissed() {
|
fun onNotificationsRationaleDialogDismissed() {
|
||||||
isNeedToShowNotificationsRationaleDialog.update { false }
|
isNeedToShowNotificationsRationaleDialog.update { false }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationsRationaleDialogCancelClicked() {
|
fun onNotificationsRationaleDialogCancelClicked() {
|
||||||
isNeedToShowNotificationsRationaleDialog.update { false }
|
isNeedToShowNotificationsRationaleDialog.update { false }
|
||||||
disableBackgroundLongPoll()
|
disableBackgroundLongPoll()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUserAuthenticated() {
|
fun onUserAuthenticated() {
|
||||||
loadProfile()
|
loadProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +171,20 @@ class MainViewModelImpl(
|
|||||||
|
|
||||||
private fun loadAccounts() {
|
private fun loadAccounts() {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val currentAccount = getCurrentAccountUseCase()
|
val currentAccount = getCurrentAccountUseCase()?.mapToDto()
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this@MainViewModel::class,
|
||||||
|
"loadAccounts(): currentAccount: %s"
|
||||||
|
.format(
|
||||||
|
currentAccount?.copy(
|
||||||
|
accessToken = if (currentAccount.accessToken.isNotEmpty()) "<redacted>"
|
||||||
|
else "null"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
listenLongPollState()
|
||||||
|
|
||||||
if (currentAccount != null) {
|
if (currentAccount != null) {
|
||||||
UserConfig.apply {
|
UserConfig.apply {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package dev.meloda.fast.common
|
package dev.meloda.fast.common
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import coil.ImageLoaderFactory
|
import coil.ImageLoaderFactory
|
||||||
@@ -8,14 +10,16 @@ import com.skydoves.compose.stability.runtime.ComposeStabilityAnalyzer
|
|||||||
import dev.meloda.fast.auth.BuildConfig
|
import dev.meloda.fast.auth.BuildConfig
|
||||||
import dev.meloda.fast.common.di.applicationModule
|
import dev.meloda.fast.common.di.applicationModule
|
||||||
import dev.meloda.fast.datastore.AppSettings
|
import dev.meloda.fast.datastore.AppSettings
|
||||||
import org.acra.config.dialog
|
import dev.meloda.fast.logger.FastLogLevel
|
||||||
import org.acra.config.mailSender
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import org.acra.data.StringFormat
|
import dev.meloda.fast.presentation.CrashActivity
|
||||||
import org.acra.ktx.initAcra
|
|
||||||
import org.koin.android.ext.android.get
|
import org.koin.android.ext.android.get
|
||||||
import org.koin.android.ext.koin.androidContext
|
import org.koin.android.ext.koin.androidContext
|
||||||
import org.koin.android.ext.koin.androidLogger
|
import org.koin.android.ext.koin.androidLogger
|
||||||
import org.koin.core.context.GlobalContext.startKoin
|
import org.koin.core.context.GlobalContext.startKoin
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
class AppGlobal : Application(), ImageLoaderFactory {
|
class AppGlobal : Application(), ImageLoaderFactory {
|
||||||
|
|
||||||
@@ -27,7 +31,15 @@ class AppGlobal : Application(), ImageLoaderFactory {
|
|||||||
ComposeStabilityAnalyzer.setEnabled(BuildConfig.DEBUG)
|
ComposeStabilityAnalyzer.setEnabled(BuildConfig.DEBUG)
|
||||||
|
|
||||||
initKoin()
|
initKoin()
|
||||||
initAcra()
|
initCrashHandler()
|
||||||
|
|
||||||
|
val logLevel =
|
||||||
|
if (BuildConfig.DEBUG) FastLogLevel.DEBUG
|
||||||
|
else FastLogLevel.ERROR
|
||||||
|
|
||||||
|
get<FastLogger>()
|
||||||
|
.apply { setLogLevel(logLevel) }
|
||||||
|
.also { FastLogger.setInstance(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun newImageLoader(): ImageLoader = get()
|
override fun newImageLoader(): ImageLoader = get()
|
||||||
@@ -40,20 +52,36 @@ class AppGlobal : Application(), ImageLoaderFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initAcra() {
|
private fun initCrashHandler() {
|
||||||
initAcra {
|
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||||
buildConfigClass = BuildConfig::class.java
|
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
|
||||||
reportFormat = StringFormat.JSON
|
val crashLogsDirectory = File(filesDir.absolutePath + "/crashlogs")
|
||||||
|
if (!crashLogsDirectory.exists()) {
|
||||||
mailSender {
|
crashLogsDirectory.mkdirs()
|
||||||
mailTo = "lischenkodev@gmail.com"
|
|
||||||
reportAsFile = true
|
|
||||||
reportFileName = "Crash.txt"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dialog {
|
val crashLogFileName = "crash_" + System.currentTimeMillis() + ".txt"
|
||||||
text = "App crashed"
|
val crashLogFile = File(crashLogsDirectory.absolutePath + "/" + crashLogFileName)
|
||||||
enabled = true
|
|
||||||
|
FileOutputStream(crashLogFile).use { stream ->
|
||||||
|
stream.write(throwable.stackTraceToString().toByteArray())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (AppSettings.Debug.showAlertAfterCrash) {
|
||||||
|
try {
|
||||||
|
val intent = Intent(this, CrashActivity::class.java)
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
||||||
|
intent.putExtra("CRASH_LOG_FILE_URI", Uri.fromFile(crashLogFile))
|
||||||
|
startActivity(intent)
|
||||||
|
|
||||||
|
exitProcess(0)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
if (e !is RuntimeException) {
|
||||||
|
defaultExceptionHandler?.uncaughtException(thread, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
defaultExceptionHandler?.uncaughtException(thread, throwable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package dev.meloda.fast.common.di
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.res.Resources
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.os.PowerManager
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import org.koin.android.ext.koin.androidContext
|
||||||
|
import org.koin.core.module.dsl.factoryOf
|
||||||
|
import org.koin.dsl.module
|
||||||
|
|
||||||
|
val androidModule = module {
|
||||||
|
// TODO: 14/05/2024, Danil Nikolaev: extract all operations with preferences to standalone class
|
||||||
|
factoryOf(PreferenceManager::getDefaultSharedPreferences)
|
||||||
|
factory<Resources> { androidContext().resources }
|
||||||
|
factory<PowerManager> { androidContext().getSystemService(Context.POWER_SERVICE) as PowerManager }
|
||||||
|
factory<ConnectivityManager> { androidContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }
|
||||||
|
}
|
||||||
@@ -1,16 +1,13 @@
|
|||||||
package dev.meloda.fast.common.di
|
package dev.meloda.fast.common.di
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.res.Resources
|
|
||||||
import android.os.PowerManager
|
|
||||||
import androidx.preference.PreferenceManager
|
|
||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import coil.annotation.ExperimentalCoilApi
|
import coil.annotation.ExperimentalCoilApi
|
||||||
import dev.meloda.fast.MainViewModelImpl
|
import dev.meloda.fast.MainViewModel
|
||||||
import dev.meloda.fast.auth.authModule
|
import dev.meloda.fast.auth.authModule
|
||||||
import dev.meloda.fast.chatmaterials.di.chatMaterialsModule
|
import dev.meloda.fast.chatmaterials.di.chatMaterialsModule
|
||||||
import dev.meloda.fast.common.LongPollController
|
import dev.meloda.fast.common.LongPollController
|
||||||
import dev.meloda.fast.common.LongPollControllerImpl
|
import dev.meloda.fast.common.LongPollControllerImpl
|
||||||
|
import dev.meloda.fast.common.NetworkStateListener
|
||||||
import dev.meloda.fast.common.provider.Provider
|
import dev.meloda.fast.common.provider.Provider
|
||||||
import dev.meloda.fast.common.provider.ResourceProvider
|
import dev.meloda.fast.common.provider.ResourceProvider
|
||||||
import dev.meloda.fast.common.provider.ResourceProviderImpl
|
import dev.meloda.fast.common.provider.ResourceProviderImpl
|
||||||
@@ -19,13 +16,15 @@ import dev.meloda.fast.convos.di.createChatModule
|
|||||||
import dev.meloda.fast.domain.di.domainModule
|
import dev.meloda.fast.domain.di.domainModule
|
||||||
import dev.meloda.fast.friends.di.friendsModule
|
import dev.meloda.fast.friends.di.friendsModule
|
||||||
import dev.meloda.fast.languagepicker.di.languagePickerModule
|
import dev.meloda.fast.languagepicker.di.languagePickerModule
|
||||||
|
import dev.meloda.fast.logger.loggerModule
|
||||||
import dev.meloda.fast.messageshistory.di.messagesHistoryModule
|
import dev.meloda.fast.messageshistory.di.messagesHistoryModule
|
||||||
import dev.meloda.fast.photoviewer.di.photoViewModule
|
import dev.meloda.fast.photoviewer.di.photoViewModule
|
||||||
|
import dev.meloda.fast.presentation.NetworkObserver
|
||||||
import dev.meloda.fast.profile.di.profileModule
|
import dev.meloda.fast.profile.di.profileModule
|
||||||
import dev.meloda.fast.provider.ApiLanguageProvider
|
import dev.meloda.fast.provider.ApiLanguageProvider
|
||||||
import dev.meloda.fast.service.longpolling.di.longPollModule
|
import dev.meloda.fast.service.longpolling.di.longPollModule
|
||||||
import dev.meloda.fast.settings.di.settingsModule
|
import dev.meloda.fast.settings.di.settingsModule
|
||||||
import org.koin.android.ext.koin.androidContext
|
import org.koin.core.module.dsl.factoryOf
|
||||||
import org.koin.core.module.dsl.singleOf
|
import org.koin.core.module.dsl.singleOf
|
||||||
import org.koin.core.module.dsl.viewModelOf
|
import org.koin.core.module.dsl.viewModelOf
|
||||||
import org.koin.core.qualifier.qualifier
|
import org.koin.core.qualifier.qualifier
|
||||||
@@ -49,24 +48,25 @@ val applicationModule = module {
|
|||||||
createChatModule
|
createChatModule
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: 14/05/2024, Danil Nikolaev: extract all operations with preferences to standalone class
|
includes(loggerModule)
|
||||||
singleOf(PreferenceManager::getDefaultSharedPreferences)
|
includes(androidModule)
|
||||||
single<Resources> { androidContext().resources }
|
|
||||||
factory<PowerManager> { androidContext().getSystemService(Context.POWER_SERVICE) as PowerManager }
|
|
||||||
|
|
||||||
singleOf(::ApiLanguageProvider) bind Provider::class
|
factoryOf(::ApiLanguageProvider) bind Provider::class
|
||||||
|
|
||||||
viewModelOf(::MainViewModelImpl) {
|
viewModelOf(::MainViewModel) { qualifier = qualifier("main") }
|
||||||
qualifier = qualifier("main")
|
|
||||||
}
|
|
||||||
|
|
||||||
single<ImageLoader> {
|
single<ImageLoader> {
|
||||||
ImageLoader.Builder(get())
|
ImageLoader.Builder(get())
|
||||||
.crossfade(true)
|
.crossfade(true)
|
||||||
.build()
|
.build()
|
||||||
.also { it.diskCache?.directory?.toFile()?.listFiles() }
|
.also {
|
||||||
|
it.diskCache?.directory?.toFile()?.listFiles()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
singleOf(::LongPollControllerImpl) bind LongPollController::class
|
singleOf(::LongPollControllerImpl) bind LongPollController::class
|
||||||
singleOf(::ResourceProviderImpl) bind ResourceProvider::class
|
singleOf(::ResourceProviderImpl) bind ResourceProvider::class
|
||||||
|
|
||||||
|
singleOf(::NetworkStateListener)
|
||||||
|
singleOf(::NetworkObserver) { qualifier = qualifier("main") }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package dev.meloda.fast.presentation
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.window.DialogProperties
|
||||||
|
import dev.meloda.fast.ui.R
|
||||||
|
import dev.meloda.fast.ui.components.MaterialDialog
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AppCrashedDialog(
|
||||||
|
stacktrace: String,
|
||||||
|
onDismiss: () -> Unit,
|
||||||
|
onShare: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
) {
|
||||||
|
var showTrace by rememberSaveable { mutableStateOf(false) }
|
||||||
|
|
||||||
|
MaterialDialog(
|
||||||
|
modifier = modifier,
|
||||||
|
onDismissRequest = onDismiss,
|
||||||
|
title = stringResource(R.string.title_error),
|
||||||
|
text = if (showTrace) stacktrace else stringResource(R.string.error_occurred),
|
||||||
|
confirmText = stringResource(R.string.action_share),
|
||||||
|
confirmAction = onShare,
|
||||||
|
cancelText = stringResource(if (showTrace) R.string.action_hide_stacktrace else R.string.action_show_stacktrace),
|
||||||
|
cancelAction = { showTrace = !showTrace },
|
||||||
|
neutralText = stringResource(R.string.action_close),
|
||||||
|
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package dev.meloda.fast.presentation
|
||||||
|
|
||||||
|
import android.content.ClipData
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.compose.setContent
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
|
import androidx.core.net.toFile
|
||||||
|
import dev.meloda.fast.datastore.UserSettings
|
||||||
|
import dev.meloda.fast.ui.theme.AppTheme
|
||||||
|
import dev.meloda.fast.ui.util.isNeedToEnableDarkMode
|
||||||
|
import org.koin.compose.koinInject
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class CrashActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
|
||||||
|
val crashLogFileUri = intent.getParcelableExtra<Uri>("CRASH_LOG_FILE_URI") ?: run {
|
||||||
|
finish()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val crashLogFile = crashLogFileUri.toFile().takeIf(File::exists) ?: run {
|
||||||
|
finish()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val stacktrace = crashLogFile.bufferedReader().readText()
|
||||||
|
|
||||||
|
setContent {
|
||||||
|
val userSettings: UserSettings = koinInject()
|
||||||
|
|
||||||
|
AppTheme(
|
||||||
|
useDarkTheme = isNeedToEnableDarkMode(darkMode = userSettings.darkMode.collectAsState().value),
|
||||||
|
useDynamicColors = userSettings.enableDynamicColors.collectAsState().value,
|
||||||
|
selectedColorScheme = 0,
|
||||||
|
useAmoledBackground = userSettings.enableAmoledDark.collectAsState().value,
|
||||||
|
useSystemFont = userSettings.useSystemFont.collectAsState().value
|
||||||
|
) {
|
||||||
|
AppCrashedDialog(
|
||||||
|
stacktrace = stacktrace,
|
||||||
|
onDismiss = { finish() },
|
||||||
|
onShare = {
|
||||||
|
val uri = FileProvider.getUriForFile(
|
||||||
|
this,
|
||||||
|
"$packageName.provider",
|
||||||
|
crashLogFile
|
||||||
|
)
|
||||||
|
|
||||||
|
val sendIntent = Intent().apply {
|
||||||
|
action = Intent.ACTION_SEND
|
||||||
|
type = "text/plain"
|
||||||
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
putExtra(Intent.EXTRA_STREAM, uri)
|
||||||
|
clipData = ClipData.newRawUri(null, uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
val chooserIntent = Intent.createChooser(sendIntent, null)
|
||||||
|
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
startActivity(chooserIntent)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,20 +13,24 @@ import androidx.activity.SystemBarStyle
|
|||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.toArgb
|
import androidx.compose.ui.graphics.toArgb
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.lifecycle.compose.LifecycleResumeEffect
|
import androidx.lifecycle.compose.LifecycleResumeEffect
|
||||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||||
import dev.meloda.fast.MainViewModel
|
import dev.meloda.fast.MainViewModel
|
||||||
import dev.meloda.fast.MainViewModelImpl
|
|
||||||
import dev.meloda.fast.common.AppConstants
|
import dev.meloda.fast.common.AppConstants
|
||||||
import dev.meloda.fast.datastore.AppSettings
|
import dev.meloda.fast.datastore.AppSettings
|
||||||
|
import dev.meloda.fast.domain.LongPollEventsHandler
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import dev.meloda.fast.service.OnlineService
|
import dev.meloda.fast.service.OnlineService
|
||||||
import dev.meloda.fast.service.longpolling.LongPollingService
|
import dev.meloda.fast.service.longpolling.LongPollingService
|
||||||
import dev.meloda.fast.ui.R
|
import dev.meloda.fast.ui.R
|
||||||
|
import dev.meloda.fast.ui.common.LocalLogger
|
||||||
|
import org.koin.android.ext.android.get
|
||||||
import org.koin.androidx.compose.koinViewModel
|
import org.koin.androidx.compose.koinViewModel
|
||||||
|
import org.koin.compose.koinInject
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@@ -63,24 +67,28 @@ class MainActivity : AppCompatActivity() {
|
|||||||
requestNotificationPermissions()
|
requestNotificationPermissions()
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
|
val logger: FastLogger = koinInject()
|
||||||
|
|
||||||
|
val viewModel: MainViewModel = koinViewModel()
|
||||||
LifecycleResumeEffect(true) {
|
LifecycleResumeEffect(true) {
|
||||||
viewModel.onAppResumed(intent)
|
viewModel.onAppResumed(intent)
|
||||||
onPauseOrDispose {}
|
onPauseOrDispose {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CompositionLocalProvider(LocalLogger provides logger) {
|
||||||
RootScreen(
|
RootScreen(
|
||||||
toggleLongPollService = { enable, inBackground ->
|
toggleLongPollService = { enable, inBackground ->
|
||||||
toggleLongPollService(
|
toggleLongPollService(
|
||||||
enable = enable,
|
enable = enable,
|
||||||
inBackground = inBackground ?: AppSettings.Experimental.longPollInBackground
|
inBackground = inBackground
|
||||||
|
?: AppSettings.Experimental.longPollInBackground
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
toggleOnlineService = ::toggleOnlineService
|
toggleOnlineService = ::toggleOnlineService
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun createNotificationChannels() {
|
private fun createNotificationChannels() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@@ -174,6 +182,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
stopServices()
|
stopServices()
|
||||||
|
get<LongPollEventsHandler>().onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -38,14 +38,13 @@ import dev.chrisbanes.haze.HazeState
|
|||||||
import dev.chrisbanes.haze.hazeEffect
|
import dev.chrisbanes.haze.hazeEffect
|
||||||
import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi
|
import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi
|
||||||
import dev.chrisbanes.haze.materials.HazeMaterials
|
import dev.chrisbanes.haze.materials.HazeMaterials
|
||||||
import dev.meloda.fast.convos.navigation.ConvoGraph
|
import dev.meloda.fast.convos.model.ConvoNavigationIntent
|
||||||
import dev.meloda.fast.convos.navigation.convosGraph
|
import dev.meloda.fast.convos.navigation.convosGraph
|
||||||
import dev.meloda.fast.friends.navigation.Friends
|
import dev.meloda.fast.friends.navigation.Friends
|
||||||
import dev.meloda.fast.friends.navigation.friendsScreen
|
import dev.meloda.fast.friends.navigation.friendsScreen
|
||||||
import dev.meloda.fast.model.BaseError
|
import dev.meloda.fast.model.BaseError
|
||||||
import dev.meloda.fast.model.BottomNavigationItem
|
import dev.meloda.fast.model.BottomNavigationItem
|
||||||
import dev.meloda.fast.navigation.MainGraph
|
import dev.meloda.fast.navigation.MainGraph
|
||||||
import dev.meloda.fast.profile.navigation.Profile
|
|
||||||
import dev.meloda.fast.profile.navigation.profileScreen
|
import dev.meloda.fast.profile.navigation.profileScreen
|
||||||
import dev.meloda.fast.ui.theme.LocalBottomPadding
|
import dev.meloda.fast.ui.theme.LocalBottomPadding
|
||||||
import dev.meloda.fast.ui.theme.LocalHazeState
|
import dev.meloda.fast.ui.theme.LocalHazeState
|
||||||
@@ -70,18 +69,15 @@ fun MainScreen(
|
|||||||
val theme = LocalThemeConfig.current
|
val theme = LocalThemeConfig.current
|
||||||
val hazeState = remember { HazeState(true) }
|
val hazeState = remember { HazeState(true) }
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
val defaultTabIndex = remember(navigationItems) {
|
|
||||||
navigationItems.indexOfFirst { it.route == ConvoGraph }.takeIf { it >= 0 } ?: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
var selectedItemIndex by rememberSaveable {
|
var selectedItemIndex by rememberSaveable {
|
||||||
mutableIntStateOf(defaultTabIndex)
|
mutableIntStateOf(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
BackHandler(enabled = selectedItemIndex != defaultTabIndex) {
|
BackHandler(enabled = selectedItemIndex != 1) {
|
||||||
val currentRoute = navigationItems[selectedItemIndex].route
|
val currentRoute = navigationItems[selectedItemIndex].route
|
||||||
|
|
||||||
selectedItemIndex = defaultTabIndex
|
selectedItemIndex = 1
|
||||||
navController.navigate(navigationItems[selectedItemIndex].route) {
|
navController.navigate(navigationItems[selectedItemIndex].route) {
|
||||||
popUpTo(route = currentRoute) {
|
popUpTo(route = currentRoute) {
|
||||||
inclusive = true
|
inclusive = true
|
||||||
@@ -131,7 +127,7 @@ fun MainScreen(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
icon = {
|
icon = {
|
||||||
if (item.route == Profile) {
|
if (index == navigationItems.size - 1) {
|
||||||
var isLoading by remember {
|
var isLoading by remember {
|
||||||
mutableStateOf(true)
|
mutableStateOf(true)
|
||||||
}
|
}
|
||||||
@@ -202,19 +198,20 @@ fun MainScreen(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
convosGraph(
|
convosGraph(
|
||||||
|
handleNavigationIntent = { intent ->
|
||||||
|
when (intent) {
|
||||||
|
ConvoNavigationIntent.Back -> {}
|
||||||
|
ConvoNavigationIntent.Archive -> {}
|
||||||
|
ConvoNavigationIntent.CreateChat -> onNavigateToCreateChat()
|
||||||
|
is ConvoNavigationIntent.MessagesHistory -> {
|
||||||
|
onNavigateToMessagesHistory(intent.convoId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
activity = activity,
|
activity = activity,
|
||||||
onError = onError,
|
|
||||||
onNavigateToMessagesHistory = onNavigateToMessagesHistory,
|
|
||||||
onNavigateToCreateChat = onNavigateToCreateChat,
|
|
||||||
onScrolledToTop = {
|
|
||||||
tabReselected = tabReselected.toMutableMap().also {
|
|
||||||
it[ConvoGraph] = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
profileScreen(
|
profileScreen(
|
||||||
activity = activity,
|
activity = activity,
|
||||||
onError = onError,
|
|
||||||
onSettingsButtonClicked = onSettingsButtonClicked,
|
onSettingsButtonClicked = onSettingsButtonClicked,
|
||||||
onPhotoClicked = onPhotoClicked
|
onPhotoClicked = onPhotoClicked
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,238 @@
|
|||||||
|
package dev.meloda.fast.presentation
|
||||||
|
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.LinkProperties
|
||||||
|
import android.net.Network
|
||||||
|
import android.net.NetworkCapabilities
|
||||||
|
import android.net.NetworkRequest
|
||||||
|
import android.os.Build
|
||||||
|
import dev.meloda.fast.common.NetworkStateListener
|
||||||
|
import dev.meloda.fast.common.model.NetworkState
|
||||||
|
import dev.meloda.fast.common.model.NetworkStatus
|
||||||
|
import dev.meloda.fast.common.model.NetworkType
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
|
internal class NetworkObserver(
|
||||||
|
private val connectivityManager: ConnectivityManager,
|
||||||
|
private val logger: FastLogger,
|
||||||
|
private val networkStateListener: NetworkStateListener
|
||||||
|
) {
|
||||||
|
|
||||||
|
private val networks = ConcurrentHashMap<Network, NetworkModel>()
|
||||||
|
|
||||||
|
private var clearCallbacks: (() -> Unit)? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
startListener()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun syncNetworkState() {
|
||||||
|
val state = if (networks.values.any { it.isInternetAvailable() }) {
|
||||||
|
NetworkState.CONNECTED
|
||||||
|
} else {
|
||||||
|
NetworkState.DISCONNECTED
|
||||||
|
}
|
||||||
|
|
||||||
|
networkStateListener.updateNetworkState(state)
|
||||||
|
log("STATE: $state")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startListener() {
|
||||||
|
val callback = object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
log("onAvailable(): network: $network")
|
||||||
|
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
capabilities = connectivityManager.getNetworkCapabilities(network),
|
||||||
|
properties = connectivityManager.getLinkProperties(network),
|
||||||
|
status = NetworkStatus.AVAILABLE,
|
||||||
|
assumeInternet = true
|
||||||
|
)
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onUnavailable() {
|
||||||
|
log("onUnavailable()")
|
||||||
|
|
||||||
|
networks.clear()
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
log("onLost() network: $network")
|
||||||
|
|
||||||
|
networks.remove(network)
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCapabilitiesChanged(
|
||||||
|
network: Network,
|
||||||
|
networkCapabilities: NetworkCapabilities
|
||||||
|
) {
|
||||||
|
log("onCapabilitiesChanged(): network: $network; caps: $networkCapabilities")
|
||||||
|
|
||||||
|
val current = networks[network]
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
capabilities = networkCapabilities,
|
||||||
|
from = current,
|
||||||
|
status = current?.status ?: NetworkStatus.AVAILABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBlockedStatusChanged(
|
||||||
|
network: Network,
|
||||||
|
blocked: Boolean
|
||||||
|
) {
|
||||||
|
log("onBlockedStatusChanged(): network: $network; blocked: $blocked")
|
||||||
|
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
from = networks[network],
|
||||||
|
status = if (blocked) NetworkStatus.BLOCKED else NetworkStatus.UNBLOCKED
|
||||||
|
)
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLinkPropertiesChanged(
|
||||||
|
network: Network,
|
||||||
|
linkProperties: LinkProperties
|
||||||
|
) {
|
||||||
|
log("onLinkPropertiesChanged(): network: $network; props: $linkProperties")
|
||||||
|
|
||||||
|
val current = networks[network]
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
properties = linkProperties,
|
||||||
|
from = current,
|
||||||
|
status = current?.status ?: NetworkStatus.AVAILABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLosing(network: Network, maxMsToLive: Int) {
|
||||||
|
log("onLosing(): network: $network; maxMsToLive: $maxMsToLive")
|
||||||
|
|
||||||
|
val current = networks[network]
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
maxMsToLive = maxMsToLive.toLong(),
|
||||||
|
from = current,
|
||||||
|
status = current?.status ?: NetworkStatus.AVAILABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onReserved(networkCapabilities: NetworkCapabilities) {
|
||||||
|
log("onReserved(): caps: $networkCapabilities")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val request = NetworkRequest.Builder()
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
connectivityManager.registerNetworkCallback(request, callback)
|
||||||
|
clearCallbacks = { connectivityManager.unregisterNetworkCallback(callback) }
|
||||||
|
|
||||||
|
refreshActiveNetwork()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun log(text: String) {
|
||||||
|
logger.debug(this::class, text)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun refreshActiveNetwork() {
|
||||||
|
val network = connectivityManager.activeNetwork
|
||||||
|
if (network == null) {
|
||||||
|
networks.clear()
|
||||||
|
syncNetworkState()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val capabilities = connectivityManager.getNetworkCapabilities(network)
|
||||||
|
if (capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true) {
|
||||||
|
networks[network] = mapNetworkModel(
|
||||||
|
network = network,
|
||||||
|
capabilities = capabilities,
|
||||||
|
properties = connectivityManager.getLinkProperties(network),
|
||||||
|
status = NetworkStatus.AVAILABLE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
syncNetworkState()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mapNetworkModel(
|
||||||
|
network: Network,
|
||||||
|
capabilities: NetworkCapabilities? = null,
|
||||||
|
properties: LinkProperties? = null,
|
||||||
|
status: NetworkStatus? = null,
|
||||||
|
maxMsToLive: Long? = null,
|
||||||
|
from: NetworkModel? = null,
|
||||||
|
assumeInternet: Boolean = false
|
||||||
|
): NetworkModel {
|
||||||
|
val caps = capabilities
|
||||||
|
?: from?.networkCapabilities
|
||||||
|
?: connectivityManager.getNetworkCapabilities(network)
|
||||||
|
|
||||||
|
val networkType = when {
|
||||||
|
caps?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true -> NetworkType.CELLULAR
|
||||||
|
caps?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true -> NetworkType.WIFI
|
||||||
|
else -> from?.type ?: NetworkType.UNKNOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
val hasInternet = if (caps != null) {
|
||||||
|
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ||
|
||||||
|
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
} else {
|
||||||
|
from?.hasInternet ?: assumeInternet
|
||||||
|
}
|
||||||
|
|
||||||
|
val signalStrength =
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
caps?.signalStrength
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
} ?: from?.signalStrength ?: Int.MAX_VALUE
|
||||||
|
|
||||||
|
return NetworkModel(
|
||||||
|
id = network.hashCode(),
|
||||||
|
type = networkType,
|
||||||
|
original = network,
|
||||||
|
hasInternet = hasInternet,
|
||||||
|
signalStrength = signalStrength,
|
||||||
|
status = status ?: from?.status ?: NetworkStatus.UNAVAILABLE,
|
||||||
|
maxMsToLive = maxMsToLive ?: from?.maxMsToLive,
|
||||||
|
networkCapabilities = caps,
|
||||||
|
linkProperties = properties
|
||||||
|
?: from?.linkProperties
|
||||||
|
?: connectivityManager.getLinkProperties(network)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
data class NetworkModel(
|
||||||
|
val id: Int,
|
||||||
|
val type: NetworkType,
|
||||||
|
val original: Network,
|
||||||
|
val hasInternet: Boolean,
|
||||||
|
val signalStrength: Int,
|
||||||
|
val status: NetworkStatus,
|
||||||
|
val maxMsToLive: Long?,
|
||||||
|
val networkCapabilities: NetworkCapabilities?,
|
||||||
|
val linkProperties: LinkProperties?
|
||||||
|
) {
|
||||||
|
fun isStatusOk(): Boolean = status.isOk()
|
||||||
|
|
||||||
|
fun isInternetAvailable(): Boolean = hasInternet && isStatusOk()
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package dev.meloda.fast.presentation
|
|
||||||
|
|
||||||
import androidx.compose.material3.AlertDialog
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import dev.meloda.fast.model.BaseError
|
|
||||||
import dev.meloda.fast.ui.R
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun RootErrorDialog(
|
|
||||||
baseError: BaseError?,
|
|
||||||
onDismiss: () -> Unit,
|
|
||||||
onConfirm: () -> Unit,
|
|
||||||
) {
|
|
||||||
if (baseError == null) return
|
|
||||||
|
|
||||||
val errorText = when (baseError) {
|
|
||||||
BaseError.ConnectionError -> "Connection error"
|
|
||||||
BaseError.InternalError -> "Internal error"
|
|
||||||
BaseError.UnknownError -> "Unknown error"
|
|
||||||
is BaseError.SimpleError -> baseError.message
|
|
||||||
BaseError.SessionExpired -> "Session expired"
|
|
||||||
BaseError.AccountBlocked -> "Account blocked"
|
|
||||||
}
|
|
||||||
|
|
||||||
AlertDialog(
|
|
||||||
onDismissRequest = onDismiss,
|
|
||||||
title = { Text(text = stringResource(id = R.string.warning)) },
|
|
||||||
text = { Text(text = errorText) },
|
|
||||||
confirmButton = {
|
|
||||||
TextButton(onClick = onConfirm) {
|
|
||||||
Text(text = stringResource(id = R.string.try_again))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -10,6 +10,9 @@ import androidx.compose.animation.fadeIn
|
|||||||
import androidx.compose.animation.fadeOut
|
import androidx.compose.animation.fadeOut
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
@@ -35,13 +38,13 @@ import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
|||||||
import com.google.accompanist.permissions.isGranted
|
import com.google.accompanist.permissions.isGranted
|
||||||
import com.google.accompanist.permissions.rememberPermissionState
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
import dev.meloda.fast.MainViewModel
|
import dev.meloda.fast.MainViewModel
|
||||||
import dev.meloda.fast.MainViewModelImpl
|
|
||||||
import dev.meloda.fast.auth.authNavGraph
|
import dev.meloda.fast.auth.authNavGraph
|
||||||
import dev.meloda.fast.auth.captcha.presentation.CaptchaScreen
|
import dev.meloda.fast.auth.captcha.presentation.CaptchaScreen
|
||||||
import dev.meloda.fast.auth.navigateToAuth
|
import dev.meloda.fast.auth.navigateToAuth
|
||||||
import dev.meloda.fast.chatmaterials.navigation.chatMaterialsScreen
|
import dev.meloda.fast.chatmaterials.navigation.chatMaterialsScreen
|
||||||
import dev.meloda.fast.chatmaterials.navigation.navigateToChatMaterials
|
import dev.meloda.fast.chatmaterials.navigation.navigateToChatMaterials
|
||||||
import dev.meloda.fast.common.LongPollController
|
import dev.meloda.fast.common.LongPollController
|
||||||
|
import dev.meloda.fast.common.NetworkStateListener
|
||||||
import dev.meloda.fast.common.model.LongPollState
|
import dev.meloda.fast.common.model.LongPollState
|
||||||
import dev.meloda.fast.convos.navigation.createChatScreen
|
import dev.meloda.fast.convos.navigation.createChatScreen
|
||||||
import dev.meloda.fast.convos.navigation.navigateToCreateChat
|
import dev.meloda.fast.convos.navigation.navigateToCreateChat
|
||||||
@@ -56,9 +59,12 @@ import dev.meloda.fast.model.api.domain.VkUser
|
|||||||
import dev.meloda.fast.navigation.Main
|
import dev.meloda.fast.navigation.Main
|
||||||
import dev.meloda.fast.navigation.mainScreen
|
import dev.meloda.fast.navigation.mainScreen
|
||||||
import dev.meloda.fast.photoviewer.presentation.PhotoViewDialog
|
import dev.meloda.fast.photoviewer.presentation.PhotoViewDialog
|
||||||
|
import dev.meloda.fast.settings.model.SettingsNavigationIntent
|
||||||
import dev.meloda.fast.settings.navigation.navigateToSettings
|
import dev.meloda.fast.settings.navigation.navigateToSettings
|
||||||
import dev.meloda.fast.settings.navigation.settingsScreen
|
import dev.meloda.fast.settings.navigation.settingsScreen
|
||||||
import dev.meloda.fast.ui.R
|
import dev.meloda.fast.ui.R
|
||||||
|
import dev.meloda.fast.ui.common.LocalLogger
|
||||||
|
import dev.meloda.fast.ui.common.LocalNetworkState
|
||||||
import dev.meloda.fast.ui.common.LocalSizeConfig
|
import dev.meloda.fast.ui.common.LocalSizeConfig
|
||||||
import dev.meloda.fast.ui.model.DeviceSize
|
import dev.meloda.fast.ui.model.DeviceSize
|
||||||
import dev.meloda.fast.ui.model.SizeConfig
|
import dev.meloda.fast.ui.model.SizeConfig
|
||||||
@@ -79,6 +85,7 @@ fun RootScreen(
|
|||||||
toggleLongPollService: (enable: Boolean, inBackground: Boolean?) -> Unit,
|
toggleLongPollService: (enable: Boolean, inBackground: Boolean?) -> Unit,
|
||||||
toggleOnlineService: (enable: Boolean) -> Unit
|
toggleOnlineService: (enable: Boolean) -> Unit
|
||||||
) {
|
) {
|
||||||
|
val logger = LocalLogger.current
|
||||||
val resources = LocalResources.current
|
val resources = LocalResources.current
|
||||||
|
|
||||||
val userSettings: UserSettings = koinInject()
|
val userSettings: UserSettings = koinInject()
|
||||||
@@ -87,8 +94,7 @@ fun RootScreen(
|
|||||||
val longPollCurrentState by longPollController.currentState.collectAsStateWithLifecycle()
|
val longPollCurrentState by longPollController.currentState.collectAsStateWithLifecycle()
|
||||||
val longPollStateToApply by longPollController.stateToApply.collectAsStateWithLifecycle()
|
val longPollStateToApply by longPollController.stateToApply.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
|
val viewModel: MainViewModel = koinViewModel()
|
||||||
|
|
||||||
val currentUser: VkUser? by viewModel.currentUser.collectAsStateWithLifecycle()
|
val currentUser: VkUser? by viewModel.currentUser.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val permissionState =
|
val permissionState =
|
||||||
@@ -119,6 +125,7 @@ fun RootScreen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
LifecycleResumeEffect(longPollStateToApply) {
|
LifecycleResumeEffect(longPollStateToApply) {
|
||||||
|
logger.debug("RootScreen", "longPollStateToApply: $longPollStateToApply")
|
||||||
if (longPollStateToApply != LongPollState.Background) {
|
if (longPollStateToApply != LongPollState.Background) {
|
||||||
if (longPollStateToApply.isLaunched() && longPollCurrentState.isLaunched()
|
if (longPollStateToApply.isLaunched() && longPollCurrentState.isLaunched()
|
||||||
&& longPollCurrentState != longPollStateToApply
|
&& longPollCurrentState != longPollStateToApply
|
||||||
@@ -215,10 +222,17 @@ fun RootScreen(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val networkStateListener: NetworkStateListener = koinInject()
|
||||||
|
val networkState by networkStateListener.networkStateFlow.collectAsStateWithLifecycle()
|
||||||
|
LaunchedEffect(networkState) {
|
||||||
|
logger.debug("RootScreen", "NetworkState: $networkState")
|
||||||
|
}
|
||||||
|
|
||||||
CompositionLocalProvider(
|
CompositionLocalProvider(
|
||||||
LocalThemeConfig provides themeConfig,
|
LocalThemeConfig provides themeConfig,
|
||||||
LocalSizeConfig provides sizeConfig,
|
LocalSizeConfig provides sizeConfig,
|
||||||
LocalUser provides currentUser
|
LocalUser provides currentUser,
|
||||||
|
LocalNetworkState provides networkState
|
||||||
) {
|
) {
|
||||||
AppTheme(
|
AppTheme(
|
||||||
useDarkTheme = themeConfig.darkMode,
|
useDarkTheme = themeConfig.darkMode,
|
||||||
@@ -232,7 +246,6 @@ fun RootScreen(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val startDestination by viewModel.startDestination.collectAsStateWithLifecycle()
|
val startDestination by viewModel.startDestination.collectAsStateWithLifecycle()
|
||||||
val isNeedToOpenAuth by viewModel.isNeedToReplaceWithAuth.collectAsStateWithLifecycle()
|
val isNeedToOpenAuth by viewModel.isNeedToReplaceWithAuth.collectAsStateWithLifecycle()
|
||||||
val baseError by viewModel.baseError.collectAsStateWithLifecycle()
|
|
||||||
val isNeedToShowDeniedDialog by viewModel.isNeedToShowNotificationsDeniedDialog.collectAsStateWithLifecycle()
|
val isNeedToShowDeniedDialog by viewModel.isNeedToShowNotificationsDeniedDialog.collectAsStateWithLifecycle()
|
||||||
val isNeedToShowRationaleDialog by viewModel.isNeedToShowNotificationsRationaleDialog.collectAsStateWithLifecycle()
|
val isNeedToShowRationaleDialog by viewModel.isNeedToShowNotificationsRationaleDialog.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
@@ -296,12 +309,6 @@ fun RootScreen(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
RootErrorDialog(
|
|
||||||
baseError = baseError,
|
|
||||||
onDismiss = viewModel::onErrorConsumed,
|
|
||||||
onConfirm = viewModel::onErrorConsumed
|
|
||||||
)
|
|
||||||
|
|
||||||
if (startDestination != null) {
|
if (startDestination != null) {
|
||||||
CompositionLocalProvider(
|
CompositionLocalProvider(
|
||||||
LocalNavRootController provides navController,
|
LocalNavRootController provides navController,
|
||||||
@@ -364,18 +371,31 @@ fun RootScreen(
|
|||||||
)
|
)
|
||||||
|
|
||||||
settingsScreen(
|
settingsScreen(
|
||||||
onBack = navController::navigateUp,
|
handleNavigationIntent = { intent ->
|
||||||
onLogOutButtonClicked = { navController.navigateToAuth(true) },
|
when (intent) {
|
||||||
onLanguageItemClicked = navController::navigateToLanguagePicker,
|
SettingsNavigationIntent.Back -> navController.navigateUp()
|
||||||
onRestartRequired = {
|
SettingsNavigationIntent.Language -> navController.navigateToLanguagePicker()
|
||||||
|
SettingsNavigationIntent.Restart -> {
|
||||||
activity?.let {
|
activity?.let {
|
||||||
val intent = Intent(activity, MainActivity::class.java)
|
val intent =
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
Intent(activity, MainActivity::class.java)
|
||||||
activity.startActivity(intent)
|
intent.setFlags(
|
||||||
|
Intent.FLAG_ACTIVITY_NEW_TASK or
|
||||||
|
Intent.FLAG_ACTIVITY_CLEAR_TASK or
|
||||||
|
Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
|
)
|
||||||
activity.finish()
|
activity.finish()
|
||||||
|
activity.startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsNavigationIntent.LogOut -> {
|
||||||
|
navController.navigateToAuth(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
languagePickerScreen(onBack = navController::navigateUp)
|
languagePickerScreen(onBack = navController::navigateUp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import android.os.IBinder
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import dev.meloda.fast.common.extensions.createTimerFlow
|
import dev.meloda.fast.common.extensions.createTimerFlow
|
||||||
import dev.meloda.fast.data.UserConfig
|
import dev.meloda.fast.data.UserConfig
|
||||||
import dev.meloda.fast.domain.AccountUseCase
|
|
||||||
import dev.meloda.fast.data.processState
|
import dev.meloda.fast.data.processState
|
||||||
|
import dev.meloda.fast.domain.AccountUseCase
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -24,11 +25,12 @@ import kotlin.time.Duration.Companion.minutes
|
|||||||
|
|
||||||
class OnlineService : Service() {
|
class OnlineService : Service() {
|
||||||
|
|
||||||
|
private val logger: FastLogger by inject()
|
||||||
|
|
||||||
private val job = SupervisorJob()
|
private val job = SupervisorJob()
|
||||||
|
|
||||||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
|
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
|
||||||
Log.d(TAG, "error: $throwable")
|
logger.error(this::class.java, "CoroutineException", throwable)
|
||||||
throwable.printStackTrace()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val coroutineContext: CoroutineContext
|
private val coroutineContext: CoroutineContext
|
||||||
@@ -42,17 +44,20 @@ class OnlineService : Service() {
|
|||||||
private var onlineJob: Job? = null
|
private var onlineJob: Job? = null
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? {
|
override fun onBind(intent: Intent?): IBinder? {
|
||||||
Log.d(STATE_TAG, "onBind: intent: $intent")
|
logger.debug(this::class, "STATE: onBind(): intent: $intent")
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (startId > 1) return START_STICKY
|
if (startId > 1) return START_STICKY
|
||||||
|
|
||||||
Log.d(STATE_TAG, "onStartCommand: flags: $flags; startId: $startId\ninstance: $this")
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"STATE: onStartCommand(): flags: %s; startId: %s;\ninstance: %s"
|
||||||
|
.format("$flags", "$startId", "$this")
|
||||||
|
)
|
||||||
|
|
||||||
createTimer()
|
createTimer()
|
||||||
|
|
||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,13 +73,13 @@ class OnlineService : Service() {
|
|||||||
private fun setOnline() {
|
private fun setOnline() {
|
||||||
if (onlineJob != null) return
|
if (onlineJob != null) return
|
||||||
|
|
||||||
Log.d(TAG, "setOnline()")
|
logger.debug(this::class, "setOnline()")
|
||||||
|
|
||||||
onlineJob = coroutineScope.launch {
|
onlineJob = coroutineScope.launch {
|
||||||
val token = UserConfig.fastToken ?: UserConfig.accessToken
|
val token = UserConfig.fastToken ?: UserConfig.accessToken
|
||||||
|
|
||||||
if (token.isBlank()) {
|
if (token.isBlank()) {
|
||||||
Log.d(TAG, "setOnline: token is empty")
|
logger.debug(this::class, "setOnline(): token is empty")
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,10 +89,10 @@ class OnlineService : Service() {
|
|||||||
).onEach { state ->
|
).onEach { state ->
|
||||||
state.processState(
|
state.processState(
|
||||||
error = { error ->
|
error = { error ->
|
||||||
Log.w(TAG, "setOnline(): error: $error")
|
logger.error(this@OnlineService::class, "setOnline(): ERROR: $error")
|
||||||
},
|
},
|
||||||
success = { response ->
|
success = { response ->
|
||||||
Log.d(TAG, "setOnline(): success: $response")
|
logger.debug(this@OnlineService::class, "setOnline(): response: $response")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}.collect()
|
}.collect()
|
||||||
@@ -96,7 +101,7 @@ class OnlineService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d(STATE_TAG, "onDestroy")
|
logger.debug(this::class, "onDestroy()")
|
||||||
|
|
||||||
timerJob?.cancel("OnlineService destroyed")
|
timerJob?.cancel("OnlineService destroyed")
|
||||||
onlineJob?.cancel("OnlineService destroyed")
|
onlineJob?.cancel("OnlineService destroyed")
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import android.net.Uri
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.util.Log
|
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.ServiceCompat
|
import androidx.core.app.ServiceCompat
|
||||||
import com.conena.nanokt.android.app.stopForegroundCompat
|
import com.conena.nanokt.android.app.stopForegroundCompat
|
||||||
@@ -16,17 +15,13 @@ import dev.meloda.fast.common.LongPollController
|
|||||||
import dev.meloda.fast.common.VkConstants
|
import dev.meloda.fast.common.VkConstants
|
||||||
import dev.meloda.fast.common.extensions.listenValue
|
import dev.meloda.fast.common.extensions.listenValue
|
||||||
import dev.meloda.fast.common.model.LongPollState
|
import dev.meloda.fast.common.model.LongPollState
|
||||||
import dev.meloda.fast.data.VkMemoryCache
|
|
||||||
import dev.meloda.fast.data.UserConfig
|
import dev.meloda.fast.data.UserConfig
|
||||||
import dev.meloda.fast.data.processState
|
import dev.meloda.fast.data.processState
|
||||||
import dev.meloda.fast.datastore.AppSettings
|
import dev.meloda.fast.datastore.AppSettings
|
||||||
|
import dev.meloda.fast.domain.LongPollEventsHandler
|
||||||
import dev.meloda.fast.domain.LongPollUpdatesParser
|
import dev.meloda.fast.domain.LongPollUpdatesParser
|
||||||
import dev.meloda.fast.domain.LongPollUseCase
|
import dev.meloda.fast.domain.LongPollUseCase
|
||||||
import dev.meloda.fast.domain.MessagesUseCase
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import dev.meloda.fast.domain.StoreUsersUseCase
|
|
||||||
import dev.meloda.fast.model.api.data.VkGroupData
|
|
||||||
import dev.meloda.fast.model.api.data.VkUserData
|
|
||||||
import dev.meloda.fast.model.api.data.asDomain
|
|
||||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import dev.meloda.fast.ui.R
|
import dev.meloda.fast.ui.R
|
||||||
@@ -39,7 +34,6 @@ import kotlinx.coroutines.SupervisorJob
|
|||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import kotlinx.coroutines.flow.last
|
|
||||||
import org.koin.android.ext.android.inject
|
import org.koin.android.ext.android.inject
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
@@ -47,6 +41,8 @@ import kotlin.time.Duration.Companion.seconds
|
|||||||
|
|
||||||
class LongPollingService : Service() {
|
class LongPollingService : Service() {
|
||||||
|
|
||||||
|
private val logger: FastLogger by inject()
|
||||||
|
|
||||||
private val longPollController: LongPollController by inject()
|
private val longPollController: LongPollController by inject()
|
||||||
|
|
||||||
private val job = SupervisorJob()
|
private val job = SupervisorJob()
|
||||||
@@ -62,9 +58,8 @@ class LongPollingService : Service() {
|
|||||||
private val coroutineScope = CoroutineScope(coroutineContext)
|
private val coroutineScope = CoroutineScope(coroutineContext)
|
||||||
|
|
||||||
private val longPollUseCase: LongPollUseCase by inject()
|
private val longPollUseCase: LongPollUseCase by inject()
|
||||||
private val messagesUseCase: MessagesUseCase by inject()
|
|
||||||
private val storeUsersUseCase: StoreUsersUseCase by inject()
|
|
||||||
private val updatesParser: LongPollUpdatesParser by inject()
|
private val updatesParser: LongPollUpdatesParser by inject()
|
||||||
|
private val eventsHandler: LongPollEventsHandler by inject()
|
||||||
|
|
||||||
private var currentJob: Job? = null
|
private var currentJob: Job? = null
|
||||||
|
|
||||||
@@ -72,20 +67,21 @@ class LongPollingService : Service() {
|
|||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
Log.d(STATE_TAG, "onCreate()")
|
logger.debug(this::class, "STATE: onCreate()")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? {
|
override fun onBind(intent: Intent?): IBinder? {
|
||||||
Log.d(STATE_TAG, "onBind: intent: $intent")
|
logger.debug(this::class, "STATE: onBind(): intent: $intent")
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (startId > 1) return START_STICKY
|
if (startId > 1) return START_STICKY
|
||||||
|
|
||||||
Log.d(
|
logger.debug(
|
||||||
STATE_TAG,
|
this::class,
|
||||||
"onStartCommand: asForeground: $inBackground; flags: $flags; startId: $startId;\ninstance: $this"
|
"STATE: onStartCommand(): asForeground: %s; flags: %s; startId: %s;\ninstance: %s"
|
||||||
|
.format("$inBackground", "$flags", "$startId", "$this")
|
||||||
)
|
)
|
||||||
|
|
||||||
startJob()
|
startJob()
|
||||||
@@ -140,11 +136,15 @@ class LongPollingService : Service() {
|
|||||||
|
|
||||||
private fun startPolling(): Job {
|
private fun startPolling(): Job {
|
||||||
if (job.isCompleted || job.isCancelled) {
|
if (job.isCompleted || job.isCancelled) {
|
||||||
Log.d(STATE_TAG, "Job is completed or cancelled")
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"startPolling(): Job is already done. isCompleted: %s; isCancelled: %s"
|
||||||
|
.format("${job.isCompleted}", "${job.isCancelled}")
|
||||||
|
)
|
||||||
throw Exception("Job is over")
|
throw Exception("Job is over")
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(STATE_TAG, "Starting job...")
|
logger.debug(this::class, "startPolling(): Starting job.")
|
||||||
|
|
||||||
return coroutineScope.launch(coroutineContext) {
|
return coroutineScope.launch(coroutineContext) {
|
||||||
longPollController.updateCurrentState(
|
longPollController.updateCurrentState(
|
||||||
@@ -159,8 +159,6 @@ class LongPollingService : Service() {
|
|||||||
var serverInfo = getServerInfo()
|
var serverInfo = getServerInfo()
|
||||||
?: throw LongPollException(message = "bad VK response (server info)")
|
?: throw LongPollException(message = "bad VK response (server info)")
|
||||||
|
|
||||||
syncLongPollHistory(serverInfo)
|
|
||||||
|
|
||||||
var lastUpdatesResponse: LongPollUpdates? = getUpdatesResponse(serverInfo)
|
var lastUpdatesResponse: LongPollUpdates? = getUpdatesResponse(serverInfo)
|
||||||
?: throw LongPollException(message = "initiation error: bad VK response (last updates)")
|
?: throw LongPollException(message = "initiation error: bad VK response (last updates)")
|
||||||
|
|
||||||
@@ -171,7 +169,6 @@ class LongPollingService : Service() {
|
|||||||
failCount++
|
failCount++
|
||||||
serverInfo = getServerInfo()
|
serverInfo = getServerInfo()
|
||||||
?: throw LongPollException(message = "failed retrieving server info after error: bad VK response (server info #2)")
|
?: throw LongPollException(message = "failed retrieving server info after error: bad VK response (server info #2)")
|
||||||
syncLongPollHistory(serverInfo)
|
|
||||||
lastUpdatesResponse = getUpdatesResponse(serverInfo)
|
lastUpdatesResponse = getUpdatesResponse(serverInfo)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -191,7 +188,6 @@ class LongPollingService : Service() {
|
|||||||
?: throw LongPollException(
|
?: throw LongPollException(
|
||||||
message = "failed retrieving server info after error: bad VK response (server info #3)"
|
message = "failed retrieving server info after error: bad VK response (server info #3)"
|
||||||
)
|
)
|
||||||
syncLongPollHistory(serverInfo)
|
|
||||||
lastUpdatesResponse = getUpdatesResponse(serverInfo)
|
lastUpdatesResponse = getUpdatesResponse(serverInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,12 +202,9 @@ class LongPollingService : Service() {
|
|||||||
if (updates == null) {
|
if (updates == null) {
|
||||||
failCount++
|
failCount++
|
||||||
} else {
|
} else {
|
||||||
updates.forEach(updatesParser::parseNextUpdate)
|
parseUpdates(updates)
|
||||||
}
|
}
|
||||||
|
|
||||||
AppSettings.LongPoll.ts = lastUpdatesResponse.ts ?: serverInfo.ts
|
|
||||||
AppSettings.LongPoll.pts = lastUpdatesResponse.pts ?: AppSettings.LongPoll.pts
|
|
||||||
|
|
||||||
lastUpdatesResponse = getUpdatesResponse(serverInfo.copy(ts = newTs))
|
lastUpdatesResponse = getUpdatesResponse(serverInfo.copy(ts = newTs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,11 +220,11 @@ class LongPollingService : Service() {
|
|||||||
).listenValue(coroutineScope) { state ->
|
).listenValue(coroutineScope) { state ->
|
||||||
state.processState(
|
state.processState(
|
||||||
success = { response ->
|
success = { response ->
|
||||||
Log.d(TAG, "getServerInfo: serverInfoResponse: $response")
|
logger.debug(this::class, "getServerInfo(): response: $response")
|
||||||
it.resume(response)
|
it.resume(response)
|
||||||
},
|
},
|
||||||
error = { error ->
|
error = { error ->
|
||||||
Log.e(TAG, "getServerInfo: $error")
|
logger.error(this::class, "getServerInfo(): ERROR: $error")
|
||||||
it.resume(null)
|
it.resume(null)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -251,86 +244,24 @@ class LongPollingService : Service() {
|
|||||||
).listenValue(coroutineScope) { state ->
|
).listenValue(coroutineScope) { state ->
|
||||||
state.processState(
|
state.processState(
|
||||||
success = { response ->
|
success = { response ->
|
||||||
Log.d(TAG, "lastUpdateResponse: $response")
|
logger.debug(this::class, "getUpdatesResponse(): response: $response")
|
||||||
it.resume(response)
|
it.resume(response)
|
||||||
},
|
},
|
||||||
error = { error ->
|
error = { error ->
|
||||||
Log.d(TAG, "getUpdatesResponse: error: $error")
|
logger.debug(this::class, "getUpdatesResponse(): error: $error")
|
||||||
it.resume(null)
|
it.resume(null)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun syncLongPollHistory(serverInfo: VkLongPollData) {
|
private suspend fun parseUpdates(updates: List<List<Any>>) {
|
||||||
val cursorTs = AppSettings.LongPoll.ts ?: serverInfo.ts
|
val parsedUpdates = updates.flatMap { updatesParser.parseNextUpdate(it) }
|
||||||
val cursorPts = AppSettings.LongPoll.pts ?: serverInfo.pts
|
eventsHandler.handleEvents(parsedUpdates)
|
||||||
val maxMsgId = messagesUseCase.getLocalMaxMessageId()
|
|
||||||
|
|
||||||
var currentTs = cursorTs
|
|
||||||
var currentPts = cursorPts
|
|
||||||
var more: Int?
|
|
||||||
|
|
||||||
do {
|
|
||||||
val historyResponse = getLongPollHistory(
|
|
||||||
ts = currentTs,
|
|
||||||
pts = currentPts,
|
|
||||||
maxMsgId = maxMsgId
|
|
||||||
) ?: return
|
|
||||||
|
|
||||||
historyResponse.history.orEmpty().forEach(updatesParser::parseNextUpdate)
|
|
||||||
|
|
||||||
historyResponse.messages?.items.orEmpty().takeIf { it.isNotEmpty() }?.let { rawMessages ->
|
|
||||||
val messages = rawMessages.map { it.asDomain() }
|
|
||||||
messagesUseCase.storeMessages(messages)
|
|
||||||
}
|
|
||||||
|
|
||||||
historyResponse.profiles.orEmpty().takeIf { it.isNotEmpty() }?.let { profiles ->
|
|
||||||
val users = profiles.map(VkUserData::mapToDomain)
|
|
||||||
VkMemoryCache.appendUsers(users)
|
|
||||||
storeUsersUseCase(users).last()
|
|
||||||
}
|
|
||||||
|
|
||||||
historyResponse.groups.orEmpty().takeIf { it.isNotEmpty() }?.let { groups ->
|
|
||||||
VkMemoryCache.appendGroups(groups.map(VkGroupData::mapToDomain))
|
|
||||||
}
|
|
||||||
|
|
||||||
currentTs = historyResponse.ts ?: historyResponse.fromPts ?: currentTs
|
|
||||||
currentPts = historyResponse.newPts ?: historyResponse.pts ?: currentPts
|
|
||||||
more = historyResponse.more
|
|
||||||
|
|
||||||
AppSettings.LongPoll.ts = currentTs
|
|
||||||
AppSettings.LongPoll.pts = currentPts
|
|
||||||
} while (more == 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getLongPollHistory(
|
|
||||||
ts: Int,
|
|
||||||
pts: Int,
|
|
||||||
maxMsgId: Long?
|
|
||||||
): dev.meloda.fast.model.api.data.LongPollHistoryResponse? = suspendCancellableCoroutine {
|
|
||||||
longPollUseCase.getLongPollHistory(
|
|
||||||
ts = ts,
|
|
||||||
pts = pts,
|
|
||||||
lpVersion = VkConstants.LP_VERSION,
|
|
||||||
maxMsgId = maxMsgId,
|
|
||||||
eventsLimit = 1000,
|
|
||||||
msgsLimit = 200
|
|
||||||
).listenValue(coroutineScope) { state ->
|
|
||||||
state.processState(
|
|
||||||
success = { response ->
|
|
||||||
it.resume(response)
|
|
||||||
},
|
|
||||||
error = { error ->
|
|
||||||
Log.e(TAG, "getLongPollHistory: error: $error")
|
|
||||||
it.resume(null)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleError(throwable: Throwable) {
|
private fun handleError(throwable: Throwable) {
|
||||||
Log.e(TAG, "error: $throwable")
|
logger.error(this::class, "CoroutineException", throwable)
|
||||||
|
|
||||||
if (throwable !is NoAccessTokenException) {
|
if (throwable !is NoAccessTokenException) {
|
||||||
throwable.printStackTrace()
|
throwable.printStackTrace()
|
||||||
@@ -345,7 +276,7 @@ class LongPollingService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d(STATE_TAG, "onDestroy")
|
logger.debug(this::class, "STATE: onDestroy()")
|
||||||
longPollController.updateCurrentState(LongPollState.Stopped)
|
longPollController.updateCurrentState(LongPollState.Stopped)
|
||||||
try {
|
try {
|
||||||
AppSettings.edit { putBoolean(KEY_LONG_POLL_WAS_DESTROYED, true) }
|
AppSettings.edit { putBoolean(KEY_LONG_POLL_WAS_DESTROYED, true) }
|
||||||
@@ -357,7 +288,7 @@ class LongPollingService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onTrimMemory(level: Int) {
|
override fun onTrimMemory(level: Int) {
|
||||||
Log.d(STATE_TAG, "onTrimMemory. Level: $level")
|
logger.debug(this::class, "STATE: onTrimMemory(): Level: $level")
|
||||||
super.onTrimMemory(level)
|
super.onTrimMemory(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package dev.meloda.fast.service.longpolling.di
|
package dev.meloda.fast.service.longpolling.di
|
||||||
|
|
||||||
|
import dev.meloda.fast.domain.LongPollEventsHandler
|
||||||
import dev.meloda.fast.domain.LongPollUpdatesParser
|
import dev.meloda.fast.domain.LongPollUpdatesParser
|
||||||
import dev.meloda.fast.domain.LongPollUpdatesReducer
|
|
||||||
import dev.meloda.fast.domain.LongPollUseCase
|
import dev.meloda.fast.domain.LongPollUseCase
|
||||||
import dev.meloda.fast.domain.LongPollUseCaseImpl
|
import dev.meloda.fast.domain.LongPollUseCaseImpl
|
||||||
import org.koin.core.module.dsl.singleOf
|
import org.koin.core.module.dsl.singleOf
|
||||||
@@ -11,5 +11,5 @@ import org.koin.dsl.module
|
|||||||
val longPollModule = module {
|
val longPollModule = module {
|
||||||
singleOf(::LongPollUseCaseImpl) bind LongPollUseCase::class
|
singleOf(::LongPollUseCaseImpl) bind LongPollUseCase::class
|
||||||
singleOf(::LongPollUpdatesParser)
|
singleOf(::LongPollUpdatesParser)
|
||||||
singleOf(::LongPollUpdatesReducer)
|
singleOf(::LongPollEventsHandler)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
|
|||||||
compileSdk = getVersionInt("compileSdk")
|
compileSdk = getVersionInt("compileSdk")
|
||||||
targetSdk = getVersionInt("targetSdk")
|
targetSdk = getVersionInt("targetSdk")
|
||||||
}
|
}
|
||||||
|
lint {
|
||||||
|
abortOnError = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import com.android.build.api.dsl.LibraryExtension
|
|||||||
import com.android.build.api.variant.LibraryAndroidComponentsExtension
|
import com.android.build.api.variant.LibraryAndroidComponentsExtension
|
||||||
import dev.meloda.fast.configureKotlinAndroid
|
import dev.meloda.fast.configureKotlinAndroid
|
||||||
import dev.meloda.fast.disableUnnecessaryAndroidTests
|
import dev.meloda.fast.disableUnnecessaryAndroidTests
|
||||||
|
import dev.meloda.fast.getVersionInt
|
||||||
import org.gradle.api.Plugin
|
import org.gradle.api.Plugin
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.kotlin.dsl.configure
|
import org.gradle.kotlin.dsl.configure
|
||||||
@@ -20,7 +21,13 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
|
|||||||
extensions.configure<LibraryExtension> {
|
extensions.configure<LibraryExtension> {
|
||||||
configureKotlinAndroid(this)
|
configureKotlinAndroid(this)
|
||||||
androidResources.enable = false
|
androidResources.enable = false
|
||||||
defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
defaultConfig {
|
||||||
|
minSdk = getVersionInt("minSdk")
|
||||||
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
}
|
||||||
|
lint {
|
||||||
|
abortOnError = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
extensions.configure<LibraryAndroidComponentsExtension> {
|
extensions.configure<LibraryAndroidComponentsExtension> {
|
||||||
disableUnnecessaryAndroidTests(target)
|
disableUnnecessaryAndroidTests(target)
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ internal fun Project.configureKotlinAndroid(
|
|||||||
|
|
||||||
commonExtension.apply {
|
commonExtension.apply {
|
||||||
compileSdk = getVersionInt("compileSdk")
|
compileSdk = getVersionInt("compileSdk")
|
||||||
buildToolsVersion = "36.1.0"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
configureKotlin<KotlinAndroidProjectExtension>()
|
configureKotlin<KotlinAndroidProjectExtension>()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ object AppConstants {
|
|||||||
|
|
||||||
const val INSTALL_APP_MIME_TYPE = "application/vnd.android.package-archive"
|
const val INSTALL_APP_MIME_TYPE = "application/vnd.android.package-archive"
|
||||||
|
|
||||||
const val API_VERSION = "5.238"
|
const val API_VERSION = "5.263"
|
||||||
const val URL_OAUTH = "https://oauth.vk.ru"
|
const val URL_OAUTH = "https://oauth.vk.ru"
|
||||||
const val URL_API = "https://api.vk.ru/method"
|
const val URL_API = "https://api.vk.ru/method"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package dev.meloda.fast.common
|
||||||
|
|
||||||
|
import dev.meloda.fast.common.model.NetworkState
|
||||||
|
import dev.meloda.fast.common.model.NetworkStatus
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
|
||||||
|
class NetworkStateListener {
|
||||||
|
|
||||||
|
private val networkStatus = MutableStateFlow(NetworkStatus.UNAVAILABLE)
|
||||||
|
val networkStatusFlow = networkStatus.asStateFlow()
|
||||||
|
|
||||||
|
private val networkState = MutableStateFlow(NetworkState.DISCONNECTED)
|
||||||
|
val networkStateFlow = networkState.asStateFlow()
|
||||||
|
|
||||||
|
fun updateNetworkState(state: NetworkState) {
|
||||||
|
networkState.value = state
|
||||||
|
networkStatus.value = when (state) {
|
||||||
|
NetworkState.CONNECTED -> NetworkStatus.AVAILABLE
|
||||||
|
NetworkState.DISCONNECTED -> NetworkStatus.UNAVAILABLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,10 +2,13 @@ package dev.meloda.fast.common.extensions
|
|||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
@@ -41,19 +44,21 @@ fun <T> MutableList<T>.removeIfCompat(condition: (T) -> Boolean): Boolean {
|
|||||||
return removed
|
return removed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context(viewModel: ViewModel)
|
||||||
|
fun <T> Flow<T>.listenValue(action: suspend (T) -> Unit): Job =
|
||||||
|
listenValue(viewModel.viewModelScope, action)
|
||||||
|
|
||||||
|
context(viewModel: ViewModel)
|
||||||
|
fun <T> MutableSharedFlow<T>.emitOnMain(value: T) {
|
||||||
|
val flow = this
|
||||||
|
viewModel.viewModelScope.launch { flow.emit(value) }
|
||||||
|
}
|
||||||
|
|
||||||
fun <T> Flow<T>.listenValue(
|
fun <T> Flow<T>.listenValue(
|
||||||
coroutineScope: CoroutineScope,
|
coroutineScope: CoroutineScope,
|
||||||
action: suspend (T) -> Unit
|
action: suspend (T) -> Unit
|
||||||
): Job = onEach(action::invoke).launchIn(coroutineScope)
|
): Job = onEach(action::invoke).launchIn(coroutineScope)
|
||||||
|
|
||||||
fun CoroutineScope.launchDbRefresh(
|
|
||||||
load: suspend () -> Unit,
|
|
||||||
after: suspend () -> Unit
|
|
||||||
): Job = launch {
|
|
||||||
load()
|
|
||||||
after()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createTimerFlow(
|
fun createTimerFlow(
|
||||||
time: Int,
|
time: Int,
|
||||||
onStartAction: (suspend () -> Unit)? = null,
|
onStartAction: (suspend () -> Unit)? = null,
|
||||||
|
|||||||
+2
-2
@@ -1,13 +1,13 @@
|
|||||||
package dev.meloda.fast.common.model
|
package dev.meloda.fast.common.model
|
||||||
|
|
||||||
enum class LogLevel(val value: Int) {
|
enum class NetworkLogLevel(val value: Int) {
|
||||||
NONE(0),
|
NONE(0),
|
||||||
BASIC(1),
|
BASIC(1),
|
||||||
HEADERS(2),
|
HEADERS(2),
|
||||||
BODY(3);
|
BODY(3);
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun parse(value: Int): LogLevel = entries.firstOrNull { it.value == value }
|
fun parse(value: Int): NetworkLogLevel = entries.firstOrNull { it.value == value }
|
||||||
?: throw IllegalArgumentException("Unknown log level with value: $value")
|
?: throw IllegalArgumentException("Unknown log level with value: $value")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package dev.meloda.fast.common.model
|
||||||
|
|
||||||
|
enum class NetworkState { CONNECTED, DISCONNECTED }
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package dev.meloda.fast.common.model
|
||||||
|
|
||||||
|
enum class NetworkStatus {
|
||||||
|
AVAILABLE, UNAVAILABLE, LOST, BLOCKED, UNBLOCKED;
|
||||||
|
|
||||||
|
fun isOk(): Boolean = when (this) {
|
||||||
|
AVAILABLE, UNBLOCKED -> true
|
||||||
|
UNAVAILABLE, LOST, BLOCKED -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package dev.meloda.fast.common.model
|
||||||
|
|
||||||
|
enum class NetworkType {
|
||||||
|
CELLULAR, WIFI, UNKNOWN
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package dev.meloda.fast.common.paging
|
|
||||||
|
|
||||||
fun canPaginate(pageSize: Int, loadedCount: Int): Boolean = loadedCount == pageSize
|
|
||||||
|
|
||||||
fun isPaginationExhausted(
|
|
||||||
pageSize: Int,
|
|
||||||
loadedCount: Int,
|
|
||||||
hasExistingItems: Boolean
|
|
||||||
): Boolean = loadedCount != pageSize && hasExistingItems
|
|
||||||
|
|
||||||
fun <T> mergePage(
|
|
||||||
existing: List<T>,
|
|
||||||
page: List<T>,
|
|
||||||
offset: Int
|
|
||||||
): List<T> = if (offset == 0) page else existing + page
|
|
||||||
|
|
||||||
data class LoadingFlags(
|
|
||||||
val isLoading: Boolean,
|
|
||||||
val isPaginating: Boolean
|
|
||||||
)
|
|
||||||
|
|
||||||
fun loadingFlags(offset: Int, isLoading: Boolean): LoadingFlags = LoadingFlags(
|
|
||||||
isLoading = offset == 0 && isLoading,
|
|
||||||
isPaginating = offset > 0 && isLoading
|
|
||||||
)
|
|
||||||
@@ -3,9 +3,7 @@ package dev.meloda.fast.common.provider
|
|||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
|
|
||||||
interface ResourceProvider {
|
interface ResourceProvider {
|
||||||
|
|
||||||
val resources: Resources
|
val resources: Resources
|
||||||
|
|
||||||
fun getString(resId: Int): String
|
fun getString(resId: Int): String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
package dev.meloda.fast.data
|
package dev.meloda.fast.data
|
||||||
|
|
||||||
|
import androidx.core.net.toUri
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
import java.net.URLEncoder
|
||||||
|
|
||||||
class AccessTokenInterceptor : Interceptor {
|
class AccessTokenInterceptor : Interceptor {
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val builder = chain.request().url.newBuilder()
|
||||||
val urlBuilder = request.url.newBuilder()
|
|
||||||
|
|
||||||
if (request.url.queryParameter("access_token") == null) {
|
val uri = builder.build().toUri().toString().toUri()
|
||||||
urlBuilder.addQueryParameter("access_token", UserConfig.accessToken)
|
|
||||||
|
if (uri.getQueryParameter("access_token") == null) {
|
||||||
|
builder.addQueryParameter(
|
||||||
|
"access_token",
|
||||||
|
URLEncoder.encode(UserConfig.accessToken, "utf-8")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return chain.proceed(request.newBuilder().url(urlBuilder.build()).build())
|
return chain.proceed(chain.request().newBuilder().apply { url(builder.build()) }.build())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,6 @@ object UserConfig {
|
|||||||
accessToken = ""
|
accessToken = ""
|
||||||
fastToken = ""
|
fastToken = ""
|
||||||
userId = -1
|
userId = -1
|
||||||
trustedHash = null
|
|
||||||
exchangeToken = null
|
|
||||||
AppSettings.LongPoll.clear()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isLoggedIn(): Boolean {
|
fun isLoggedIn(): Boolean {
|
||||||
|
|||||||
@@ -21,10 +21,12 @@ class VkGroupsMap(
|
|||||||
else map[abs(convo.id)]
|
else map[abs(convo.id)]
|
||||||
|
|
||||||
fun messageActionGroup(message: VkMessage): VkGroupDomain? =
|
fun messageActionGroup(message: VkMessage): VkGroupDomain? =
|
||||||
message.actionMemberId?.takeIf { it < 0 }?.let { map[abs(it)] }
|
if (message.actionMemberId == null || message.actionMemberId!! >= 0) null
|
||||||
|
else map[abs(message.actionMemberId!!)]
|
||||||
|
|
||||||
fun messageActionGroup(message: VkMessageData): VkGroupDomain? =
|
fun messageActionGroup(message: VkMessageData): VkGroupDomain? =
|
||||||
message.action?.memberId?.takeIf { it < 0 }?.let { map[abs(it)] }
|
if (message.action?.memberId == null || message.action!!.memberId!! >= 0) null
|
||||||
|
else map[abs(message.action!!.memberId!!)]
|
||||||
|
|
||||||
fun messageGroup(message: VkMessage): VkGroupDomain? =
|
fun messageGroup(message: VkMessage): VkGroupDomain? =
|
||||||
if (!message.isGroup()) null
|
if (!message.isGroup()) null
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package dev.meloda.fast.data
|
package dev.meloda.fast.data
|
||||||
|
|
||||||
|
import dev.meloda.fast.data.UserConfig.userId
|
||||||
import dev.meloda.fast.model.api.domain.VkContactDomain
|
import dev.meloda.fast.model.api.domain.VkContactDomain
|
||||||
import dev.meloda.fast.model.api.domain.VkConvo
|
import dev.meloda.fast.model.api.domain.VkConvo
|
||||||
import dev.meloda.fast.model.api.domain.VkGroupDomain
|
import dev.meloda.fast.model.api.domain.VkGroupDomain
|
||||||
@@ -34,18 +35,10 @@ object VkMemoryCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun appendContacts(contacts: List<VkContactDomain>) {
|
fun appendContacts(contacts: List<VkContactDomain>) {
|
||||||
contacts.forEach { contact -> VkMemoryCache.contacts[contact.userId] = contact }
|
contacts.forEach { contact -> VkMemoryCache[contact.userId] = contact }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clear() {
|
operator fun set(userid: Long, user: VkUser) {
|
||||||
users.clear()
|
|
||||||
groups.clear()
|
|
||||||
messages.clear()
|
|
||||||
convos.clear()
|
|
||||||
contacts.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
operator fun set(userId: Long, user: VkUser) {
|
|
||||||
users[userId] = user
|
users[userId] = user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,12 @@ class VkUsersMap(
|
|||||||
else map[convo.id]
|
else map[convo.id]
|
||||||
|
|
||||||
fun messageActionUser(message: VkMessage): VkUser? =
|
fun messageActionUser(message: VkMessage): VkUser? =
|
||||||
message.actionMemberId?.takeIf { it > 0 }?.let(map::get)
|
if (message.actionMemberId == null || message.actionMemberId!! <= 0) null
|
||||||
|
else map[message.actionMemberId]
|
||||||
|
|
||||||
fun messageActionUser(message: VkMessageData): VkUser? =
|
fun messageActionUser(message: VkMessageData): VkUser? =
|
||||||
message.action?.memberId?.takeIf { it > 0 }?.let(map::get)
|
if (message.action?.memberId == null || message.action!!.memberId!! <= 0) null
|
||||||
|
else map[message.action!!.memberId]
|
||||||
|
|
||||||
fun messageUser(message: VkMessage): VkUser? =
|
fun messageUser(message: VkMessage): VkUser? =
|
||||||
if (!message.isUser()) null
|
if (!message.isUser()) null
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ import dev.meloda.fast.network.RestApiErrorDomain
|
|||||||
interface ConvosRepository {
|
interface ConvosRepository {
|
||||||
|
|
||||||
suspend fun storeConvos(convos: List<VkConvo>)
|
suspend fun storeConvos(convos: List<VkConvo>)
|
||||||
suspend fun getLocalConvos(): List<VkConvo>
|
|
||||||
suspend fun getLocalConvoById(peerId: Long): VkConvo?
|
|
||||||
suspend fun deleteLocalConvo(peerId: Long)
|
|
||||||
|
|
||||||
suspend fun getConvos(
|
suspend fun getConvos(
|
||||||
count: Int?,
|
count: Int?,
|
||||||
|
|||||||
@@ -19,13 +19,11 @@ import dev.meloda.fast.model.api.domain.VkGroupDomain
|
|||||||
import dev.meloda.fast.model.api.domain.VkMessage
|
import dev.meloda.fast.model.api.domain.VkMessage
|
||||||
import dev.meloda.fast.model.api.domain.VkUser
|
import dev.meloda.fast.model.api.domain.VkUser
|
||||||
import dev.meloda.fast.model.api.domain.asEntity
|
import dev.meloda.fast.model.api.domain.asEntity
|
||||||
import dev.meloda.fast.model.database.VkConvoEntity
|
|
||||||
import dev.meloda.fast.model.api.requests.ConvosGetRequest
|
import dev.meloda.fast.model.api.requests.ConvosGetRequest
|
||||||
import dev.meloda.fast.network.RestApiErrorDomain
|
import dev.meloda.fast.network.RestApiErrorDomain
|
||||||
import dev.meloda.fast.network.mapApiDefault
|
import dev.meloda.fast.network.mapApiDefault
|
||||||
import dev.meloda.fast.network.mapApiResult
|
import dev.meloda.fast.network.mapApiResult
|
||||||
import dev.meloda.fast.network.service.convos.ConvosService
|
import dev.meloda.fast.network.service.convos.ConvosService
|
||||||
import dev.meloda.fast.model.database.asExternalModel
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -42,28 +40,6 @@ class ConvosRepositoryImpl(
|
|||||||
convoDao.insertAll(convos.map(VkConvo::asEntity))
|
convoDao.insertAll(convos.map(VkConvo::asEntity))
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getLocalConvos(): List<VkConvo> = withContext(Dispatchers.IO) {
|
|
||||||
convoDao.getAllWithMessage().map { convoWithMessage ->
|
|
||||||
convoWithMessage.convo.asExternalModel().copy(
|
|
||||||
lastMessage = convoWithMessage.message?.asExternalModel()
|
|
||||||
)
|
|
||||||
}.sorted()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalConvoById(peerId: Long): VkConvo? = withContext(Dispatchers.IO) {
|
|
||||||
convoDao.getByIdWithMessage(peerId)?.let { convoWithMessage ->
|
|
||||||
convoWithMessage.convo.asExternalModel().copy(
|
|
||||||
lastMessage = convoWithMessage.message?.asExternalModel()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteLocalConvo(peerId: Long) {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
convoDao.deleteById(peerId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getConvos(
|
override suspend fun getConvos(
|
||||||
count: Int?,
|
count: Int?,
|
||||||
offset: Int?,
|
offset: Int?,
|
||||||
@@ -153,6 +129,10 @@ class ConvosRepositoryImpl(
|
|||||||
val groupsList = response.groups.orEmpty().map(VkGroupData::mapToDomain)
|
val groupsList = response.groups.orEmpty().map(VkGroupData::mapToDomain)
|
||||||
val contactsList = response.contacts.orEmpty().map(VkContactData::mapToDomain)
|
val contactsList = response.contacts.orEmpty().map(VkContactData::mapToDomain)
|
||||||
|
|
||||||
|
VkMemoryCache.appendUsers(profilesList)
|
||||||
|
VkMemoryCache.appendGroups(groupsList)
|
||||||
|
VkMemoryCache.appendContacts(contactsList)
|
||||||
|
|
||||||
val usersMap = VkUsersMap.forUsers(profilesList)
|
val usersMap = VkUsersMap.forUsers(profilesList)
|
||||||
val groupsMap = VkGroupsMap.forGroups(groupsList)
|
val groupsMap = VkGroupsMap.forGroups(groupsList)
|
||||||
|
|
||||||
@@ -171,10 +151,6 @@ class ConvosRepositoryImpl(
|
|||||||
groupDao.insertAll(groupsList.map(VkGroupDomain::asEntity))
|
groupDao.insertAll(groupsList.map(VkGroupDomain::asEntity))
|
||||||
}
|
}
|
||||||
|
|
||||||
VkMemoryCache.appendUsers(profilesList)
|
|
||||||
VkMemoryCache.appendGroups(groupsList)
|
|
||||||
VkMemoryCache.appendContacts(contactsList)
|
|
||||||
|
|
||||||
convos
|
convos
|
||||||
},
|
},
|
||||||
errorMapper = { error ->
|
errorMapper = { error ->
|
||||||
@@ -222,28 +198,4 @@ class ConvosRepositoryImpl(
|
|||||||
): ApiResult<Int, RestApiErrorDomain> = withContext(Dispatchers.IO) {
|
): ApiResult<Int, RestApiErrorDomain> = withContext(Dispatchers.IO) {
|
||||||
convosService.unarchive(mapOf("peer_id" to peerId.toString())).mapApiDefault()
|
convosService.unarchive(mapOf("peer_id" to peerId.toString())).mapApiDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<VkConvo>.sorted(): List<VkConvo> {
|
|
||||||
val newConvos = toMutableList()
|
|
||||||
|
|
||||||
val pinnedConvos = newConvos
|
|
||||||
.filter(VkConvo::isPinned)
|
|
||||||
.sortedWith { c1, c2 ->
|
|
||||||
val diff = c2.majorId - c1.majorId
|
|
||||||
|
|
||||||
if (diff == 0) {
|
|
||||||
c2.minorId - c1.minorId
|
|
||||||
} else {
|
|
||||||
diff
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
newConvos.removeAll(pinnedConvos)
|
|
||||||
newConvos.sortWith { c1, c2 ->
|
|
||||||
(c2.lastMessage?.date ?: 0) - (c1.lastMessage?.date ?: 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
newConvos.addAll(0, pinnedConvos)
|
|
||||||
return newConvos
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package dev.meloda.fast.data.api.friends
|
package dev.meloda.fast.data.api.friends
|
||||||
|
|
||||||
|
import com.slack.eithernet.ApiResult
|
||||||
|
import com.slack.eithernet.successOrElse
|
||||||
import dev.meloda.fast.common.VkConstants
|
import dev.meloda.fast.common.VkConstants
|
||||||
import dev.meloda.fast.data.VkMemoryCache
|
import dev.meloda.fast.data.VkMemoryCache
|
||||||
import dev.meloda.fast.database.dao.UserDao
|
import dev.meloda.fast.database.dao.UserDao
|
||||||
import dev.meloda.fast.model.FriendsInfo
|
import dev.meloda.fast.model.FriendsInfo
|
||||||
|
import dev.meloda.fast.model.api.data.VkContactData
|
||||||
import dev.meloda.fast.model.api.data.VkUserData
|
import dev.meloda.fast.model.api.data.VkUserData
|
||||||
import dev.meloda.fast.model.api.domain.VkUser
|
import dev.meloda.fast.model.api.domain.VkUser
|
||||||
import dev.meloda.fast.model.api.domain.asEntity
|
import dev.meloda.fast.model.api.domain.asEntity
|
||||||
@@ -13,8 +16,6 @@ import dev.meloda.fast.network.RestApiErrorDomain
|
|||||||
import dev.meloda.fast.network.mapApiDefault
|
import dev.meloda.fast.network.mapApiDefault
|
||||||
import dev.meloda.fast.network.mapApiResult
|
import dev.meloda.fast.network.mapApiResult
|
||||||
import dev.meloda.fast.network.service.friends.FriendsService
|
import dev.meloda.fast.network.service.friends.FriendsService
|
||||||
import com.slack.eithernet.ApiResult
|
|
||||||
import com.slack.eithernet.successOrElse
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -51,14 +52,18 @@ class FriendsRepositoryImpl(
|
|||||||
order = order,
|
order = order,
|
||||||
count = count,
|
count = count,
|
||||||
offset = offset,
|
offset = offset,
|
||||||
fields = VkConstants.USER_FIELDS
|
fields = VkConstants.USER_FIELDS,
|
||||||
|
extended = true
|
||||||
)
|
)
|
||||||
service.getFriends(requestModel.map).mapApiResult(
|
service.getFriends(requestModel.map).mapApiResult(
|
||||||
successMapper = { apiResponse ->
|
successMapper = { apiResponse ->
|
||||||
val response = apiResponse.requireResponse()
|
val response = apiResponse.requireResponse()
|
||||||
|
|
||||||
val users = response.items.map(VkUserData::mapToDomain)
|
val users = response.items.map(VkUserData::mapToDomain)
|
||||||
|
val contactsList = response.contacts.orEmpty().map(VkContactData::mapToDomain)
|
||||||
|
|
||||||
VkMemoryCache.appendUsers(users)
|
VkMemoryCache.appendUsers(users)
|
||||||
|
VkMemoryCache.appendContacts(contactsList)
|
||||||
|
|
||||||
users
|
users
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package dev.meloda.fast.data.api.longpoll
|
package dev.meloda.fast.data.api.longpoll
|
||||||
|
|
||||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||||
import dev.meloda.fast.model.api.data.LongPollHistoryResponse
|
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import dev.meloda.fast.network.RestApiErrorDomain
|
import dev.meloda.fast.network.RestApiErrorDomain
|
||||||
import com.slack.eithernet.ApiResult
|
import com.slack.eithernet.ApiResult
|
||||||
@@ -22,14 +21,4 @@ interface LongPollRepository {
|
|||||||
mode: Int,
|
mode: Int,
|
||||||
version: Int
|
version: Int
|
||||||
): ApiResult<LongPollUpdates, RestApiErrorDomain>
|
): ApiResult<LongPollUpdates, RestApiErrorDomain>
|
||||||
|
|
||||||
suspend fun getLongPollHistory(
|
|
||||||
ts: Int,
|
|
||||||
pts: Int,
|
|
||||||
lpVersion: Int,
|
|
||||||
lastN: Int? = null,
|
|
||||||
maxMsgId: Long? = null,
|
|
||||||
eventsLimit: Int? = null,
|
|
||||||
msgsLimit: Int? = null
|
|
||||||
): ApiResult<LongPollHistoryResponse, RestApiErrorDomain>
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package dev.meloda.fast.data.api.longpoll
|
package dev.meloda.fast.data.api.longpoll
|
||||||
|
|
||||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||||
import dev.meloda.fast.model.api.data.LongPollHistoryResponse
|
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import dev.meloda.fast.model.api.requests.LongPollGetHistoryRequest
|
|
||||||
import dev.meloda.fast.model.api.requests.LongPollGetUpdatesRequest
|
import dev.meloda.fast.model.api.requests.LongPollGetUpdatesRequest
|
||||||
import dev.meloda.fast.model.api.requests.MessagesGetLongPollServerRequest
|
import dev.meloda.fast.model.api.requests.MessagesGetLongPollServerRequest
|
||||||
import dev.meloda.fast.network.RestApiErrorDomain
|
import dev.meloda.fast.network.RestApiErrorDomain
|
||||||
@@ -54,29 +52,4 @@ class LongPollRepositoryImpl(
|
|||||||
|
|
||||||
longPollService.getResponse(serverUrl, requestModel.map).mapDefault()
|
longPollService.getResponse(serverUrl, requestModel.map).mapDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getLongPollHistory(
|
|
||||||
ts: Int,
|
|
||||||
pts: Int,
|
|
||||||
lpVersion: Int,
|
|
||||||
lastN: Int?,
|
|
||||||
maxMsgId: Long?,
|
|
||||||
eventsLimit: Int?,
|
|
||||||
msgsLimit: Int?
|
|
||||||
): ApiResult<LongPollHistoryResponse, RestApiErrorDomain> = withContext(Dispatchers.IO) {
|
|
||||||
val requestModel = LongPollGetHistoryRequest(
|
|
||||||
ts = ts,
|
|
||||||
pts = pts,
|
|
||||||
lpVersion = lpVersion,
|
|
||||||
lastN = lastN,
|
|
||||||
maxMsgId = maxMsgId,
|
|
||||||
eventsLimit = eventsLimit,
|
|
||||||
msgsLimit = msgsLimit
|
|
||||||
)
|
|
||||||
|
|
||||||
messagesService.getLongPollHistory(requestModel.map).mapApiResult(
|
|
||||||
successMapper = { response -> response.requireResponse() },
|
|
||||||
errorMapper = { error -> error?.toDomain() }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,12 +14,6 @@ interface MessagesRepository {
|
|||||||
|
|
||||||
suspend fun storeMessages(messages: List<VkMessage>)
|
suspend fun storeMessages(messages: List<VkMessage>)
|
||||||
|
|
||||||
suspend fun getLocalMessages(convoId: Long): List<VkMessage>
|
|
||||||
suspend fun getLocalMessageById(messageId: Long): VkMessage?
|
|
||||||
suspend fun getLocalMessageByConvoMessageId(convoId: Long, cmId: Long): VkMessage?
|
|
||||||
suspend fun getLocalMaxMessageId(): Long?
|
|
||||||
suspend fun deleteLocalMessages(messageIds: List<Long>)
|
|
||||||
|
|
||||||
suspend fun getHistory(
|
suspend fun getHistory(
|
||||||
convoId: Long,
|
convoId: Long,
|
||||||
offset: Int?,
|
offset: Int?,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import dev.meloda.fast.model.api.domain.VkGroupDomain
|
|||||||
import dev.meloda.fast.model.api.domain.VkMessage
|
import dev.meloda.fast.model.api.domain.VkMessage
|
||||||
import dev.meloda.fast.model.api.domain.VkUser
|
import dev.meloda.fast.model.api.domain.VkUser
|
||||||
import dev.meloda.fast.model.api.domain.asEntity
|
import dev.meloda.fast.model.api.domain.asEntity
|
||||||
import dev.meloda.fast.model.database.VkMessageEntity
|
|
||||||
import dev.meloda.fast.model.api.requests.MessagesCreateChatRequest
|
import dev.meloda.fast.model.api.requests.MessagesCreateChatRequest
|
||||||
import dev.meloda.fast.model.api.requests.MessagesDeleteRequest
|
import dev.meloda.fast.model.api.requests.MessagesDeleteRequest
|
||||||
import dev.meloda.fast.model.api.requests.MessagesEditRequest
|
import dev.meloda.fast.model.api.requests.MessagesEditRequest
|
||||||
@@ -40,7 +39,6 @@ import dev.meloda.fast.model.api.requests.MessagesUnpinMessageRequest
|
|||||||
import dev.meloda.fast.model.api.responses.MessagesGetConvoMembersResponse
|
import dev.meloda.fast.model.api.responses.MessagesGetConvoMembersResponse
|
||||||
import dev.meloda.fast.model.api.responses.MessagesGetReadPeersResponse
|
import dev.meloda.fast.model.api.responses.MessagesGetReadPeersResponse
|
||||||
import dev.meloda.fast.model.api.responses.MessagesSendResponse
|
import dev.meloda.fast.model.api.responses.MessagesSendResponse
|
||||||
import dev.meloda.fast.model.database.asExternalModel
|
|
||||||
import dev.meloda.fast.network.RestApiErrorDomain
|
import dev.meloda.fast.network.RestApiErrorDomain
|
||||||
import dev.meloda.fast.network.mapApiDefault
|
import dev.meloda.fast.network.mapApiDefault
|
||||||
import dev.meloda.fast.network.mapApiResult
|
import dev.meloda.fast.network.mapApiResult
|
||||||
@@ -356,28 +354,6 @@ class MessagesRepositoryImpl(
|
|||||||
messageDao.insertAll(messages.map(VkMessage::asEntity))
|
messageDao.insertAll(messages.map(VkMessage::asEntity))
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getLocalMessages(convoId: Long): List<VkMessage> = withContext(Dispatchers.IO) {
|
|
||||||
messageDao.getAll(convoId).map(VkMessageEntity::asExternalModel)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMessageById(messageId: Long): VkMessage? = withContext(Dispatchers.IO) {
|
|
||||||
messageDao.getById(messageId)?.asExternalModel()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMessageByConvoMessageId(convoId: Long, cmId: Long): VkMessage? = withContext(Dispatchers.IO) {
|
|
||||||
messageDao.getByConvoMessageId(convoId, cmId)?.asExternalModel()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMaxMessageId(): Long? = withContext(Dispatchers.IO) {
|
|
||||||
messageDao.getMaxId()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteLocalMessages(messageIds: List<Long>) {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
messageDao.deleteByIds(messageIds)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun edit(
|
override suspend fun edit(
|
||||||
peerId: Long,
|
peerId: Long,
|
||||||
messageId: Long?,
|
messageId: Long?,
|
||||||
|
|||||||
@@ -0,0 +1,425 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 12,
|
||||||
|
"identityHash": "5eca3b3da167aaf7e772977a1f4e56e2",
|
||||||
|
"entities": [
|
||||||
|
{
|
||||||
|
"tableName": "users",
|
||||||
|
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `firstName` TEXT NOT NULL, `lastName` TEXT NOT NULL, `isOnline` INTEGER NOT NULL, `isOnlineMobile` INTEGER NOT NULL, `onlineAppId` INTEGER, `lastSeen` INTEGER, `lastSeenStatus` TEXT, `birthday` TEXT, `photo50` TEXT, `photo100` TEXT, `photo200` TEXT, `photo400Orig` TEXT, PRIMARY KEY(`id`))",
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"fieldPath": "id",
|
||||||
|
"columnName": "id",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "firstName",
|
||||||
|
"columnName": "firstName",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "lastName",
|
||||||
|
"columnName": "lastName",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isOnline",
|
||||||
|
"columnName": "isOnline",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isOnlineMobile",
|
||||||
|
"columnName": "isOnlineMobile",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "onlineAppId",
|
||||||
|
"columnName": "onlineAppId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "lastSeen",
|
||||||
|
"columnName": "lastSeen",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "lastSeenStatus",
|
||||||
|
"columnName": "lastSeenStatus",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "birthday",
|
||||||
|
"columnName": "birthday",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo50",
|
||||||
|
"columnName": "photo50",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo100",
|
||||||
|
"columnName": "photo100",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo200",
|
||||||
|
"columnName": "photo200",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo400Orig",
|
||||||
|
"columnName": "photo400Orig",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"primaryKey": {
|
||||||
|
"autoGenerate": false,
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tableName": "groups",
|
||||||
|
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `screenName` TEXT NOT NULL, `photo50` TEXT, `photo100` TEXT, `photo200` TEXT, `membersCount` INTEGER, PRIMARY KEY(`id`))",
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"fieldPath": "id",
|
||||||
|
"columnName": "id",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "name",
|
||||||
|
"columnName": "name",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "screenName",
|
||||||
|
"columnName": "screenName",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo50",
|
||||||
|
"columnName": "photo50",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo100",
|
||||||
|
"columnName": "photo100",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo200",
|
||||||
|
"columnName": "photo200",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "membersCount",
|
||||||
|
"columnName": "membersCount",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"primaryKey": {
|
||||||
|
"autoGenerate": false,
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tableName": "messages",
|
||||||
|
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `cmId` INTEGER NOT NULL, `text` TEXT, `isOut` INTEGER NOT NULL, `peerId` INTEGER NOT NULL, `fromId` INTEGER NOT NULL, `date` INTEGER NOT NULL, `randomId` INTEGER NOT NULL, `action` TEXT, `actionMemberId` INTEGER, `actionText` TEXT, `actionCmId` INTEGER, `actionMessage` TEXT, `updateTime` INTEGER, `isImportant` INTEGER NOT NULL, `forwardIds` TEXT, `attachments` TEXT, `replyMessageId` INTEGER, `geoType` TEXT, `pinnedAt` INTEGER, `isPinned` INTEGER NOT NULL, `isDeleted` INTEGER NOT NULL, `isSpam` INTEGER NOT NULL, PRIMARY KEY(`id`))",
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"fieldPath": "id",
|
||||||
|
"columnName": "id",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "cmId",
|
||||||
|
"columnName": "cmId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "text",
|
||||||
|
"columnName": "text",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isOut",
|
||||||
|
"columnName": "isOut",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "peerId",
|
||||||
|
"columnName": "peerId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "fromId",
|
||||||
|
"columnName": "fromId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "date",
|
||||||
|
"columnName": "date",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "randomId",
|
||||||
|
"columnName": "randomId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "action",
|
||||||
|
"columnName": "action",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "actionMemberId",
|
||||||
|
"columnName": "actionMemberId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "actionText",
|
||||||
|
"columnName": "actionText",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "actionCmId",
|
||||||
|
"columnName": "actionCmId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "actionMessage",
|
||||||
|
"columnName": "actionMessage",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "updateTime",
|
||||||
|
"columnName": "updateTime",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isImportant",
|
||||||
|
"columnName": "isImportant",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "forwardIds",
|
||||||
|
"columnName": "forwardIds",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "attachments",
|
||||||
|
"columnName": "attachments",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "replyMessageId",
|
||||||
|
"columnName": "replyMessageId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "geoType",
|
||||||
|
"columnName": "geoType",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "pinnedAt",
|
||||||
|
"columnName": "pinnedAt",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isPinned",
|
||||||
|
"columnName": "isPinned",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isDeleted",
|
||||||
|
"columnName": "isDeleted",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isSpam",
|
||||||
|
"columnName": "isSpam",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"primaryKey": {
|
||||||
|
"autoGenerate": false,
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tableName": "convos",
|
||||||
|
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `localId` INTEGER NOT NULL, `ownerId` INTEGER, `title` TEXT, `photo50` TEXT, `photo100` TEXT, `photo200` TEXT, `isPhantom` INTEGER NOT NULL, `lastCmId` INTEGER NOT NULL, `inReadCmId` INTEGER NOT NULL, `outReadCmId` INTEGER NOT NULL, `inRead` INTEGER NOT NULL, `outRead` INTEGER NOT NULL, `lastMessageId` INTEGER, `unreadCount` INTEGER NOT NULL, `membersCount` INTEGER, `canChangePin` INTEGER NOT NULL, `canChangeInfo` INTEGER NOT NULL, `majorId` INTEGER NOT NULL, `minorId` INTEGER NOT NULL, `pinnedMessageId` INTEGER, `peerType` TEXT NOT NULL, `isArchived` INTEGER NOT NULL, PRIMARY KEY(`id`))",
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"fieldPath": "id",
|
||||||
|
"columnName": "id",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "localId",
|
||||||
|
"columnName": "localId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "ownerId",
|
||||||
|
"columnName": "ownerId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "title",
|
||||||
|
"columnName": "title",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo50",
|
||||||
|
"columnName": "photo50",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo100",
|
||||||
|
"columnName": "photo100",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "photo200",
|
||||||
|
"columnName": "photo200",
|
||||||
|
"affinity": "TEXT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isPhantom",
|
||||||
|
"columnName": "isPhantom",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "lastCmId",
|
||||||
|
"columnName": "lastCmId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "inReadCmId",
|
||||||
|
"columnName": "inReadCmId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "outReadCmId",
|
||||||
|
"columnName": "outReadCmId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "inRead",
|
||||||
|
"columnName": "inRead",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "outRead",
|
||||||
|
"columnName": "outRead",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "lastMessageId",
|
||||||
|
"columnName": "lastMessageId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "unreadCount",
|
||||||
|
"columnName": "unreadCount",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "membersCount",
|
||||||
|
"columnName": "membersCount",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "canChangePin",
|
||||||
|
"columnName": "canChangePin",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "canChangeInfo",
|
||||||
|
"columnName": "canChangeInfo",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "majorId",
|
||||||
|
"columnName": "majorId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "minorId",
|
||||||
|
"columnName": "minorId",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "pinnedMessageId",
|
||||||
|
"columnName": "pinnedMessageId",
|
||||||
|
"affinity": "INTEGER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "peerType",
|
||||||
|
"columnName": "peerType",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isArchived",
|
||||||
|
"columnName": "isArchived",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"primaryKey": {
|
||||||
|
"autoGenerate": false,
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"setupQueries": [
|
||||||
|
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||||
|
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5eca3b3da167aaf7e772977a1f4e56e2')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ import dev.meloda.fast.model.database.VkUserEntity
|
|||||||
VkConvoEntity::class
|
VkConvoEntity::class
|
||||||
],
|
],
|
||||||
|
|
||||||
version = 11
|
version = 12
|
||||||
)
|
)
|
||||||
@TypeConverters(Converters::class)
|
@TypeConverters(Converters::class)
|
||||||
abstract class CacheDatabase : RoomDatabase() {
|
abstract class CacheDatabase : RoomDatabase() {
|
||||||
|
|||||||
@@ -9,15 +9,11 @@ import dev.meloda.fast.model.database.VkConvoEntity
|
|||||||
@Dao
|
@Dao
|
||||||
abstract class ConvoDao : EntityDao<VkConvoEntity> {
|
abstract class ConvoDao : EntityDao<VkConvoEntity> {
|
||||||
|
|
||||||
@Query("SELECT * FROM convos ORDER BY majorId DESC, minorId DESC")
|
@Query("SELECT * FROM convos")
|
||||||
abstract suspend fun getAll(): List<VkConvoEntity>
|
abstract suspend fun getAll(): List<VkConvoEntity>
|
||||||
|
|
||||||
@Transaction
|
|
||||||
@Query("SELECT * FROM convos ORDER BY majorId DESC, minorId DESC")
|
|
||||||
abstract suspend fun getAllWithMessage(): List<ConvoWithMessage>
|
|
||||||
|
|
||||||
@Query("SELECT * FROM convos WHERE id IN (:ids)")
|
@Query("SELECT * FROM convos WHERE id IN (:ids)")
|
||||||
abstract suspend fun getAllByIds(ids: List<Int>): List<VkConvoEntity>
|
abstract suspend fun getAllByIds(ids: List<Long>): List<VkConvoEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM convos WHERE id IS (:id)")
|
@Query("SELECT * FROM convos WHERE id IS (:id)")
|
||||||
abstract suspend fun getById(id: Long): VkConvoEntity?
|
abstract suspend fun getById(id: Long): VkConvoEntity?
|
||||||
@@ -26,12 +22,24 @@ abstract class ConvoDao : EntityDao<VkConvoEntity> {
|
|||||||
@Query("SELECT * FROM convos WHERE id IS (:id)")
|
@Query("SELECT * FROM convos WHERE id IS (:id)")
|
||||||
abstract suspend fun getByIdWithMessage(id: Long): ConvoWithMessage?
|
abstract suspend fun getByIdWithMessage(id: Long): ConvoWithMessage?
|
||||||
|
|
||||||
@Query("DELETE FROM convos WHERE id IS (:id)")
|
|
||||||
abstract suspend fun deleteById(id: Long): Int
|
|
||||||
|
|
||||||
@Query("DELETE FROM convos WHERE rowid IN (:ids)")
|
@Query("DELETE FROM convos WHERE rowid IN (:ids)")
|
||||||
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
abstract suspend fun deleteByIds(ids: List<Long>): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET inReadCmId = :cmId, unreadCount = :unreadCount WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateReadIncoming(convoId: Long, cmId: Long, unreadCount: Int): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET outReadCmId = :cmId, unreadCount = :unreadCount WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateReadOutgoing(convoId: Long, cmId: Long, unreadCount: Int): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET isArchived = :isArchived WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateIsArchived(convoId: Long, isArchived: Boolean): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET majorId = :majorId WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateMajorId(convoId: Long, majorId: Int): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET minorId = :minorId WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateMinorId(convoId: Long, minorId: Int): Int
|
||||||
|
|
||||||
|
@Query("UPDATE convos SET lastCmId = :cmId WHERE id = :convoId")
|
||||||
|
abstract suspend fun updateLastCmId(convoId: Long, cmId: Long): Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,24 +7,27 @@ import dev.meloda.fast.model.database.VkMessageEntity
|
|||||||
@Dao
|
@Dao
|
||||||
abstract class MessageDao : EntityDao<VkMessageEntity> {
|
abstract class MessageDao : EntityDao<VkMessageEntity> {
|
||||||
|
|
||||||
@Query("SELECT * FROM messages")
|
@Query("SELECT * FROM messages WHERE isDeleted = 0 AND isSpam = 0")
|
||||||
abstract suspend fun getAll(): List<VkMessageEntity>
|
abstract suspend fun getAll(): List<VkMessageEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM messages WHERE peerId IS (:convoId) ORDER BY date DESC, id DESC")
|
@Query("SELECT * FROM messages WHERE peerId IS (:convoId)")
|
||||||
abstract suspend fun getAll(convoId: Long): List<VkMessageEntity>
|
abstract suspend fun getAll(convoId: Long): List<VkMessageEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM messages WHERE id IN (:ids)")
|
@Query("SELECT * FROM messages WHERE id IN (:ids)")
|
||||||
abstract suspend fun getAllByIds(ids: List<Long>): List<VkMessageEntity>
|
abstract suspend fun getAllByIds(ids: List<Int>): List<VkMessageEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM messages WHERE id IS (:messageId)")
|
@Query("SELECT * FROM messages WHERE id IS (:messageId)")
|
||||||
abstract suspend fun getById(messageId: Long): VkMessageEntity?
|
abstract suspend fun getById(messageId: Long): VkMessageEntity?
|
||||||
|
|
||||||
@Query("SELECT * FROM messages WHERE peerId IS (:convoId) AND cmId IS (:cmId)")
|
|
||||||
abstract suspend fun getByConvoMessageId(convoId: Long, cmId: Long): VkMessageEntity?
|
|
||||||
|
|
||||||
@Query("SELECT MAX(id) FROM messages")
|
|
||||||
abstract suspend fun getMaxId(): Long?
|
|
||||||
|
|
||||||
@Query("DELETE FROM messages WHERE id IN (:ids)")
|
@Query("DELETE FROM messages WHERE id IN (:ids)")
|
||||||
abstract suspend fun deleteByIds(ids: List<Long>): Int
|
abstract suspend fun deleteByIds(ids: List<Int>): Int
|
||||||
|
|
||||||
|
@Query("UPDATE messages SET isDeleted = :isDeleted WHERE peerId = :convoId AND cmId = :cmId")
|
||||||
|
abstract suspend fun markAsDeleted(convoId: Long, cmId: Long, isDeleted: Boolean): Int
|
||||||
|
|
||||||
|
@Query("UPDATE messages SET isImportant = :isImportant WHERE peerId = :convoId AND cmId = :cmId")
|
||||||
|
abstract suspend fun markAsImportant(convoId: Long, cmId: Long, isImportant: Boolean): Int
|
||||||
|
|
||||||
|
@Query("UPDATE messages SET isSpam = :isSpam WHERE peerId = :convoId AND cmId = :cmId")
|
||||||
|
abstract suspend fun markAsSpam(convoId: Long, cmId: Long, isSpam: Boolean): Int
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package dev.meloda.fast.datastore
|
|||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import dev.meloda.fast.common.model.DarkMode
|
import dev.meloda.fast.common.model.DarkMode
|
||||||
import dev.meloda.fast.common.model.LogLevel
|
import dev.meloda.fast.common.model.NetworkLogLevel
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
@@ -230,21 +230,6 @@ object AppSettings {
|
|||||||
set(value) = put(SettingsKeys.KEY_MORE_ANIMATIONS, value)
|
set(value) = put(SettingsKeys.KEY_MORE_ANIMATIONS, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
object LongPoll {
|
|
||||||
var ts: Int?
|
|
||||||
get() = get(SettingsKeys.KEY_LONG_POLL_TS, 0).takeIf { it > 0 }
|
|
||||||
set(value) = put(SettingsKeys.KEY_LONG_POLL_TS, value ?: 0)
|
|
||||||
|
|
||||||
var pts: Int?
|
|
||||||
get() = get(SettingsKeys.KEY_LONG_POLL_PTS, 0).takeIf { it > 0 }
|
|
||||||
set(value) = put(SettingsKeys.KEY_LONG_POLL_PTS, value ?: 0)
|
|
||||||
|
|
||||||
fun clear() {
|
|
||||||
ts = null
|
|
||||||
pts = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object Debug {
|
object Debug {
|
||||||
var showAlertAfterCrash: Boolean
|
var showAlertAfterCrash: Boolean
|
||||||
get() = get(
|
get() = get(
|
||||||
@@ -253,11 +238,11 @@ object AppSettings {
|
|||||||
)
|
)
|
||||||
set(value) = put(SettingsKeys.KEY_DEBUG_SHOW_CRASH_ALERT, value)
|
set(value) = put(SettingsKeys.KEY_DEBUG_SHOW_CRASH_ALERT, value)
|
||||||
|
|
||||||
var networkLogLevel: LogLevel
|
var networkLogLevel: NetworkLogLevel
|
||||||
get() = get(
|
get() = get(
|
||||||
SettingsKeys.KEY_DEBUG_NETWORK_LOG_LEVEL,
|
SettingsKeys.KEY_DEBUG_NETWORK_LOG_LEVEL,
|
||||||
SettingsKeys.DEFAULT_NETWORK_LOG_LEVEL
|
SettingsKeys.DEFAULT_NETWORK_LOG_LEVEL
|
||||||
).let(LogLevel::parse)
|
).let(NetworkLogLevel::parse)
|
||||||
set(level) = put(SettingsKeys.KEY_DEBUG_NETWORK_LOG_LEVEL, level.value)
|
set(level) = put(SettingsKeys.KEY_DEBUG_NETWORK_LOG_LEVEL, level.value)
|
||||||
|
|
||||||
var showDebugCategory: Boolean
|
var showDebugCategory: Boolean
|
||||||
|
|||||||
@@ -39,8 +39,6 @@ object SettingsKeys {
|
|||||||
const val DEFAULT_VALUE_FEATURES_FAST_TEXT = "¯\\_(ツ)_/¯"
|
const val DEFAULT_VALUE_FEATURES_FAST_TEXT = "¯\\_(ツ)_/¯"
|
||||||
const val KEY_LONG_POLL_IN_BACKGROUND = "lp_background"
|
const val KEY_LONG_POLL_IN_BACKGROUND = "lp_background"
|
||||||
const val DEFAULT_LONG_POLL_IN_BACKGROUND = false
|
const val DEFAULT_LONG_POLL_IN_BACKGROUND = false
|
||||||
const val KEY_LONG_POLL_TS = "lp_ts"
|
|
||||||
const val KEY_LONG_POLL_PTS = "lp_pts"
|
|
||||||
|
|
||||||
const val KEY_ACTIVITY_SEND_ONLINE_STATUS = "activity_send_online_status"
|
const val KEY_ACTIVITY_SEND_ONLINE_STATUS = "activity_send_online_status"
|
||||||
const val DEFAULT_VALUE_KEY_ACTIVITY_SEND_ONLINE_STATUS = false
|
const val DEFAULT_VALUE_KEY_ACTIVITY_SEND_ONLINE_STATUS = false
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
interface ConvoUseCase : BaseUseCase {
|
interface ConvoUseCase : BaseUseCase {
|
||||||
|
|
||||||
suspend fun storeConvos(convos: List<VkConvo>)
|
suspend fun storeConvos(convos: List<VkConvo>)
|
||||||
suspend fun getLocalConvos(): List<VkConvo>
|
|
||||||
suspend fun getLocalConvoById(peerId: Long): VkConvo?
|
|
||||||
suspend fun deleteLocalConvo(peerId: Long)
|
|
||||||
|
|
||||||
fun getConvos(
|
fun getConvos(
|
||||||
count: Int? = null,
|
count: Int? = null,
|
||||||
|
|||||||
@@ -19,18 +19,6 @@ class ConvoUseCaseImpl(
|
|||||||
repository.storeConvos(convos)
|
repository.storeConvos(convos)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getLocalConvos(): List<VkConvo> = withContext(Dispatchers.IO) {
|
|
||||||
repository.getLocalConvos()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalConvoById(peerId: Long): VkConvo? = withContext(Dispatchers.IO) {
|
|
||||||
repository.getLocalConvoById(peerId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteLocalConvo(peerId: Long) = withContext(Dispatchers.IO) {
|
|
||||||
repository.deleteLocalConvo(peerId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getConvos(
|
override fun getConvos(
|
||||||
count: Int?,
|
count: Int?,
|
||||||
offset: Int?,
|
offset: Int?,
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package dev.meloda.fast.domain
|
|
||||||
|
|
||||||
import dev.meloda.fast.model.LongPollEvent
|
|
||||||
import dev.meloda.fast.model.LongPollParsedEvent
|
|
||||||
|
|
||||||
internal class LongPollEventDispatcher {
|
|
||||||
private val listenersMap: MutableMap<LongPollEvent, MutableList<VkEventCallback<LongPollParsedEvent>>> =
|
|
||||||
mutableMapOf()
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
fun <T : LongPollParsedEvent> dispatch(eventType: LongPollEvent, event: T) {
|
|
||||||
listenersMap[eventType]?.forEach { callback ->
|
|
||||||
(callback as? VkEventCallback<T>)?.onEvent(event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun dispatchAll(eventType: LongPollEvent, events: List<LongPollParsedEvent>) {
|
|
||||||
events.forEach { event -> dispatch(eventType, event) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
fun <T : LongPollParsedEvent> registerListener(
|
|
||||||
eventType: LongPollEvent,
|
|
||||||
listener: VkEventCallback<T>
|
|
||||||
) {
|
|
||||||
listenersMap[eventType] = (listenersMap[eventType] ?: mutableListOf())
|
|
||||||
.also { it.add(listener as VkEventCallback<LongPollParsedEvent>) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T : LongPollParsedEvent> registerListeners(
|
|
||||||
eventTypes: List<LongPollEvent>,
|
|
||||||
listener: VkEventCallback<T>
|
|
||||||
) {
|
|
||||||
eventTypes.forEach { eventType -> registerListener(eventType, listener) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,537 +0,0 @@
|
|||||||
package dev.meloda.fast.domain
|
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import dev.meloda.fast.common.VkConstants
|
|
||||||
import dev.meloda.fast.common.extensions.asInt
|
|
||||||
import dev.meloda.fast.common.extensions.asLong
|
|
||||||
import dev.meloda.fast.common.extensions.listenValue
|
|
||||||
import dev.meloda.fast.common.extensions.toList
|
|
||||||
import dev.meloda.fast.data.UserConfig
|
|
||||||
import dev.meloda.fast.data.processState
|
|
||||||
import dev.meloda.fast.model.ApiEvent
|
|
||||||
import dev.meloda.fast.model.ConvoFlags
|
|
||||||
import dev.meloda.fast.model.InteractionType
|
|
||||||
import dev.meloda.fast.model.LongPollEvent
|
|
||||||
import dev.meloda.fast.model.LongPollParsedEvent
|
|
||||||
import dev.meloda.fast.model.MessageFlags
|
|
||||||
import dev.meloda.fast.model.api.domain.VkConvo
|
|
||||||
import dev.meloda.fast.model.api.domain.VkMessage
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
||||||
import kotlin.coroutines.resume
|
|
||||||
|
|
||||||
internal class LongPollEventParser(
|
|
||||||
private val coroutineScope: CoroutineScope,
|
|
||||||
private val convoUseCase: ConvoUseCase,
|
|
||||||
private val messagesUseCase: MessagesUseCase,
|
|
||||||
private val dispatch: (LongPollEvent, LongPollParsedEvent) -> Unit,
|
|
||||||
private val dispatchAll: (LongPollEvent, List<LongPollParsedEvent>) -> Unit
|
|
||||||
) {
|
|
||||||
fun parseNextUpdate(event: List<Any>) {
|
|
||||||
val eventId = event.first().asInt()
|
|
||||||
|
|
||||||
when (val eventType = ApiEvent.parseOrNull(eventId)) {
|
|
||||||
null -> Unit
|
|
||||||
|
|
||||||
ApiEvent.MESSAGE_SET_FLAGS -> parseMessageSetFlags(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_CLEAR_FLAGS -> parseMessageClearFlags(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_NEW -> parseMessageNew(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_EDIT -> parseMessageEdit(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_READ_INCOMING -> parseMessageReadIncoming(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_READ_OUTGOING -> parseMessageReadOutgoing(eventType, event)
|
|
||||||
ApiEvent.CHAT_CLEAR_FLAGS -> parseChatClearFlags(eventType, event)
|
|
||||||
ApiEvent.CHAT_SET_FLAGS -> parseChatSetFlags(eventType, event)
|
|
||||||
ApiEvent.MESSAGES_DELETED -> parseMessagesDeleted(eventType, event)
|
|
||||||
ApiEvent.CHAT_MAJOR_CHANGED -> parseChatMajorChanged(eventType, event)
|
|
||||||
ApiEvent.CHAT_MINOR_CHANGED -> parseChatMinorChanged(eventType, event)
|
|
||||||
|
|
||||||
ApiEvent.TYPING,
|
|
||||||
ApiEvent.AUDIO_MESSAGE_RECORDING,
|
|
||||||
ApiEvent.PHOTO_UPLOADING,
|
|
||||||
ApiEvent.VIDEO_UPLOADING,
|
|
||||||
ApiEvent.FILE_UPLOADING -> parseInteraction(eventType, event)
|
|
||||||
|
|
||||||
ApiEvent.UNREAD_COUNT_UPDATE -> parseUnreadCounterUpdate(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_UPDATED -> parseMessageUpdated(eventType, event)
|
|
||||||
ApiEvent.MESSAGE_CACHE_CLEAR -> parseMessageCacheClear(eventType, event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageSetFlags(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val cmId = event[1].asLong()
|
|
||||||
val flags = event[2].asInt()
|
|
||||||
val peerId = event[3].asLong()
|
|
||||||
|
|
||||||
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
|
||||||
|
|
||||||
val parsedFlags = MessageFlags.parse(flags)
|
|
||||||
parsedFlags.forEach { flag ->
|
|
||||||
when (flag) {
|
|
||||||
MessageFlags.IMPORTANT -> {
|
|
||||||
val eventToSend = LongPollParsedEvent.MessageMarkedAsImportant(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId,
|
|
||||||
marked = true
|
|
||||||
)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MARKED_AS_IMPORTANT, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageFlags.SPAM -> {
|
|
||||||
val eventToSend = LongPollParsedEvent.MessageMarkedAsSpam(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MARKED_AS_SPAM, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageFlags.DELETED -> {
|
|
||||||
val eventToSend =
|
|
||||||
if (parsedFlags.contains(MessageFlags.DELETED_FOR_ALL)) {
|
|
||||||
LongPollParsedEvent.MessageDeleted(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId,
|
|
||||||
forAll = true
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
LongPollParsedEvent.MessageDeleted(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId,
|
|
||||||
forAll = false
|
|
||||||
)
|
|
||||||
}
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MESSAGE_DELETED, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageFlags.AUDIO_LISTENED -> {
|
|
||||||
val eventToSend = LongPollParsedEvent.AudioMessageListened(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.AUDIO_MESSAGE_LISTENED, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatchAll(LongPollEvent.MESSAGE_SET_FLAGS, eventsToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageClearFlags(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val cmId = event[1].asLong()
|
|
||||||
val flags = event[2].asInt()
|
|
||||||
val peerId = event[3].asLong()
|
|
||||||
|
|
||||||
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
|
||||||
|
|
||||||
val parsedFlags = MessageFlags.parse(flags)
|
|
||||||
|
|
||||||
coroutineScope.launch {
|
|
||||||
parsedFlags.forEach { flag ->
|
|
||||||
when (flag) {
|
|
||||||
MessageFlags.IMPORTANT -> {
|
|
||||||
val eventToSend = LongPollParsedEvent.MessageMarkedAsImportant(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId,
|
|
||||||
marked = false
|
|
||||||
)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MARKED_AS_IMPORTANT, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageFlags.SPAM -> {
|
|
||||||
if (parsedFlags.contains(MessageFlags.CANCEL_SPAM)) {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
val message = loadMessage(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)
|
|
||||||
message?.let {
|
|
||||||
val eventToSend =
|
|
||||||
LongPollParsedEvent.MessageMarkedAsNotSpam(message = message)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MARKED_AS_NOT_SPAM, eventToSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageFlags.DELETED -> {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
val message = loadMessage(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)
|
|
||||||
message?.let {
|
|
||||||
val eventToSend =
|
|
||||||
LongPollParsedEvent.MessageRestored(message = message)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.MESSAGE_RESTORED, eventToSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatchAll(LongPollEvent.MESSAGE_CLEAR_FLAGS, eventsToSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageNew(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val cmId = event[1].asLong()
|
|
||||||
val peerId = event[4].asLong()
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
val message =
|
|
||||||
async { loadMessage(peerId = peerId, cmId = cmId) }.await()
|
|
||||||
|
|
||||||
val convo =
|
|
||||||
async {
|
|
||||||
loadConvo(
|
|
||||||
peerId = peerId,
|
|
||||||
extended = true,
|
|
||||||
fields = VkConstants.ALL_FIELDS
|
|
||||||
)
|
|
||||||
}.await()
|
|
||||||
|
|
||||||
message?.let {
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.MESSAGE_NEW,
|
|
||||||
LongPollParsedEvent.NewMessage(
|
|
||||||
message = message,
|
|
||||||
inArchive = convo?.isArchived == true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageEdit(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val cmId = event[1].asLong()
|
|
||||||
val peerId = event[3].asLong()
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
loadMessage(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)?.let { message ->
|
|
||||||
dispatch(LongPollEvent.MESSAGE_EDITED, LongPollParsedEvent.MessageEdited(message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageReadIncoming(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
dispatchMessageRead(
|
|
||||||
longPollEvent = LongPollEvent.INCOMING_MESSAGE_READ,
|
|
||||||
parsedEvent = LongPollParsedEvent.IncomingMessageRead(
|
|
||||||
peerId = event[1].asLong(),
|
|
||||||
cmId = event[2].asLong(),
|
|
||||||
unreadCount = event[3].asInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageReadOutgoing(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
dispatchMessageRead(
|
|
||||||
longPollEvent = LongPollEvent.OUTGOING_MESSAGE_READ,
|
|
||||||
parsedEvent = LongPollParsedEvent.OutgoingMessageRead(
|
|
||||||
peerId = event[1].asLong(),
|
|
||||||
cmId = event[2].asLong(),
|
|
||||||
unreadCount = event[3].asInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseChatClearFlags(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val flags = event[2].asInt()
|
|
||||||
|
|
||||||
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
|
||||||
|
|
||||||
val parsedFlags = ConvoFlags.parse(flags)
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
parsedFlags.forEach { flag ->
|
|
||||||
when (flag) {
|
|
||||||
ConvoFlags.ARCHIVED -> {
|
|
||||||
handleArchivedChat(
|
|
||||||
peerId = peerId,
|
|
||||||
archived = false,
|
|
||||||
eventsToSend = eventsToSend
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatchAll(LongPollEvent.CHAT_CLEAR_FLAGS, eventsToSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseChatSetFlags(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val flags = event[2].asInt()
|
|
||||||
|
|
||||||
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
|
||||||
|
|
||||||
val parsedFlags = ConvoFlags.parse(flags)
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
parsedFlags.forEach { flag ->
|
|
||||||
when (flag) {
|
|
||||||
ConvoFlags.ARCHIVED -> {
|
|
||||||
handleArchivedChat(
|
|
||||||
peerId = peerId,
|
|
||||||
archived = true,
|
|
||||||
eventsToSend = eventsToSend
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatchAll(LongPollEvent.CHAT_SET_FLAGS, eventsToSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessagesDeleted(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val cmId = event[2].asLong()
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.CHAT_CLEARED,
|
|
||||||
LongPollParsedEvent.ChatCleared(
|
|
||||||
peerId = peerId,
|
|
||||||
toCmId = cmId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseChatMajorChanged(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val majorId = event[2].asInt()
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.CHAT_MAJOR_CHANGED,
|
|
||||||
LongPollParsedEvent.ChatMajorChanged(
|
|
||||||
peerId = peerId,
|
|
||||||
majorId = majorId,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseChatMinorChanged(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val minorId = event[2].asInt()
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.CHAT_MINOR_CHANGED,
|
|
||||||
LongPollParsedEvent.ChatMinorChanged(
|
|
||||||
peerId = peerId,
|
|
||||||
minorId = minorId,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseInteraction(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val interactionType = when (eventType) {
|
|
||||||
ApiEvent.TYPING -> InteractionType.Typing
|
|
||||||
ApiEvent.AUDIO_MESSAGE_RECORDING -> InteractionType.VoiceMessage
|
|
||||||
ApiEvent.PHOTO_UPLOADING -> InteractionType.Photo
|
|
||||||
ApiEvent.VIDEO_UPLOADING -> InteractionType.Video
|
|
||||||
ApiEvent.FILE_UPLOADING -> InteractionType.File
|
|
||||||
else -> return
|
|
||||||
}
|
|
||||||
|
|
||||||
val longPollEvent: LongPollEvent = when (eventType) {
|
|
||||||
ApiEvent.TYPING -> LongPollEvent.TYPING
|
|
||||||
ApiEvent.AUDIO_MESSAGE_RECORDING -> LongPollEvent.AUDIO_MESSAGE_RECORDING
|
|
||||||
ApiEvent.PHOTO_UPLOADING -> LongPollEvent.PHOTO_UPLOADING
|
|
||||||
ApiEvent.VIDEO_UPLOADING -> LongPollEvent.VIDEO_UPLOADING
|
|
||||||
ApiEvent.FILE_UPLOADING -> LongPollEvent.FILE_UPLOADING
|
|
||||||
else -> return
|
|
||||||
}
|
|
||||||
|
|
||||||
val peerId = event[1].asLong()
|
|
||||||
val userIds = event[2].toList(Any::asLong).filter { it != UserConfig.userId }
|
|
||||||
val totalCount = event[3].asInt()
|
|
||||||
val timestamp = event[4].asInt()
|
|
||||||
|
|
||||||
if (userIds.isEmpty()) return
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
longPollEvent,
|
|
||||||
LongPollParsedEvent.Interaction(
|
|
||||||
interactionType = interactionType,
|
|
||||||
peerId = peerId,
|
|
||||||
userIds = userIds,
|
|
||||||
totalCount = totalCount,
|
|
||||||
timestamp = timestamp
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseUnreadCounterUpdate(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val unreadCount = event[1].asInt()
|
|
||||||
val unreadUnmutedCount = event[2].asInt()
|
|
||||||
val showOnlyMuted = event[3].asInt() == 1
|
|
||||||
val businessNotifyUnreadCount = event[4].asInt()
|
|
||||||
val archiveUnreadCount = event[7].asInt()
|
|
||||||
val archiveUnreadUnmutedCount = event[8].asInt()
|
|
||||||
val archiveMentionsCount = event[9].asInt()
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.UNREAD_COUNTER_UPDATE,
|
|
||||||
LongPollParsedEvent.UnreadCounter(
|
|
||||||
unread = unreadCount,
|
|
||||||
unreadUnmuted = unreadUnmutedCount,
|
|
||||||
showOnlyMuted = showOnlyMuted,
|
|
||||||
business = businessNotifyUnreadCount,
|
|
||||||
archive = archiveUnreadCount,
|
|
||||||
archiveUnmuted = archiveUnreadUnmutedCount,
|
|
||||||
archiveMentions = archiveMentionsCount
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageUpdated(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val cmId = event[1].asLong()
|
|
||||||
val peerId = event[4].asLong()
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
loadMessage(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = cmId
|
|
||||||
)?.let { message ->
|
|
||||||
dispatch(LongPollEvent.MESSAGE_UPDATED, LongPollParsedEvent.MessageUpdated(message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseMessageCacheClear(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
val messageId = event[1].asLong()
|
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
loadMessage(messageId = messageId)?.let { message ->
|
|
||||||
dispatch(
|
|
||||||
LongPollEvent.MESSAGE_CACHE_CLEAR,
|
|
||||||
LongPollParsedEvent.MessageCacheClear(message)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun loadMessage(
|
|
||||||
peerId: Long? = null,
|
|
||||||
cmId: Long? = null,
|
|
||||||
messageId: Long? = null
|
|
||||||
): VkMessage? = suspendCancellableCoroutine { continuation ->
|
|
||||||
require((peerId != null && cmId != null) || messageId != null)
|
|
||||||
|
|
||||||
val job = coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
messagesUseCase.getById(
|
|
||||||
peerCmIds = null,
|
|
||||||
peerId = peerId,
|
|
||||||
messageIds = messageId?.let(::listOf),
|
|
||||||
cmIds = cmId?.let(::listOf),
|
|
||||||
extended = true,
|
|
||||||
fields = VkConstants.ALL_FIELDS
|
|
||||||
).listenValue(this) { state ->
|
|
||||||
state.processState(
|
|
||||||
error = { error ->
|
|
||||||
Log.e("LongPollEventParser", "loadMessage: error: $error")
|
|
||||||
continuation.resume(null)
|
|
||||||
},
|
|
||||||
success = { response ->
|
|
||||||
val message = response.singleOrNull() ?: run {
|
|
||||||
continuation.resume(null)
|
|
||||||
return@listenValue
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation.resume(message)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation.invokeOnCancellation {
|
|
||||||
job.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun loadConvo(
|
|
||||||
peerId: Long,
|
|
||||||
extended: Boolean = false,
|
|
||||||
fields: String? = null
|
|
||||||
): VkConvo? = suspendCancellableCoroutine { continuation ->
|
|
||||||
val job = coroutineScope.launch(Dispatchers.IO) {
|
|
||||||
convoUseCase.getById(
|
|
||||||
peerIds = listOf(peerId),
|
|
||||||
extended = extended,
|
|
||||||
fields = fields
|
|
||||||
).listenValue(coroutineScope) { state ->
|
|
||||||
state.processState(
|
|
||||||
error = { error ->
|
|
||||||
Log.e("LongPollEventParser", "loadConvo: error: $error")
|
|
||||||
continuation.resume(null)
|
|
||||||
},
|
|
||||||
success = { response ->
|
|
||||||
val convo = response.singleOrNull() ?: run {
|
|
||||||
continuation.resume(null)
|
|
||||||
return@listenValue
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation.resume(convo)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continuation.invokeOnCancellation {
|
|
||||||
job.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun handleArchivedChat(
|
|
||||||
peerId: Long,
|
|
||||||
archived: Boolean,
|
|
||||||
eventsToSend: MutableList<LongPollParsedEvent>
|
|
||||||
) {
|
|
||||||
val convo = loadConvo(
|
|
||||||
peerId = peerId,
|
|
||||||
extended = true,
|
|
||||||
fields = VkConstants.ALL_FIELDS
|
|
||||||
) ?: return
|
|
||||||
|
|
||||||
val message = loadMessage(
|
|
||||||
peerId = peerId,
|
|
||||||
cmId = convo.lastCmId
|
|
||||||
)
|
|
||||||
|
|
||||||
val eventToSend = LongPollParsedEvent.ChatArchived(
|
|
||||||
convo = convo.copy(lastMessage = message),
|
|
||||||
archived = archived
|
|
||||||
)
|
|
||||||
eventsToSend += eventToSend
|
|
||||||
dispatch(LongPollEvent.CHAT_ARCHIVED, eventToSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun dispatchMessageRead(
|
|
||||||
longPollEvent: LongPollEvent,
|
|
||||||
parsedEvent: LongPollParsedEvent
|
|
||||||
) {
|
|
||||||
dispatch(longPollEvent, parsedEvent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,332 @@
|
|||||||
|
package dev.meloda.fast.domain
|
||||||
|
|
||||||
|
import dev.meloda.fast.database.dao.ConvoDao
|
||||||
|
import dev.meloda.fast.database.dao.MessageDao
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
import dev.meloda.fast.model.InteractionType
|
||||||
|
import dev.meloda.fast.model.LongPollEvent
|
||||||
|
import dev.meloda.fast.model.LongPollParsedEvent
|
||||||
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
|
typealias EventListener = (event: LongPollParsedEvent) -> Unit
|
||||||
|
typealias EventListenerMap = MutableMap<LongPollEvent, MutableList<EventListener>>
|
||||||
|
|
||||||
|
class LongPollEventsHandler(
|
||||||
|
private val logger: FastLogger,
|
||||||
|
private val convoUseCase: ConvoUseCase,
|
||||||
|
private val messagesUseCase: MessagesUseCase,
|
||||||
|
private val convoDao: ConvoDao,
|
||||||
|
private val messageDao: MessageDao,
|
||||||
|
) {
|
||||||
|
private val job = SupervisorJob()
|
||||||
|
|
||||||
|
private val exceptionHandler =
|
||||||
|
CoroutineExceptionHandler { _, throwable ->
|
||||||
|
logger.error(this::class, "CoroutineException", throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val coroutineContext: CoroutineContext
|
||||||
|
get() = Dispatchers.Default + job + exceptionHandler
|
||||||
|
|
||||||
|
private val coroutineScope = CoroutineScope(coroutineContext)
|
||||||
|
|
||||||
|
private val listenersMap: EventListenerMap = mutableMapOf()
|
||||||
|
|
||||||
|
fun handleEvents(events: List<LongPollParsedEvent>) {
|
||||||
|
coroutineScope.launch {
|
||||||
|
// TODO: 30.05.2026, Danil Nikolaev: switch to interactors or something else
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
events.forEach { handleNextEvent(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleNextEvent(event: LongPollParsedEvent) {
|
||||||
|
when (event) {
|
||||||
|
is LongPollParsedEvent.AudioMessageListened -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.ChatArchived -> {
|
||||||
|
val affectedRows = convoDao.updateIsArchived(
|
||||||
|
convoId = event.convo.id,
|
||||||
|
isArchived = event.convo.isArchived
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"isArchived ${event.convo.isArchived}: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.ChatCleared -> {
|
||||||
|
val affectedRows = convoDao.updateLastCmId(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.toCmId
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"updateLastCmId: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.ChatMajorChanged -> {
|
||||||
|
val affectedRows = convoDao.updateMajorId(
|
||||||
|
convoId = event.peerId,
|
||||||
|
majorId = event.majorId
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"updateMajorId: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.ChatMinorChanged -> {
|
||||||
|
val affectedRows = convoDao.updateMinorId(
|
||||||
|
convoId = event.peerId,
|
||||||
|
minorId = event.minorId
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"updateMinorId: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.Interaction -> {
|
||||||
|
val eventType = when (event.interactionType) {
|
||||||
|
InteractionType.Typing -> LongPollEvent.TYPING
|
||||||
|
InteractionType.VoiceMessage -> LongPollEvent.AUDIO_MESSAGE_RECORDING
|
||||||
|
InteractionType.Photo -> LongPollEvent.PHOTO_UPLOADING
|
||||||
|
InteractionType.Video -> LongPollEvent.VIDEO_UPLOADING
|
||||||
|
InteractionType.File -> LongPollEvent.FILE_UPLOADING
|
||||||
|
}
|
||||||
|
|
||||||
|
emitEvent(eventType, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageCacheClear -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_CACHE_CLEAR, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageDeleted -> {
|
||||||
|
val affectedRows = messageDao.markAsDeleted(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.cmId,
|
||||||
|
isDeleted = true
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"markDeleted: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_DELETED, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageEdited -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_EDITED, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageMarkedAsImportant -> {
|
||||||
|
val affectedRows = messageDao.markAsImportant(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.cmId,
|
||||||
|
isImportant = event.marked
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"markImportant: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MARKED_AS_IMPORTANT, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageMarkedAsNotSpam -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MARKED_AS_NOT_SPAM, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageMarkedAsSpam -> {
|
||||||
|
val affectedRows = messageDao.markAsSpam(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.cmId,
|
||||||
|
isSpam = true
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"markSpam: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MARKED_AS_SPAM, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageRestored -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_RESTORED, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageUpdated -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_UPDATED, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.MessageNew -> {
|
||||||
|
messagesUseCase.storeMessage(event.message)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.MESSAGE_NEW, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.IncomingMessageRead -> {
|
||||||
|
val affectedRows = convoDao.updateReadIncoming(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.cmId,
|
||||||
|
unreadCount = event.unreadCount
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"inMessageRead: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.INCOMING_MESSAGE_READ, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.OutgoingMessageRead -> {
|
||||||
|
val affectedRows = convoDao.updateReadOutgoing(
|
||||||
|
convoId = event.peerId,
|
||||||
|
cmId = event.cmId,
|
||||||
|
unreadCount = event.unreadCount
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"outMessageRead: updated $affectedRows rows."
|
||||||
|
)
|
||||||
|
|
||||||
|
emitEvent(LongPollEvent.OUTGOING_MESSAGE_READ, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LongPollParsedEvent.UnreadCounter -> {
|
||||||
|
emitEvent(LongPollEvent.UNREAD_COUNTER_UPDATE, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T : LongPollParsedEvent> emitEvent(eventType: LongPollEvent, event: T) {
|
||||||
|
listenersMap[eventType]?.forEach { it(event) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T : LongPollParsedEvent> registerListener(
|
||||||
|
eventType: LongPollEvent,
|
||||||
|
listener: (T) -> Unit
|
||||||
|
) {
|
||||||
|
if (listenersMap[eventType] == null) {
|
||||||
|
listenersMap[eventType] = mutableListOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
listenersMap[eventType]?.add(listener as EventListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T : LongPollParsedEvent> registerListeners(
|
||||||
|
eventTypes: List<LongPollEvent>,
|
||||||
|
listener: (T) -> Unit
|
||||||
|
) {
|
||||||
|
eventTypes.forEach { eventType -> registerListener(eventType, listener) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageSetFlags(block: (LongPollParsedEvent) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_SET_FLAGS, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageMarkAsImportant(block: (LongPollParsedEvent.MessageMarkedAsImportant) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MARKED_AS_IMPORTANT, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageMarkAsSpam(block: (LongPollParsedEvent.MessageMarkedAsSpam) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MARKED_AS_SPAM, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageDelete(block: (LongPollParsedEvent.MessageDeleted) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_DELETED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageClearFlags(block: (LongPollParsedEvent) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_CLEAR_FLAGS, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageMarkAsNotSpam(block: (LongPollParsedEvent.MessageMarkedAsNotSpam) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MARKED_AS_NOT_SPAM, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageRestore(block: (LongPollParsedEvent.MessageRestored) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_RESTORED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageNew(block: (LongPollParsedEvent.MessageNew) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_NEW, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageEdit(block: (LongPollParsedEvent.MessageEdited) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.MESSAGE_EDITED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageIncomingRead(block: (LongPollParsedEvent.IncomingMessageRead) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.INCOMING_MESSAGE_READ, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMessageOutgoingRead(block: (LongPollParsedEvent.OutgoingMessageRead) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.OUTGOING_MESSAGE_READ, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onChatClear(block: (LongPollParsedEvent.ChatCleared) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.CHAT_CLEARED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onChatMajorChange(block: (LongPollParsedEvent.ChatMajorChanged) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.CHAT_MAJOR_CHANGED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onChatMinorChange(block: (LongPollParsedEvent.ChatMinorChanged) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.CHAT_MINOR_CHANGED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onChatArchive(block: (LongPollParsedEvent.ChatArchived) -> Unit) {
|
||||||
|
registerListener(LongPollEvent.CHAT_ARCHIVED, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onInteraction(block: (LongPollParsedEvent.Interaction) -> Unit) {
|
||||||
|
registerListeners(
|
||||||
|
eventTypes = listOf(
|
||||||
|
LongPollEvent.TYPING,
|
||||||
|
LongPollEvent.AUDIO_MESSAGE_RECORDING,
|
||||||
|
LongPollEvent.PHOTO_UPLOADING,
|
||||||
|
LongPollEvent.VIDEO_UPLOADING,
|
||||||
|
LongPollEvent.FILE_UPLOADING
|
||||||
|
),
|
||||||
|
listener = block
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDestroy() {
|
||||||
|
listenersMap.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,131 +1,630 @@
|
|||||||
package dev.meloda.fast.domain
|
package dev.meloda.fast.domain
|
||||||
|
|
||||||
import android.util.Log
|
import dev.meloda.fast.common.VkConstants
|
||||||
import dev.meloda.fast.model.LongPollEvent
|
import dev.meloda.fast.common.extensions.asInt
|
||||||
|
import dev.meloda.fast.common.extensions.asLong
|
||||||
|
import dev.meloda.fast.common.extensions.listenValue
|
||||||
|
import dev.meloda.fast.common.extensions.toList
|
||||||
|
import dev.meloda.fast.data.UserConfig
|
||||||
|
import dev.meloda.fast.data.processState
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
import dev.meloda.fast.model.ApiEvent
|
||||||
|
import dev.meloda.fast.model.ConvoFlags
|
||||||
|
import dev.meloda.fast.model.InteractionType
|
||||||
import dev.meloda.fast.model.LongPollParsedEvent
|
import dev.meloda.fast.model.LongPollParsedEvent
|
||||||
|
import dev.meloda.fast.model.MessageFlags
|
||||||
|
import dev.meloda.fast.model.api.domain.VkConvo
|
||||||
|
import dev.meloda.fast.model.api.domain.VkMessage
|
||||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
|
||||||
class LongPollUpdatesParser(
|
class LongPollUpdatesParser(
|
||||||
convoUseCase: ConvoUseCase,
|
private val logger: FastLogger,
|
||||||
messagesUseCase: MessagesUseCase
|
private val convoUseCase: ConvoUseCase,
|
||||||
|
private val messagesUseCase: MessagesUseCase
|
||||||
) {
|
) {
|
||||||
private val job = SupervisorJob()
|
private val job = SupervisorJob()
|
||||||
|
|
||||||
private val exceptionHandler =
|
private val exceptionHandler =
|
||||||
CoroutineExceptionHandler { _, throwable ->
|
CoroutineExceptionHandler { _, throwable ->
|
||||||
Log.e("LongPollUpdatesParser", "error: $throwable")
|
logger.error(this::class, "CoroutineException", throwable)
|
||||||
throwable.printStackTrace()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val coroutineContext: CoroutineContext
|
private val coroutineContext: CoroutineContext
|
||||||
get() = Dispatchers.Default + job + exceptionHandler
|
get() = Dispatchers.Default + job + exceptionHandler
|
||||||
|
|
||||||
private val coroutineScope = CoroutineScope(coroutineContext)
|
private val coroutineScope = CoroutineScope(coroutineContext)
|
||||||
private val eventDispatcher = LongPollEventDispatcher()
|
|
||||||
private val eventParser = LongPollEventParser(
|
suspend fun parseNextUpdate(event: List<Any>): List<LongPollParsedEvent> {
|
||||||
coroutineScope = coroutineScope,
|
val eventId = event.first().asInt()
|
||||||
convoUseCase = convoUseCase,
|
|
||||||
messagesUseCase = messagesUseCase,
|
return when (val eventType = ApiEvent.parseOrNull(eventId)) {
|
||||||
dispatch = eventDispatcher::dispatch,
|
null -> {
|
||||||
dispatchAll = eventDispatcher::dispatchAll
|
logger.debug(this::class, "parseNextUpdate(): unknownEvent: $event")
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
ApiEvent.MESSAGE_SET_FLAGS -> parseMessageSetFlags(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_CLEAR_FLAGS -> parseMessageClearFlags(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_NEW -> parseMessageNew(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_EDIT -> parseMessageEdit(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_READ_INCOMING -> parseMessageReadIncoming(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_READ_OUTGOING -> parseMessageReadOutgoing(eventType, event)
|
||||||
|
ApiEvent.CHAT_CLEAR_FLAGS -> parseChatClearFlags(eventType, event)
|
||||||
|
ApiEvent.CHAT_SET_FLAGS -> parseChatSetFlags(eventType, event)
|
||||||
|
ApiEvent.MESSAGES_DELETED -> parseMessagesDeleted(eventType, event)
|
||||||
|
ApiEvent.CHAT_MAJOR_CHANGED -> parseChatMajorChanged(eventType, event)
|
||||||
|
ApiEvent.CHAT_MINOR_CHANGED -> parseChatMinorChanged(eventType, event)
|
||||||
|
|
||||||
|
ApiEvent.TYPING,
|
||||||
|
ApiEvent.AUDIO_MESSAGE_RECORDING,
|
||||||
|
ApiEvent.PHOTO_UPLOADING,
|
||||||
|
ApiEvent.VIDEO_UPLOADING,
|
||||||
|
ApiEvent.FILE_UPLOADING -> parseInteraction(eventType, event)
|
||||||
|
|
||||||
|
ApiEvent.UNREAD_COUNT_UPDATE -> parseUnreadCounterUpdate(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_UPDATED -> parseMessageUpdated(eventType, event)
|
||||||
|
ApiEvent.MESSAGE_CACHE_CLEAR -> parseMessageCacheClear(eventType, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseMessageSetFlags(
|
||||||
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseMessageSetFlags(): $eventType: $event")
|
||||||
|
|
||||||
|
val cmId = event[1].asLong()
|
||||||
|
val flags = event[2].asInt()
|
||||||
|
val peerId = event[3].asLong()
|
||||||
|
|
||||||
|
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
||||||
|
|
||||||
|
val parsedFlags = MessageFlags.parse(flags)
|
||||||
|
parsedFlags.forEach { flag ->
|
||||||
|
when (flag) {
|
||||||
|
MessageFlags.IMPORTANT -> {
|
||||||
|
val eventToSend = LongPollParsedEvent.MessageMarkedAsImportant(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
marked = true
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.SPAM -> {
|
||||||
|
val eventToSend = LongPollParsedEvent.MessageMarkedAsSpam(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.DELETED -> {
|
||||||
|
val eventToSend =
|
||||||
|
if (parsedFlags.contains(MessageFlags.DELETED_FOR_ALL)) {
|
||||||
|
LongPollParsedEvent.MessageDeleted(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
forAll = true
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
LongPollParsedEvent.MessageDeleted(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
forAll = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.AUDIO_LISTENED -> {
|
||||||
|
val eventToSend = LongPollParsedEvent.AudioMessageListened(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.UNREAD -> Unit
|
||||||
|
MessageFlags.OUTGOING -> Unit
|
||||||
|
MessageFlags.FROM_GROUP_CHAT -> Unit
|
||||||
|
MessageFlags.CANCEL_SPAM -> Unit
|
||||||
|
MessageFlags.DELETED_FOR_ALL -> Unit
|
||||||
|
MessageFlags.DO_NOT_SHOW_NOTIFICATION -> Unit
|
||||||
|
MessageFlags.MESSAGE_WITH_REPLY -> Unit
|
||||||
|
MessageFlags.REACTION -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventsToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun parseMessageClearFlags(
|
||||||
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseMessageClearFlags(): $eventType: $event")
|
||||||
|
|
||||||
|
val cmId = event[1].asLong()
|
||||||
|
val flags = event[2].asInt()
|
||||||
|
val peerId = event[3].asLong()
|
||||||
|
|
||||||
|
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
||||||
|
|
||||||
|
val parsedFlags = MessageFlags.parse(flags)
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
val message = loadMessage(peerId = peerId, cmId = cmId)
|
||||||
|
|
||||||
|
parsedFlags.forEach { flag ->
|
||||||
|
when (flag) {
|
||||||
|
MessageFlags.IMPORTANT -> {
|
||||||
|
val eventToSend = LongPollParsedEvent.MessageMarkedAsImportant(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
marked = false
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.SPAM -> {
|
||||||
|
if (parsedFlags.contains(MessageFlags.CANCEL_SPAM)) {
|
||||||
|
if (message != null) {
|
||||||
|
val eventToSend =
|
||||||
|
LongPollParsedEvent.MessageMarkedAsNotSpam(message = message)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.DELETED -> {
|
||||||
|
if (message != null) {
|
||||||
|
val eventToSend =
|
||||||
|
LongPollParsedEvent.MessageRestored(message = message)
|
||||||
|
eventsToSend += eventToSend
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageFlags.UNREAD -> Unit
|
||||||
|
MessageFlags.OUTGOING -> Unit
|
||||||
|
MessageFlags.AUDIO_LISTENED -> Unit
|
||||||
|
MessageFlags.FROM_GROUP_CHAT -> Unit
|
||||||
|
MessageFlags.CANCEL_SPAM -> Unit
|
||||||
|
MessageFlags.DELETED_FOR_ALL -> Unit
|
||||||
|
MessageFlags.DO_NOT_SHOW_NOTIFICATION -> Unit
|
||||||
|
MessageFlags.MESSAGE_WITH_REPLY -> Unit
|
||||||
|
MessageFlags.REACTION -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
continuation.resume(eventsToSend)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun parseMessageNew(
|
||||||
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseMessageNew(): $eventType: $event")
|
||||||
|
|
||||||
|
val cmId = event[1].asLong()
|
||||||
|
val peerId = event[4].asLong()
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
val message = async { loadMessage(peerId = peerId, cmId = cmId) }.await()
|
||||||
|
|
||||||
|
val convo =
|
||||||
|
async {
|
||||||
|
loadConvo(
|
||||||
|
peerId = peerId,
|
||||||
|
extended = true,
|
||||||
|
fields = VkConstants.ALL_FIELDS
|
||||||
|
)
|
||||||
|
}.await()
|
||||||
|
|
||||||
|
if (message != null) {
|
||||||
|
val event = LongPollParsedEvent.MessageNew(
|
||||||
|
message = message,
|
||||||
|
inArchive = convo?.isArchived == true
|
||||||
|
// TODO: 03-Apr-25, Danil Nikolaev:
|
||||||
|
// load user settings about restoring chats with
|
||||||
|
// enabled notifications from archive
|
||||||
)
|
)
|
||||||
|
|
||||||
fun parseNextUpdate(event: List<Any>) {
|
continuation.resume(listOf(event))
|
||||||
eventParser.parseNextUpdate(event)
|
} else {
|
||||||
|
continuation.resume(emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageSetFlags(block: (LongPollParsedEvent) -> Unit) {
|
private suspend fun parseMessageEdit(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_SET_FLAGS, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseMessageEdit(): $eventType: $event")
|
||||||
|
|
||||||
|
val cmId = event[1].asLong()
|
||||||
|
val peerId = event[3].asLong()
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
val message = loadMessage(peerId = peerId, cmId = cmId)
|
||||||
|
if (message != null) {
|
||||||
|
val event = LongPollParsedEvent.MessageEdited(message)
|
||||||
|
continuation.resume(listOf(event))
|
||||||
|
} else {
|
||||||
|
continuation.resume(emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageMarkedAsImportant(block: (LongPollParsedEvent.MessageMarkedAsImportant) -> Unit) {
|
private fun parseMessageReadIncoming(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MARKED_AS_IMPORTANT, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseMessageReadIncoming(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val cmId = event[2].asLong()
|
||||||
|
val unreadCount = event[3].asInt()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.IncomingMessageRead(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
unreadCount = unreadCount
|
||||||
|
)
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageMarkedAsSpam(block: (LongPollParsedEvent.MessageMarkedAsSpam) -> Unit) {
|
private fun parseMessageReadOutgoing(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MARKED_AS_SPAM, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseMessageReadOutgoing(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val cmId = event[2].asLong()
|
||||||
|
val unreadCount = event[3].asInt()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.OutgoingMessageRead(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = cmId,
|
||||||
|
unreadCount = unreadCount
|
||||||
|
)
|
||||||
|
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageDeleted(block: (LongPollParsedEvent.MessageDeleted) -> Unit) {
|
private suspend fun parseChatClearFlags(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_DELETED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseChatClearFlags(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val flags = event[2].asInt()
|
||||||
|
|
||||||
|
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
||||||
|
|
||||||
|
val parsedFlags = ConvoFlags.parse(flags)
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
parsedFlags.forEach { flag ->
|
||||||
|
when (flag) {
|
||||||
|
ConvoFlags.ARCHIVED -> {
|
||||||
|
val convo = loadConvo(
|
||||||
|
peerId = peerId,
|
||||||
|
extended = true,
|
||||||
|
fields = VkConstants.ALL_FIELDS
|
||||||
|
) ?: return@forEach
|
||||||
|
|
||||||
|
val message = loadMessage(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = convo.lastCmId
|
||||||
|
)
|
||||||
|
|
||||||
|
val eventToSend = LongPollParsedEvent.ChatArchived(
|
||||||
|
convo = convo.copy(lastMessage = message),
|
||||||
|
archived = false
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageClearFlags(block: (LongPollParsedEvent) -> Unit) {
|
ConvoFlags.DISABLE_PUSH -> Unit
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_CLEAR_FLAGS, assembleEventCallback(block))
|
ConvoFlags.DISABLE_SOUND -> Unit
|
||||||
|
ConvoFlags.INCOMING_CHAT_REQUEST -> Unit
|
||||||
|
ConvoFlags.DECLINED_CHAT_REQUEST -> Unit
|
||||||
|
ConvoFlags.MENTION -> Unit
|
||||||
|
ConvoFlags.HIDE_CHAT_FROM_SEARCH -> Unit
|
||||||
|
ConvoFlags.BUSINESS_CHAT -> Unit
|
||||||
|
ConvoFlags.MARKED_MESSAGE -> Unit
|
||||||
|
ConvoFlags.DO_NOT_NOTIFY_MENTIONS_ALL_ONLINE -> Unit
|
||||||
|
ConvoFlags.DO_NOT_NOTIFY_ALL_MENTIONS -> Unit
|
||||||
|
ConvoFlags.MARKED_AS_UNREAD -> Unit
|
||||||
|
ConvoFlags.CALL_IN_PROGRESS -> Unit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageMarkedAsNotSpam(block: (LongPollParsedEvent.MessageMarkedAsNotSpam) -> Unit) {
|
continuation.resume(eventsToSend)
|
||||||
eventDispatcher.registerListener(LongPollEvent.MARKED_AS_NOT_SPAM, assembleEventCallback(block))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageRestored(block: (LongPollParsedEvent.MessageRestored) -> Unit) {
|
private suspend fun parseChatSetFlags(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_RESTORED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseChatSetFlags(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val flags = event[2].asInt()
|
||||||
|
|
||||||
|
val eventsToSend = mutableListOf<LongPollParsedEvent>()
|
||||||
|
|
||||||
|
val parsedFlags = ConvoFlags.parse(flags)
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
parsedFlags.forEach { flag ->
|
||||||
|
when (flag) {
|
||||||
|
ConvoFlags.ARCHIVED -> {
|
||||||
|
val convo = loadConvo(
|
||||||
|
peerId = peerId,
|
||||||
|
extended = true,
|
||||||
|
fields = VkConstants.ALL_FIELDS
|
||||||
|
) ?: return@forEach
|
||||||
|
|
||||||
|
val message = loadMessage(
|
||||||
|
peerId = peerId,
|
||||||
|
cmId = convo.lastCmId
|
||||||
|
)
|
||||||
|
|
||||||
|
val eventToSend = LongPollParsedEvent.ChatArchived(
|
||||||
|
convo = convo.copy(lastMessage = message),
|
||||||
|
archived = true
|
||||||
|
)
|
||||||
|
eventsToSend += eventToSend
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageUpdated(block: (LongPollParsedEvent.MessageUpdated) -> Unit) {
|
ConvoFlags.DISABLE_PUSH -> Unit
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_UPDATED, assembleEventCallback(block))
|
ConvoFlags.DISABLE_SOUND -> Unit
|
||||||
|
ConvoFlags.INCOMING_CHAT_REQUEST -> Unit
|
||||||
|
ConvoFlags.DECLINED_CHAT_REQUEST -> Unit
|
||||||
|
ConvoFlags.MENTION -> Unit
|
||||||
|
ConvoFlags.HIDE_CHAT_FROM_SEARCH -> Unit
|
||||||
|
ConvoFlags.BUSINESS_CHAT -> Unit
|
||||||
|
ConvoFlags.MARKED_MESSAGE -> Unit
|
||||||
|
ConvoFlags.DO_NOT_NOTIFY_MENTIONS_ALL_ONLINE -> Unit
|
||||||
|
ConvoFlags.DO_NOT_NOTIFY_ALL_MENTIONS -> Unit
|
||||||
|
ConvoFlags.MARKED_AS_UNREAD -> Unit
|
||||||
|
ConvoFlags.CALL_IN_PROGRESS -> Unit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageCacheClear(block: (LongPollParsedEvent.MessageCacheClear) -> Unit) {
|
continuation.resume(eventsToSend)
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_CACHE_CLEAR, assembleEventCallback(block))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onNewMessage(block: (LongPollParsedEvent.NewMessage) -> Unit) {
|
private fun parseMessagesDeleted(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_NEW, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseMessagesDeleted(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val cmId = event[2].asLong()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.ChatCleared(
|
||||||
|
peerId = peerId,
|
||||||
|
toCmId = cmId
|
||||||
|
)
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageEdited(block: (LongPollParsedEvent.MessageEdited) -> Unit) {
|
private fun parseChatMajorChanged(
|
||||||
eventDispatcher.registerListener(LongPollEvent.MESSAGE_EDITED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseChatMajorChanged(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val majorId = event[2].asInt()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.ChatMajorChanged(
|
||||||
|
peerId = peerId,
|
||||||
|
majorId = majorId,
|
||||||
|
)
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageIncomingRead(block: (LongPollParsedEvent.IncomingMessageRead) -> Unit) {
|
private fun parseChatMinorChanged(
|
||||||
eventDispatcher.registerListener(LongPollEvent.INCOMING_MESSAGE_READ, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseChatMinorChanged(): $eventType: $event")
|
||||||
|
|
||||||
|
val peerId = event[1].asLong()
|
||||||
|
val minorId = event[2].asInt()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.ChatMinorChanged(
|
||||||
|
peerId = peerId,
|
||||||
|
minorId = minorId,
|
||||||
|
)
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMessageOutgoingRead(block: (LongPollParsedEvent.OutgoingMessageRead) -> Unit) {
|
private fun parseInteraction(
|
||||||
eventDispatcher.registerListener(LongPollEvent.OUTGOING_MESSAGE_READ, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseInteraction(): $eventType: $event")
|
||||||
|
|
||||||
|
val interactionType = when (eventType) {
|
||||||
|
ApiEvent.TYPING -> InteractionType.Typing
|
||||||
|
ApiEvent.AUDIO_MESSAGE_RECORDING -> InteractionType.VoiceMessage
|
||||||
|
ApiEvent.PHOTO_UPLOADING -> InteractionType.Photo
|
||||||
|
ApiEvent.VIDEO_UPLOADING -> InteractionType.Video
|
||||||
|
ApiEvent.FILE_UPLOADING -> InteractionType.File
|
||||||
|
else -> return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onChatCleared(block: (LongPollParsedEvent.ChatCleared) -> Unit) {
|
val peerId = event[1].asLong()
|
||||||
eventDispatcher.registerListener(LongPollEvent.CHAT_CLEARED, assembleEventCallback(block))
|
val userIds = event[2].toList(Any::asLong).filter { it != UserConfig.userId }
|
||||||
|
val totalCount = event[3].asInt()
|
||||||
|
val timestamp = event[4].asInt()
|
||||||
|
|
||||||
|
// if userIds contains only account's id, then we don't need to show our status
|
||||||
|
if (userIds.isEmpty()) return emptyList()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.Interaction(
|
||||||
|
interactionType = interactionType,
|
||||||
|
peerId = peerId,
|
||||||
|
userIds = userIds,
|
||||||
|
totalCount = totalCount,
|
||||||
|
timestamp = timestamp
|
||||||
|
)
|
||||||
|
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onChatMajorChanged(block: (LongPollParsedEvent.ChatMajorChanged) -> Unit) {
|
private fun parseUnreadCounterUpdate(
|
||||||
eventDispatcher.registerListener(LongPollEvent.CHAT_MAJOR_CHANGED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> {
|
||||||
|
logger.debug(this::class, "parseUnreadCounterUpdate(): $eventType: $event")
|
||||||
|
|
||||||
|
val unreadCount = event[1].asInt()
|
||||||
|
val unreadUnmutedCount = event[2].asInt()
|
||||||
|
val showOnlyMuted = event[3].asInt() == 1
|
||||||
|
val businessNotifyUnreadCount = event[4].asInt()
|
||||||
|
val archiveUnreadCount = event[7].asInt()
|
||||||
|
val archiveUnreadUnmutedCount = event[8].asInt()
|
||||||
|
val archiveMentionsCount = event[9].asInt()
|
||||||
|
|
||||||
|
val event = LongPollParsedEvent.UnreadCounter(
|
||||||
|
unread = unreadCount,
|
||||||
|
unreadUnmuted = unreadUnmutedCount,
|
||||||
|
showOnlyMuted = showOnlyMuted,
|
||||||
|
business = businessNotifyUnreadCount,
|
||||||
|
archive = archiveUnreadCount,
|
||||||
|
archiveUnmuted = archiveUnreadUnmutedCount,
|
||||||
|
archiveMentions = archiveMentionsCount
|
||||||
|
)
|
||||||
|
return listOf(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onChatMinorChanged(block: (LongPollParsedEvent.ChatMinorChanged) -> Unit) {
|
private suspend fun parseMessageUpdated(
|
||||||
eventDispatcher.registerListener(LongPollEvent.CHAT_MINOR_CHANGED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseMessageUpdated(): $eventType: $event")
|
||||||
|
|
||||||
|
val cmId = event[1].asLong()
|
||||||
|
val peerId = event[4].asLong()
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
val message = loadMessage(peerId = peerId, cmId = cmId)
|
||||||
|
|
||||||
|
if (message != null) {
|
||||||
|
val event = LongPollParsedEvent.MessageUpdated(message)
|
||||||
|
continuation.resume(listOf(event))
|
||||||
|
} else {
|
||||||
|
continuation.resume(emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onChatArchived(block: (LongPollParsedEvent.ChatArchived) -> Unit) {
|
private suspend fun parseMessageCacheClear(
|
||||||
eventDispatcher.registerListener(LongPollEvent.CHAT_ARCHIVED, assembleEventCallback(block))
|
eventType: ApiEvent,
|
||||||
|
event: List<Any>
|
||||||
|
): List<LongPollParsedEvent> = suspendCancellableCoroutine { continuation ->
|
||||||
|
logger.debug(this::class, "parseMessageCacheClear(): $eventType: $event")
|
||||||
|
|
||||||
|
val messageId = event[1].asLong()
|
||||||
|
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
val message = loadMessage(messageId = messageId)
|
||||||
|
if (message != null) {
|
||||||
|
val event = LongPollParsedEvent.MessageCacheClear(message)
|
||||||
|
continuation.resume(listOf(event))
|
||||||
|
} else {
|
||||||
|
continuation.resume(emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onInteractions(block: (LongPollParsedEvent.Interaction) -> Unit) {
|
private suspend fun loadMessage(
|
||||||
eventDispatcher.registerListeners(
|
peerId: Long? = null,
|
||||||
eventTypes = listOf(
|
cmId: Long? = null,
|
||||||
LongPollEvent.TYPING,
|
messageId: Long? = null
|
||||||
LongPollEvent.AUDIO_MESSAGE_RECORDING,
|
): VkMessage? = suspendCancellableCoroutine { continuation ->
|
||||||
LongPollEvent.PHOTO_UPLOADING,
|
require((peerId != null && cmId != null) || messageId != null)
|
||||||
LongPollEvent.VIDEO_UPLOADING,
|
|
||||||
LongPollEvent.FILE_UPLOADING
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
),
|
messagesUseCase.getById(
|
||||||
listener = assembleEventCallback(block)
|
peerCmIds = null,
|
||||||
|
peerId = peerId,
|
||||||
|
messageIds = messageId?.let(::listOf),
|
||||||
|
cmIds = cmId?.let(::listOf),
|
||||||
|
extended = true,
|
||||||
|
fields = VkConstants.ALL_FIELDS
|
||||||
|
).listenValue(this) { state ->
|
||||||
|
state.processState(
|
||||||
|
error = { error ->
|
||||||
|
logger.error(
|
||||||
|
this@LongPollUpdatesParser::class,
|
||||||
|
"loadMessage(): ERROR: $error"
|
||||||
|
)
|
||||||
|
continuation.resume(null)
|
||||||
|
},
|
||||||
|
success = { response ->
|
||||||
|
val message = response.singleOrNull() ?: run {
|
||||||
|
continuation.resume(null)
|
||||||
|
return@listenValue
|
||||||
|
}
|
||||||
|
|
||||||
|
continuation.resume(message)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun <R : LongPollParsedEvent> assembleEventCallback(
|
|
||||||
crossinline block: (R) -> Unit,
|
|
||||||
): VkEventCallback<R> {
|
|
||||||
return VkEventCallback { event -> block.invoke(event) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun interface VkEventCallback<in T : LongPollParsedEvent> {
|
private suspend fun loadConvo(
|
||||||
fun onEvent(event: T)
|
peerId: Long,
|
||||||
|
extended: Boolean = false,
|
||||||
|
fields: String? = null
|
||||||
|
): VkConvo? = suspendCancellableCoroutine { continuation ->
|
||||||
|
coroutineScope.launch(Dispatchers.IO) {
|
||||||
|
convoUseCase.getById(
|
||||||
|
peerIds = listOf(peerId),
|
||||||
|
extended = extended,
|
||||||
|
fields = fields
|
||||||
|
).listenValue(coroutineScope) { state ->
|
||||||
|
state.processState(
|
||||||
|
error = { error ->
|
||||||
|
logger.error(
|
||||||
|
this@LongPollUpdatesParser::class,
|
||||||
|
"loadConvo(): ERROR: $error"
|
||||||
|
)
|
||||||
|
continuation.resume(null)
|
||||||
|
},
|
||||||
|
success = { response ->
|
||||||
|
val convo = response.singleOrNull() ?: run {
|
||||||
|
continuation.resume(null)
|
||||||
|
return@listenValue
|
||||||
|
}
|
||||||
|
|
||||||
|
continuation.resume(convo)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,213 +0,0 @@
|
|||||||
package dev.meloda.fast.domain
|
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import dev.meloda.fast.model.LongPollParsedEvent
|
|
||||||
import dev.meloda.fast.model.api.domain.VkMessage
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.SupervisorJob
|
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.filterIsInstance
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class LongPollUpdatesReducer(
|
|
||||||
updatesParser: LongPollUpdatesParser,
|
|
||||||
private val messagesUseCase: MessagesUseCase,
|
|
||||||
private val convoUseCase: ConvoUseCase
|
|
||||||
) {
|
|
||||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
||||||
|
|
||||||
private val _events = MutableSharedFlow<LongPollParsedEvent>(extraBufferCapacity = 256)
|
|
||||||
val events: SharedFlow<LongPollParsedEvent> = _events.asSharedFlow()
|
|
||||||
|
|
||||||
val newMessages = events.filterIsInstance<LongPollParsedEvent.NewMessage>()
|
|
||||||
val messageEdited = events.filterIsInstance<LongPollParsedEvent.MessageEdited>()
|
|
||||||
val messageIncomingRead = events.filterIsInstance<LongPollParsedEvent.IncomingMessageRead>()
|
|
||||||
val messageOutgoingRead = events.filterIsInstance<LongPollParsedEvent.OutgoingMessageRead>()
|
|
||||||
val messageDeleted = events.filterIsInstance<LongPollParsedEvent.MessageDeleted>()
|
|
||||||
val messageRestored = events.filterIsInstance<LongPollParsedEvent.MessageRestored>()
|
|
||||||
val messageMarkedAsImportant = events.filterIsInstance<LongPollParsedEvent.MessageMarkedAsImportant>()
|
|
||||||
val messageMarkedAsSpam = events.filterIsInstance<LongPollParsedEvent.MessageMarkedAsSpam>()
|
|
||||||
val messageMarkedAsNotSpam = events.filterIsInstance<LongPollParsedEvent.MessageMarkedAsNotSpam>()
|
|
||||||
val interactions = events.filterIsInstance<LongPollParsedEvent.Interaction>()
|
|
||||||
val chatMajorChanged = events.filterIsInstance<LongPollParsedEvent.ChatMajorChanged>()
|
|
||||||
val chatMinorChanged = events.filterIsInstance<LongPollParsedEvent.ChatMinorChanged>()
|
|
||||||
val chatCleared = events.filterIsInstance<LongPollParsedEvent.ChatCleared>()
|
|
||||||
val chatArchived = events.filterIsInstance<LongPollParsedEvent.ChatArchived>()
|
|
||||||
val messageUpdated = events.filterIsInstance<LongPollParsedEvent.MessageUpdated>()
|
|
||||||
val messageCacheClear = events.filterIsInstance<LongPollParsedEvent.MessageCacheClear>()
|
|
||||||
|
|
||||||
init {
|
|
||||||
updatesParser.onNewMessage { publish(it) }
|
|
||||||
updatesParser.onMessageEdited { publish(it) }
|
|
||||||
updatesParser.onMessageIncomingRead { publish(it) }
|
|
||||||
updatesParser.onMessageOutgoingRead { publish(it) }
|
|
||||||
updatesParser.onMessageDeleted { publish(it) }
|
|
||||||
updatesParser.onMessageRestored { publish(it) }
|
|
||||||
updatesParser.onMessageUpdated { publish(it) }
|
|
||||||
updatesParser.onMessageCacheClear { publish(it) }
|
|
||||||
updatesParser.onMessageMarkedAsImportant { publish(it) }
|
|
||||||
updatesParser.onMessageMarkedAsSpam { publish(it) }
|
|
||||||
updatesParser.onMessageMarkedAsNotSpam { publish(it) }
|
|
||||||
updatesParser.onInteractions { publish(it) }
|
|
||||||
updatesParser.onChatMajorChanged { publish(it) }
|
|
||||||
updatesParser.onChatMinorChanged { publish(it) }
|
|
||||||
updatesParser.onChatCleared { publish(it) }
|
|
||||||
updatesParser.onChatArchived { publish(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun publish(event: LongPollParsedEvent) {
|
|
||||||
scope.launch {
|
|
||||||
runCatching { applyCommon(event) }
|
|
||||||
.onFailure { throwable ->
|
|
||||||
Log.e("LongPollUpdatesReducer", "applyCommon failed: $event", throwable)
|
|
||||||
}
|
|
||||||
_events.emit(event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun applyCommon(event: LongPollParsedEvent) {
|
|
||||||
when (event) {
|
|
||||||
is LongPollParsedEvent.NewMessage -> {
|
|
||||||
messagesUseCase.storeMessages(listOf(event.message))
|
|
||||||
updateConvoForMessage(event.message, unreadIncrement = if (event.message.isOut) 0 else 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageEdited -> {
|
|
||||||
messagesUseCase.storeMessage(event.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageUpdated -> {
|
|
||||||
messagesUseCase.storeMessage(event.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageCacheClear -> {
|
|
||||||
messagesUseCase.storeMessage(event.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.IncomingMessageRead -> {
|
|
||||||
updateConvoReadState(
|
|
||||||
peerId = event.peerId,
|
|
||||||
inReadCmId = event.cmId,
|
|
||||||
unreadCount = event.unreadCount
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.OutgoingMessageRead -> {
|
|
||||||
updateConvoReadState(
|
|
||||||
peerId = event.peerId,
|
|
||||||
outReadCmId = event.cmId,
|
|
||||||
unreadCount = event.unreadCount
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageDeleted -> {
|
|
||||||
val message = messagesUseCase.getLocalMessageByConvoMessageId(
|
|
||||||
convoId = event.peerId,
|
|
||||||
cmId = event.cmId
|
|
||||||
)
|
|
||||||
if (message != null) {
|
|
||||||
messagesUseCase.deleteLocalMessages(listOf(message.id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageRestored -> {
|
|
||||||
messagesUseCase.storeMessage(event.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageMarkedAsImportant -> {
|
|
||||||
val message = messagesUseCase.getLocalMessageByConvoMessageId(
|
|
||||||
convoId = event.peerId,
|
|
||||||
cmId = event.cmId
|
|
||||||
) ?: return
|
|
||||||
|
|
||||||
messagesUseCase.storeMessage(message.copy(isImportant = event.marked))
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageMarkedAsSpam -> {
|
|
||||||
val message = messagesUseCase.getLocalMessageByConvoMessageId(
|
|
||||||
convoId = event.peerId,
|
|
||||||
cmId = event.cmId
|
|
||||||
)
|
|
||||||
if (message != null) {
|
|
||||||
messagesUseCase.deleteLocalMessages(listOf(message.id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.MessageMarkedAsNotSpam -> {
|
|
||||||
messagesUseCase.storeMessage(event.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.ChatMajorChanged -> {
|
|
||||||
updateConvoSortState(event.peerId, majorId = event.majorId)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.ChatMinorChanged -> {
|
|
||||||
updateConvoSortState(event.peerId, minorId = event.minorId)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.ChatCleared -> {
|
|
||||||
convoUseCase.deleteLocalConvo(event.peerId)
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.ChatArchived -> {
|
|
||||||
event.convo.lastMessage?.let(messagesUseCase::storeMessage)
|
|
||||||
convoUseCase.storeConvos(listOf(event.convo.copy(isArchived = event.archived)))
|
|
||||||
}
|
|
||||||
|
|
||||||
is LongPollParsedEvent.Interaction -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun updateConvoReadState(
|
|
||||||
peerId: Long,
|
|
||||||
inReadCmId: Long? = null,
|
|
||||||
outReadCmId: Long? = null,
|
|
||||||
unreadCount: Int
|
|
||||||
) {
|
|
||||||
val convo = convoUseCase.getLocalConvoById(peerId) ?: return
|
|
||||||
convoUseCase.storeConvos(
|
|
||||||
listOf(
|
|
||||||
convo.copy(
|
|
||||||
inReadCmId = inReadCmId ?: convo.inReadCmId,
|
|
||||||
outReadCmId = outReadCmId ?: convo.outReadCmId,
|
|
||||||
unreadCount = unreadCount
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun updateConvoSortState(
|
|
||||||
peerId: Long,
|
|
||||||
majorId: Int? = null,
|
|
||||||
minorId: Int? = null
|
|
||||||
) {
|
|
||||||
val convo = convoUseCase.getLocalConvoById(peerId) ?: return
|
|
||||||
convoUseCase.storeConvos(
|
|
||||||
listOf(
|
|
||||||
convo.copy(
|
|
||||||
majorId = majorId ?: convo.majorId,
|
|
||||||
minorId = minorId ?: convo.minorId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun updateConvoForMessage(
|
|
||||||
message: VkMessage,
|
|
||||||
unreadIncrement: Int
|
|
||||||
) {
|
|
||||||
val convo = convoUseCase.getLocalConvoById(message.peerId) ?: return
|
|
||||||
convoUseCase.storeConvos(
|
|
||||||
listOf(
|
|
||||||
convo.copy(
|
|
||||||
lastMessageId = message.id,
|
|
||||||
lastCmId = message.cmId,
|
|
||||||
unreadCount = convo.unreadCount + unreadIncrement
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package dev.meloda.fast.domain
|
package dev.meloda.fast.domain
|
||||||
|
|
||||||
import dev.meloda.fast.data.State
|
import dev.meloda.fast.data.State
|
||||||
import dev.meloda.fast.model.api.data.LongPollHistoryResponse
|
|
||||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -22,14 +21,4 @@ interface LongPollUseCase {
|
|||||||
mode: Int,
|
mode: Int,
|
||||||
version: Int
|
version: Int
|
||||||
): Flow<State<LongPollUpdates>>
|
): Flow<State<LongPollUpdates>>
|
||||||
|
|
||||||
fun getLongPollHistory(
|
|
||||||
ts: Int,
|
|
||||||
pts: Int,
|
|
||||||
lpVersion: Int,
|
|
||||||
lastN: Int? = null,
|
|
||||||
maxMsgId: Long? = null,
|
|
||||||
eventsLimit: Int? = null,
|
|
||||||
msgsLimit: Int? = null
|
|
||||||
): Flow<State<LongPollHistoryResponse>>
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package dev.meloda.fast.domain
|
|||||||
import dev.meloda.fast.data.State
|
import dev.meloda.fast.data.State
|
||||||
import dev.meloda.fast.data.api.longpoll.LongPollRepository
|
import dev.meloda.fast.data.api.longpoll.LongPollRepository
|
||||||
import dev.meloda.fast.data.mapToState
|
import dev.meloda.fast.data.mapToState
|
||||||
import dev.meloda.fast.model.api.data.LongPollHistoryResponse
|
|
||||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -49,27 +48,4 @@ class LongPollUseCaseImpl(
|
|||||||
).mapToState()
|
).mapToState()
|
||||||
emit(newState)
|
emit(newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getLongPollHistory(
|
|
||||||
ts: Int,
|
|
||||||
pts: Int,
|
|
||||||
lpVersion: Int,
|
|
||||||
lastN: Int?,
|
|
||||||
maxMsgId: Long?,
|
|
||||||
eventsLimit: Int?,
|
|
||||||
msgsLimit: Int?
|
|
||||||
): Flow<State<LongPollHistoryResponse>> = flow {
|
|
||||||
emit(State.Loading)
|
|
||||||
|
|
||||||
val newState = repository.getLongPollHistory(
|
|
||||||
ts = ts,
|
|
||||||
pts = pts,
|
|
||||||
lpVersion = lpVersion,
|
|
||||||
lastN = lastN,
|
|
||||||
maxMsgId = maxMsgId,
|
|
||||||
eventsLimit = eventsLimit,
|
|
||||||
msgsLimit = msgsLimit
|
|
||||||
).mapToState()
|
|
||||||
emit(newState)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,6 @@ interface MessagesUseCase : BaseUseCase {
|
|||||||
|
|
||||||
suspend fun storeMessage(message: VkMessage)
|
suspend fun storeMessage(message: VkMessage)
|
||||||
suspend fun storeMessages(messages: List<VkMessage>)
|
suspend fun storeMessages(messages: List<VkMessage>)
|
||||||
suspend fun getLocalMessages(convoId: Long): List<VkMessage>
|
|
||||||
suspend fun getLocalMessageById(messageId: Long): VkMessage?
|
|
||||||
suspend fun getLocalMessageByConvoMessageId(convoId: Long, cmId: Long): VkMessage?
|
|
||||||
suspend fun getLocalMaxMessageId(): Long?
|
|
||||||
suspend fun deleteLocalMessages(messageIds: List<Long>)
|
|
||||||
|
|
||||||
fun getMessagesHistory(
|
fun getMessagesHistory(
|
||||||
convoId: Long,
|
convoId: Long,
|
||||||
|
|||||||
@@ -22,26 +22,6 @@ class MessagesUseCaseImpl(
|
|||||||
repository.storeMessages(messages)
|
repository.storeMessages(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getLocalMessages(convoId: Long): List<VkMessage> {
|
|
||||||
return repository.getLocalMessages(convoId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMessageById(messageId: Long): VkMessage? {
|
|
||||||
return repository.getLocalMessageById(messageId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMessageByConvoMessageId(convoId: Long, cmId: Long): VkMessage? {
|
|
||||||
return repository.getLocalMessageByConvoMessageId(convoId, cmId)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getLocalMaxMessageId(): Long? {
|
|
||||||
return repository.getLocalMaxMessageId()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun deleteLocalMessages(messageIds: List<Long>) {
|
|
||||||
repository.deleteLocalMessages(messageIds)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getMessagesHistory(
|
override fun getMessagesHistory(
|
||||||
convoId: Long,
|
convoId: Long,
|
||||||
count: Int?,
|
count: Int?,
|
||||||
|
|||||||
@@ -22,35 +22,22 @@ class OAuthUseCaseImpl(
|
|||||||
): Flow<State<AuthInfo>> = flow {
|
): Flow<State<AuthInfo>> = flow {
|
||||||
emit(State.Loading)
|
emit(State.Loading)
|
||||||
|
|
||||||
val newState = when (val authResult = oAuthRepository.auth(
|
val newState = oAuthRepository.auth(
|
||||||
login = login,
|
login = login,
|
||||||
password = password,
|
password = password,
|
||||||
forceSms = forceSms,
|
forceSms = forceSms,
|
||||||
validationCode = validationCode,
|
validationCode = validationCode,
|
||||||
captchaSid = captchaSid,
|
captchaSid = captchaSid,
|
||||||
captchaKey = captchaKey
|
captchaKey = captchaKey
|
||||||
)) {
|
).asState(
|
||||||
is com.slack.eithernet.ApiResult.Success -> {
|
successMapper = {
|
||||||
val value = authResult.value
|
|
||||||
val userId = value.userId
|
|
||||||
val accessToken = value.accessToken
|
|
||||||
val validationHash = value.validationHash
|
|
||||||
|
|
||||||
if (userId == null || accessToken == null || validationHash == null) {
|
|
||||||
State.Error.InternalError
|
|
||||||
} else {
|
|
||||||
State.Success(
|
|
||||||
AuthInfo(
|
AuthInfo(
|
||||||
userId = userId,
|
userId = it.userId!!,
|
||||||
accessToken = accessToken,
|
accessToken = it.accessToken!!,
|
||||||
validationHash = validationHash
|
validationHash = it.validationHash!!
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
|
|
||||||
else -> authResult.asState()
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(newState)
|
emit(newState)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ fun VkConvo.extractTitle(
|
|||||||
} else {
|
} else {
|
||||||
val userName = user?.let { user ->
|
val userName = user?.let { user ->
|
||||||
if (useContactName) {
|
if (useContactName) {
|
||||||
VkMemoryCache.getContact(user.id)?.name
|
VkMemoryCache.getContact(user.id)?.name ?: user.fullName
|
||||||
} else {
|
} else {
|
||||||
user.fullName
|
user.fullName
|
||||||
}
|
}
|
||||||
@@ -598,6 +598,7 @@ private fun getAttachmentIconByType(attachmentType: AttachmentType): UiImage? {
|
|||||||
AttachmentType.VIDEO_MESSAGE -> null
|
AttachmentType.VIDEO_MESSAGE -> null
|
||||||
AttachmentType.GROUP_CHAT_STICKER -> R.drawable.ic_sticker_fill_round_24
|
AttachmentType.GROUP_CHAT_STICKER -> R.drawable.ic_sticker_fill_round_24
|
||||||
AttachmentType.STICKER_PACK_PREVIEW -> null
|
AttachmentType.STICKER_PACK_PREVIEW -> null
|
||||||
|
AttachmentType.CHANNEL_MESSAGE -> null
|
||||||
}?.let(UiImage::Resource)
|
}?.let(UiImage::Resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -687,6 +688,7 @@ fun getAttachmentUiText(
|
|||||||
AttachmentType.VIDEO_MESSAGE -> R.string.message_attachments_video_message
|
AttachmentType.VIDEO_MESSAGE -> R.string.message_attachments_video_message
|
||||||
AttachmentType.GROUP_CHAT_STICKER -> R.string.message_attachments_group_sticker
|
AttachmentType.GROUP_CHAT_STICKER -> R.string.message_attachments_group_sticker
|
||||||
AttachmentType.STICKER_PACK_PREVIEW -> R.string.message_attachments_sticker_pack_preview
|
AttachmentType.STICKER_PACK_PREVIEW -> R.string.message_attachments_sticker_pack_preview
|
||||||
|
AttachmentType.CHANNEL_MESSAGE -> R.string.message_attachments_channel_message
|
||||||
}.let(UiText::Resource)
|
}.let(UiText::Resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
/build
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
plugins {
|
||||||
|
alias(libs.plugins.fast.android.library)
|
||||||
|
}
|
||||||
|
|
||||||
|
android {
|
||||||
|
namespace = "dev.meloda.fast.logger"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(libs.koin.android)
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package dev.meloda.fast.logger;
|
||||||
|
|
||||||
|
enum class FastLogLevel {
|
||||||
|
VERBOSE,
|
||||||
|
DEBUG,
|
||||||
|
INFO,
|
||||||
|
WARNING,
|
||||||
|
ERROR,
|
||||||
|
ASSERT;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun parse(value: Int): FastLogLevel {
|
||||||
|
if (value !in 0..5) throw IllegalArgumentException("Unknown LogLevel value $value")
|
||||||
|
return entries.first { it.ordinal == value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package dev.meloda.fast.logger
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
class FastLogger {
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private lateinit var instance: FastLogger
|
||||||
|
|
||||||
|
fun setInstance(logger: FastLogger) {
|
||||||
|
if (::instance.isInitialized) {
|
||||||
|
throw IllegalStateException("FastLogger has already been initialized.")
|
||||||
|
}
|
||||||
|
|
||||||
|
instance = logger
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getInstance(): FastLogger {
|
||||||
|
if (!::instance.isInitialized) {
|
||||||
|
throw UninitializedPropertyAccessException("FastLogger is not initialized.")
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var logLevel: FastLogLevel = FastLogLevel.ERROR
|
||||||
|
|
||||||
|
fun setLogLevel(logLevel: FastLogLevel) {
|
||||||
|
Log.v(this::class.java.simpleName, "Set LogLevel from ${this.logLevel} to $logLevel")
|
||||||
|
this.logLevel = logLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
fun verbose(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
verbose(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun verbose(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.VERBOSE)) {
|
||||||
|
Log.v(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun debug(clazz: KClass<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
debug(clazz.java, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun debug(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
debug(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun debug(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.DEBUG)) {
|
||||||
|
Log.d(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun info(clazz: KClass<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
info(clazz.java, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun info(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
info(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun info(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.INFO)) {
|
||||||
|
Log.i(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun warning(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
warning(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun warning(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.WARNING)) {
|
||||||
|
Log.w(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun error(clazz: KClass<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
error(clazz.java, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun error(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
error(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun error(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.ERROR)) {
|
||||||
|
Log.e(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assert(clazz: Class<*>, message: String, throwable: Throwable? = null) {
|
||||||
|
assert(clazz.simpleName, message, throwable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assert(tag: String, message: String, throwable: Throwable? = null) {
|
||||||
|
if (shouldLog(FastLogLevel.ASSERT)) {
|
||||||
|
Log.wtf(tag, message, throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun shouldLog(level: FastLogLevel): Boolean = level.ordinal >= logLevel.ordinal
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package dev.meloda.fast.logger
|
||||||
|
|
||||||
|
import org.koin.core.module.dsl.singleOf
|
||||||
|
import org.koin.dsl.module
|
||||||
|
|
||||||
|
val loggerModule = module {
|
||||||
|
singleOf(::FastLogger)
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package dev.meloda.fast.model
|
||||||
|
|
||||||
|
import dev.meloda.fast.model.database.AccountEntity
|
||||||
|
|
||||||
|
data class AccountDto(
|
||||||
|
val userId: Long,
|
||||||
|
val accessToken: String,
|
||||||
|
val fastToken: String?,
|
||||||
|
val trustedHash: String?,
|
||||||
|
val exchangeToken: String?
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun mapToEntity(): AccountEntity = AccountEntity(
|
||||||
|
userId = userId,
|
||||||
|
accessToken = accessToken,
|
||||||
|
fastToken = fastToken,
|
||||||
|
trustedHash = trustedHash,
|
||||||
|
exchangeToken = exchangeToken
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return super.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import dev.meloda.fast.model.api.domain.VkMessage
|
|||||||
|
|
||||||
sealed interface LongPollParsedEvent {
|
sealed interface LongPollParsedEvent {
|
||||||
|
|
||||||
data class NewMessage(
|
data class MessageNew(
|
||||||
val message: VkMessage,
|
val message: VkMessage,
|
||||||
val inArchive: Boolean
|
val inArchive: Boolean
|
||||||
) : LongPollParsedEvent
|
) : LongPollParsedEvent
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package dev.meloda.fast.model.api.data
|
package dev.meloda.fast.model.api.data
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
|
|
||||||
enum class AttachmentType(var value: String) {
|
enum class AttachmentType(var value: String) {
|
||||||
UNKNOWN("unknown"),
|
UNKNOWN("unknown"),
|
||||||
PHOTO("photo"),
|
PHOTO("photo"),
|
||||||
@@ -30,7 +28,8 @@ enum class AttachmentType(var value: String) {
|
|||||||
ARTICLE("article"),
|
ARTICLE("article"),
|
||||||
VIDEO_MESSAGE("video_message"),
|
VIDEO_MESSAGE("video_message"),
|
||||||
GROUP_CHAT_STICKER("ugc_sticker"),
|
GROUP_CHAT_STICKER("ugc_sticker"),
|
||||||
STICKER_PACK_PREVIEW("sticker_pack_preview")
|
STICKER_PACK_PREVIEW("sticker_pack_preview"),
|
||||||
|
CHANNEL_MESSAGE("channel_message")
|
||||||
;
|
;
|
||||||
|
|
||||||
fun isMultiple(): Boolean = this in listOf(PHOTO, VIDEO, AUDIO, FILE)
|
fun isMultiple(): Boolean = this in listOf(PHOTO, VIDEO, AUDIO, FILE)
|
||||||
@@ -41,10 +40,6 @@ enum class AttachmentType(var value: String) {
|
|||||||
it.value == value
|
it.value == value
|
||||||
} ?: UNKNOWN
|
} ?: UNKNOWN
|
||||||
|
|
||||||
if (parsedValue == UNKNOWN) {
|
|
||||||
Log.e("AttachmentType", "Unknown attachment type: $value")
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsedValue
|
return parsedValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package dev.meloda.fast.model.api.data
|
|
||||||
|
|
||||||
import com.squareup.moshi.Json
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class LongPollHistoryResponse(
|
|
||||||
@Json(name = "history") val history: List<List<Any>>? = null,
|
|
||||||
@Json(name = "messages") val messages: Messages? = null,
|
|
||||||
@Json(name = "profiles") val profiles: List<VkUserData>? = null,
|
|
||||||
@Json(name = "groups") val groups: List<VkGroupData>? = null,
|
|
||||||
@Json(name = "new_pts") val newPts: Int? = null,
|
|
||||||
@Json(name = "from_pts") val fromPts: Int? = null,
|
|
||||||
@Json(name = "ts") val ts: Int? = null,
|
|
||||||
@Json(name = "pts") val pts: Int? = null,
|
|
||||||
@Json(name = "more") val more: Int? = null,
|
|
||||||
@Json(name = "conversations") val conversations: List<VkConvoData>? = null
|
|
||||||
) {
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class Messages(
|
|
||||||
@Json(name = "count") val count: Int? = null,
|
|
||||||
@Json(name = "items") val items: List<VkMessageData>? = null
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -35,7 +35,8 @@ data class VkAttachmentItemData(
|
|||||||
@Json(name = "article") val article: VkArticleData?,
|
@Json(name = "article") val article: VkArticleData?,
|
||||||
@Json(name = "video_message") val videoMessage: VkVideoMessageData?,
|
@Json(name = "video_message") val videoMessage: VkVideoMessageData?,
|
||||||
@Json(name = "ugc_sticker") val groupSticker: VkGroupStickerData?,
|
@Json(name = "ugc_sticker") val groupSticker: VkGroupStickerData?,
|
||||||
@Json(name = "sticker_pack_preview") val stickerPackPreview: VkStickerPackPreviewData?
|
@Json(name = "sticker_pack_preview") val stickerPackPreview: VkStickerPackPreviewData?,
|
||||||
|
@Json(name = "channel_message") val channelMessageData: VkChannelMessageData?
|
||||||
) {
|
) {
|
||||||
fun toDomain(): VkAttachment = when (AttachmentType.parse(type)) {
|
fun toDomain(): VkAttachment = when (AttachmentType.parse(type)) {
|
||||||
AttachmentType.UNKNOWN -> VkUnknownAttachment
|
AttachmentType.UNKNOWN -> VkUnknownAttachment
|
||||||
@@ -66,5 +67,6 @@ data class VkAttachmentItemData(
|
|||||||
AttachmentType.VIDEO_MESSAGE -> videoMessage?.toDomain()
|
AttachmentType.VIDEO_MESSAGE -> videoMessage?.toDomain()
|
||||||
AttachmentType.GROUP_CHAT_STICKER -> groupSticker?.toDomain()
|
AttachmentType.GROUP_CHAT_STICKER -> groupSticker?.toDomain()
|
||||||
AttachmentType.STICKER_PACK_PREVIEW -> stickerPackPreview?.toDomain()
|
AttachmentType.STICKER_PACK_PREVIEW -> stickerPackPreview?.toDomain()
|
||||||
|
AttachmentType.CHANNEL_MESSAGE -> channelMessageData?.toDomain()
|
||||||
} ?: VkUnknownAttachment
|
} ?: VkUnknownAttachment
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package dev.meloda.fast.model.api.data
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
import dev.meloda.fast.model.api.domain.VkChannelMessage
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class VkChannelMessageData(
|
||||||
|
@Json(name = "channel_id") val channelId: Long,
|
||||||
|
@Json(name = "cmid") val cmId: Long,
|
||||||
|
@Json(name = "author_id") val authorId: Long,
|
||||||
|
@Json(name = "channel_info") val channelInfo: ChannelInfo,
|
||||||
|
@Json(name = "channel_type") val channelType: String,
|
||||||
|
@Json(name = "guid") val guid: String,
|
||||||
|
@Json(name = "text") val text: String?,
|
||||||
|
@Json(name = "time") val time: Long,
|
||||||
|
@Json(name = "attachments") val attachments: List<VkAttachmentItemData> = emptyList(),
|
||||||
|
) : VkAttachmentData {
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class ChannelInfo(
|
||||||
|
@Json(name = "photo_base") val photoBase: String?,
|
||||||
|
@Json(name = "title") val title: String
|
||||||
|
)
|
||||||
|
|
||||||
|
fun toDomain(): VkChannelMessage = VkChannelMessage(
|
||||||
|
channelId = channelId,
|
||||||
|
cmId = cmId,
|
||||||
|
authorId = authorId,
|
||||||
|
channelInfo = VkChannelMessage.ChannelInfo(
|
||||||
|
title = channelInfo.title,
|
||||||
|
photoBase = channelInfo.photoBase
|
||||||
|
),
|
||||||
|
channelType = channelType,
|
||||||
|
guid = guid,
|
||||||
|
text = text,
|
||||||
|
time = time,
|
||||||
|
attachments = attachments.map(VkAttachmentItemData::toDomain),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -117,5 +117,6 @@ fun VkMessageData.asDomain(): VkMessage = VkMessage(
|
|||||||
pinnedAt = pinnedAt,
|
pinnedAt = pinnedAt,
|
||||||
isPinned = isPinned == true,
|
isPinned = isPinned == true,
|
||||||
formatData = formatData?.asDomain(),
|
formatData = formatData?.asDomain(),
|
||||||
isSpam = false
|
isSpam = false,
|
||||||
|
isDeleted = false
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -56,5 +56,6 @@ data class VkPinnedMessageData(
|
|||||||
isPinned = true,
|
isPinned = true,
|
||||||
isSpam = false,
|
isSpam = false,
|
||||||
formatData = null,
|
formatData = null,
|
||||||
|
isDeleted = false
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package dev.meloda.fast.model.api.domain
|
||||||
|
|
||||||
|
import dev.meloda.fast.model.api.data.AttachmentType
|
||||||
|
|
||||||
|
data class VkChannelMessage(
|
||||||
|
val channelId: Long,
|
||||||
|
val cmId: Long,
|
||||||
|
val authorId: Long,
|
||||||
|
val channelInfo: ChannelInfo,
|
||||||
|
val channelType: String,
|
||||||
|
val guid: String,
|
||||||
|
val text: String?,
|
||||||
|
val time: Long,
|
||||||
|
val attachments: List<VkAttachment>?,
|
||||||
|
) : VkAttachment {
|
||||||
|
|
||||||
|
data class ChannelInfo(
|
||||||
|
val title: String,
|
||||||
|
val photoBase: String?
|
||||||
|
)
|
||||||
|
|
||||||
|
override val type: AttachmentType = AttachmentType.CHANNEL_MESSAGE
|
||||||
|
}
|
||||||
@@ -36,6 +36,8 @@ data class VkMessage(
|
|||||||
val group: VkGroupDomain?,
|
val group: VkGroupDomain?,
|
||||||
val actionUser: VkUser?,
|
val actionUser: VkUser?,
|
||||||
val actionGroup: VkGroupDomain?,
|
val actionGroup: VkGroupDomain?,
|
||||||
|
|
||||||
|
val isDeleted: Boolean
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun isPeerChat() = peerId > 2_000_000_000
|
fun isPeerChat() = peerId > 2_000_000_000
|
||||||
@@ -111,7 +113,7 @@ fun VkMessage.asEntity(): VkMessageEntity = VkMessageEntity(
|
|||||||
actionCmId = actionCmId,
|
actionCmId = actionCmId,
|
||||||
actionMessage = actionMessage,
|
actionMessage = actionMessage,
|
||||||
updateTime = updateTime,
|
updateTime = updateTime,
|
||||||
important = isImportant,
|
isImportant = isImportant,
|
||||||
forwardIds = forwards.orEmpty().map(VkMessage::id),
|
forwardIds = forwards.orEmpty().map(VkMessage::id),
|
||||||
// TODO: 05/05/2024, Danil Nikolaev: save attachments
|
// TODO: 05/05/2024, Danil Nikolaev: save attachments
|
||||||
attachments = emptyList(),
|
attachments = emptyList(),
|
||||||
@@ -119,4 +121,6 @@ fun VkMessage.asEntity(): VkMessageEntity = VkMessageEntity(
|
|||||||
geoType = geoType,
|
geoType = geoType,
|
||||||
pinnedAt = pinnedAt,
|
pinnedAt = pinnedAt,
|
||||||
isPinned = isPinned,
|
isPinned = isPinned,
|
||||||
|
isDeleted = isDeleted,
|
||||||
|
isSpam = isSpam
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ data class GetFriendsRequest(
|
|||||||
val order: String?,
|
val order: String?,
|
||||||
val count: Int?,
|
val count: Int?,
|
||||||
val offset: Int?,
|
val offset: Int?,
|
||||||
val fields: String?
|
val fields: String?,
|
||||||
|
val extended: Boolean?
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val map
|
val map
|
||||||
@@ -14,6 +15,7 @@ data class GetFriendsRequest(
|
|||||||
count?.let { this["count"] = it.toString() }
|
count?.let { this["count"] = it.toString() }
|
||||||
offset?.let { this["offset"] = it.toString() }
|
offset?.let { this["offset"] = it.toString() }
|
||||||
fields?.let { this["fields"] = it }
|
fields?.let { this["fields"] = it }
|
||||||
|
extended?.let { this["extended"] = it.toString() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,27 +19,3 @@ data class LongPollGetUpdatesRequest(
|
|||||||
"version" to version.toString()
|
"version" to version.toString()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class LongPollGetHistoryRequest(
|
|
||||||
val ts: Int,
|
|
||||||
val pts: Int,
|
|
||||||
val lpVersion: Int,
|
|
||||||
val lastN: Int? = null,
|
|
||||||
val maxMsgId: Long? = null,
|
|
||||||
val eventsLimit: Int? = null,
|
|
||||||
val msgsLimit: Int? = null,
|
|
||||||
val extended: Boolean = true,
|
|
||||||
) {
|
|
||||||
val map: Map<String, String>
|
|
||||||
get() = mutableMapOf(
|
|
||||||
"ts" to ts.toString(),
|
|
||||||
"pts" to pts.toString(),
|
|
||||||
"lp_version" to lpVersion.toString(),
|
|
||||||
"extended" to if (extended) "1" else "0",
|
|
||||||
).apply {
|
|
||||||
lastN?.let { this["last_n"] = it.toString() }
|
|
||||||
maxMsgId?.let { this["max_msg_id"] = it.toString() }
|
|
||||||
eventsLimit?.let { this["events_limit"] = it.toString() }
|
|
||||||
msgsLimit?.let { this["msgs_limit"] = it.toString() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package dev.meloda.fast.model.api.responses
|
package dev.meloda.fast.model.api.responses
|
||||||
|
|
||||||
import dev.meloda.fast.model.api.data.VkUserData
|
|
||||||
import com.squareup.moshi.Json
|
import com.squareup.moshi.Json
|
||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
|
import dev.meloda.fast.model.api.data.VkContactData
|
||||||
|
import dev.meloda.fast.model.api.data.VkUserData
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class GetFriendsResponse(
|
data class GetFriendsResponse(
|
||||||
@Json(name = "count") val count: Int,
|
@Json(name = "count") val count: Int,
|
||||||
@Json(name = "items") val items: List<VkUserData>
|
@Json(name = "items") val items: List<VkUserData>,
|
||||||
|
@Json(name = "contacts") val contacts: List<VkContactData>?
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package dev.meloda.fast.model.database
|
|||||||
|
|
||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
|
import dev.meloda.fast.model.AccountDto
|
||||||
|
|
||||||
@Entity(tableName = "accounts")
|
@Entity(tableName = "accounts")
|
||||||
data class AccountEntity(
|
data class AccountEntity(
|
||||||
@@ -11,4 +12,12 @@ data class AccountEntity(
|
|||||||
val fastToken: String?,
|
val fastToken: String?,
|
||||||
val trustedHash: String?,
|
val trustedHash: String?,
|
||||||
val exchangeToken: String?
|
val exchangeToken: String?
|
||||||
|
) {
|
||||||
|
fun mapToDto(): AccountDto = AccountDto(
|
||||||
|
userId = userId,
|
||||||
|
accessToken = accessToken,
|
||||||
|
fastToken = fastToken,
|
||||||
|
trustedHash = trustedHash,
|
||||||
|
exchangeToken = exchangeToken
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ data class VkMessageEntity(
|
|||||||
val actionCmId: Long?,
|
val actionCmId: Long?,
|
||||||
val actionMessage: String?,
|
val actionMessage: String?,
|
||||||
val updateTime: Int?,
|
val updateTime: Int?,
|
||||||
val important: Boolean,
|
val isImportant: Boolean,
|
||||||
val forwardIds: List<Long>?,
|
val forwardIds: List<Long>?,
|
||||||
val attachments: List<String>?, // TODO: 01/05/2024, Danil Nikolaev: how to store???
|
val attachments: List<String>?, // TODO: 01/05/2024, Danil Nikolaev: how to store???
|
||||||
val replyMessageId: Long?,
|
val replyMessageId: Long?,
|
||||||
val geoType: String?,
|
val geoType: String?,
|
||||||
val pinnedAt: Int?,
|
val pinnedAt: Int?,
|
||||||
val isPinned: Boolean
|
val isPinned: Boolean,
|
||||||
|
val isDeleted: Boolean,
|
||||||
|
val isSpam: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
fun VkMessageEntity.asExternalModel(): VkMessage = VkMessage(
|
fun VkMessageEntity.asExternalModel(): VkMessage = VkMessage(
|
||||||
@@ -45,7 +47,7 @@ fun VkMessageEntity.asExternalModel(): VkMessage = VkMessage(
|
|||||||
actionCmId = actionCmId,
|
actionCmId = actionCmId,
|
||||||
actionMessage = actionMessage,
|
actionMessage = actionMessage,
|
||||||
updateTime = updateTime,
|
updateTime = updateTime,
|
||||||
isImportant = important,
|
isImportant = isImportant,
|
||||||
forwards = emptyList(),//forwards.orEmpty().map(VkMessageEntity::asExternalModel),
|
forwards = emptyList(),//forwards.orEmpty().map(VkMessageEntity::asExternalModel),
|
||||||
// TODO: 05/05/2024, Danil Nikolaev: restore attachments
|
// TODO: 05/05/2024, Danil Nikolaev: restore attachments
|
||||||
attachments = attachments.orEmpty().map { VkUnknownAttachment },
|
attachments = attachments.orEmpty().map { VkUnknownAttachment },
|
||||||
@@ -59,4 +61,5 @@ fun VkMessageEntity.asExternalModel(): VkMessage = VkMessage(
|
|||||||
isPinned = isPinned,
|
isPinned = isPinned,
|
||||||
isSpam = false,
|
isSpam = false,
|
||||||
formatData = null,
|
formatData = null,
|
||||||
|
isDeleted = isDeleted
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ dependencies {
|
|||||||
api(projects.core.common)
|
api(projects.core.common)
|
||||||
api(projects.core.model)
|
api(projects.core.model)
|
||||||
api(projects.core.datastore)
|
api(projects.core.datastore)
|
||||||
|
api(projects.core.logger)
|
||||||
|
|
||||||
implementation(libs.moshi.kotlin)
|
implementation(libs.moshi.kotlin)
|
||||||
implementation(libs.koin.android)
|
implementation(libs.koin.android)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package dev.meloda.fast.network
|
package dev.meloda.fast.network
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import com.slack.eithernet.ApiException
|
import com.slack.eithernet.ApiException
|
||||||
import com.slack.eithernet.errorType
|
import com.slack.eithernet.errorType
|
||||||
import com.slack.eithernet.toType
|
import com.slack.eithernet.toType
|
||||||
import com.squareup.moshi.JsonDataException
|
import com.squareup.moshi.JsonDataException
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
import retrofit2.Converter
|
import retrofit2.Converter
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
@@ -16,7 +16,10 @@ import java.lang.reflect.Type
|
|||||||
*
|
*
|
||||||
* допускает Unit как SuccessType в случае невозможности каста ответа в ErrorType
|
* допускает Unit как SuccessType в случае невозможности каста ответа в ErrorType
|
||||||
*/
|
*/
|
||||||
class ResponseConverterFactory(private val converter: JsonConverter) : Converter.Factory() {
|
class ResponseConverterFactory(
|
||||||
|
private val converter: JsonConverter,
|
||||||
|
private val logger: FastLogger
|
||||||
|
) : Converter.Factory() {
|
||||||
|
|
||||||
override fun responseBodyConverter(
|
override fun responseBodyConverter(
|
||||||
type: Type,
|
type: Type,
|
||||||
@@ -29,6 +32,7 @@ class ResponseConverterFactory(private val converter: JsonConverter) : Converter
|
|||||||
successType = type,
|
successType = type,
|
||||||
errorRaw = errorRaw,
|
errorRaw = errorRaw,
|
||||||
converter = converter,
|
converter = converter,
|
||||||
|
logger = logger
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +40,7 @@ class ResponseConverterFactory(private val converter: JsonConverter) : Converter
|
|||||||
private val successType: Type,
|
private val successType: Type,
|
||||||
private val errorRaw: Class<*>,
|
private val errorRaw: Class<*>,
|
||||||
private val converter: JsonConverter,
|
private val converter: JsonConverter,
|
||||||
|
private val logger: FastLogger
|
||||||
) : Converter<ResponseBody, Any?> {
|
) : Converter<ResponseBody, Any?> {
|
||||||
override fun convert(value: ResponseBody): Any? {
|
override fun convert(value: ResponseBody): Any? {
|
||||||
val string = value.string()
|
val string = value.string()
|
||||||
@@ -53,7 +58,7 @@ class ResponseConverterFactory(private val converter: JsonConverter) : Converter
|
|||||||
},
|
},
|
||||||
onFailure = { failure ->
|
onFailure = { failure ->
|
||||||
if (failure is JsonDataException) {
|
if (failure is JsonDataException) {
|
||||||
Log.d("ResponseBodyConverter", "convertJsonDataException: $failure")
|
logger.error(this::class, "convert(): ERROR", failure)
|
||||||
throw ApiException(
|
throw ApiException(
|
||||||
RestApiError(
|
RestApiError(
|
||||||
errorCode = -1,
|
errorCode = -1,
|
||||||
@@ -68,10 +73,11 @@ class ResponseConverterFactory(private val converter: JsonConverter) : Converter
|
|||||||
converter.fromJson(errorRaw, string)
|
converter.fromJson(errorRaw, string)
|
||||||
}.fold(
|
}.fold(
|
||||||
onSuccess = { errorModel ->
|
onSuccess = { errorModel ->
|
||||||
Log.d("ResponseBodyConverter", "convert: $errorModel")
|
logger.debug(this::class, "convert(): errorModel: $errorModel")
|
||||||
throw ApiException(errorModel)
|
throw ApiException(errorModel)
|
||||||
},
|
},
|
||||||
onFailure = { exception ->
|
onFailure = { exception ->
|
||||||
|
logger.error(this::class, "convert(): INNER: ERROR", exception)
|
||||||
if (!isUnit) {
|
if (!isUnit) {
|
||||||
throw exception
|
throw exception
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.slack.eithernet.integration.retrofit.ApiResultConverterFactory
|
|||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import dev.meloda.fast.common.AppConstants
|
import dev.meloda.fast.common.AppConstants
|
||||||
import dev.meloda.fast.datastore.AppSettings
|
import dev.meloda.fast.datastore.AppSettings
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import dev.meloda.fast.network.JsonConverter
|
import dev.meloda.fast.network.JsonConverter
|
||||||
import dev.meloda.fast.network.MoshiConverter
|
import dev.meloda.fast.network.MoshiConverter
|
||||||
import dev.meloda.fast.network.OAuthResultCallFactory
|
import dev.meloda.fast.network.OAuthResultCallFactory
|
||||||
@@ -123,7 +124,12 @@ private fun Scope.buildRetrofit(client: OkHttpClient): Retrofit {
|
|||||||
.baseUrl("${AppConstants.URL_API}/")
|
.baseUrl("${AppConstants.URL_API}/")
|
||||||
.addConverterFactory(ApiResultConverterFactory)
|
.addConverterFactory(ApiResultConverterFactory)
|
||||||
.addCallAdapterFactory(ApiResultCallAdapterFactory)
|
.addCallAdapterFactory(ApiResultCallAdapterFactory)
|
||||||
.addConverterFactory(ResponseConverterFactory(get<JsonConverter>()))
|
.addConverterFactory(
|
||||||
|
ResponseConverterFactory(
|
||||||
|
get<JsonConverter>(),
|
||||||
|
get<FastLogger>()
|
||||||
|
)
|
||||||
|
)
|
||||||
.addConverterFactory(MoshiConverterFactory.create(get()))
|
.addConverterFactory(MoshiConverterFactory.create(get()))
|
||||||
.client(client)
|
.client(client)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
+64
-35
@@ -1,62 +1,88 @@
|
|||||||
package dev.meloda.fast.network.interceptor
|
package dev.meloda.fast.network.interceptor
|
||||||
|
|
||||||
|
import dev.meloda.fast.common.extensions.listenValue
|
||||||
import dev.meloda.fast.datastore.AppSettings
|
import dev.meloda.fast.datastore.AppSettings
|
||||||
import dev.meloda.fast.datastore.CaptchaTokenResult
|
import dev.meloda.fast.datastore.CaptchaTokenResult
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
|
||||||
import kotlinx.coroutines.sync.withLock
|
|
||||||
import kotlinx.coroutines.withTimeout
|
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.IOException
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.atomic.AtomicReference
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
import kotlin.time.Duration.Companion.minutes
|
|
||||||
|
|
||||||
class Error14HandlingInterceptor : Interceptor {
|
class Error14HandlingInterceptor(private val logger: FastLogger) : Interceptor {
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
private const val CAPTCHA_ERROR_CODE = 14
|
||||||
|
private const val CAPTCHA_ERROR_KIND = "need_captcha"
|
||||||
|
private val executor = Executors.newSingleThreadExecutor()
|
||||||
|
}
|
||||||
|
|
||||||
private val cookie = AtomicReference<String?>(null)
|
private val cookie = AtomicReference<String?>(null)
|
||||||
private val captchaMutex = Mutex()
|
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request().withCookie()
|
val request = chain.request().withCookie()
|
||||||
val response = chain.proceed(request)
|
val response = chain.proceed(request)
|
||||||
response.parseCookie()
|
response.parseCookie()
|
||||||
|
|
||||||
if (request.shouldSkipCaptcha()) return response
|
if (request.shouldSkipCaptcha()) return response
|
||||||
|
|
||||||
val redirectUri = response.getRedirectUri() ?: return response
|
val redirectUri = response.getRedirectUri() ?: return response
|
||||||
val token = awaitCaptchaToken(redirectUri)
|
val token = passCaptchaAndGetToken(redirectUri)
|
||||||
|
|
||||||
return chain.proceed(chain.request().withCookie().withSuccessToken(token))
|
return chain.proceed(chain.request().withCookie().withSuccessToken(token))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun awaitCaptchaToken(redirectUri: String): String = runBlocking(Dispatchers.IO) {
|
private fun passCaptchaAndGetToken(redirectUri: String): String = synchronized(this) {
|
||||||
captchaMutex.withLock {
|
val tokenResult = AtomicReference<Result<String>>(Result.failure(Exception("No result")))
|
||||||
AppSettings.setCaptchaResult(CaptchaTokenResult.Initial)
|
|
||||||
AppSettings.setCaptchaRedirectUri(redirectUri)
|
|
||||||
|
|
||||||
try {
|
executor.submit {
|
||||||
withTimeout(CAPTCHA_TIMEOUT) {
|
AppSettings.setCaptchaRedirectUri(redirectUri)
|
||||||
AppSettings.getCaptchaResultFlow()
|
logger.debug(this::class, "passCaptchaAndGetToken: $redirectUri")
|
||||||
.first { it != CaptchaTokenResult.Initial }
|
|
||||||
.toToken()
|
var job: Job? = null
|
||||||
}
|
job = AppSettings.getCaptchaResultFlow()
|
||||||
} finally {
|
.listenValue(CoroutineScope(Dispatchers.IO)) {
|
||||||
|
logger.debug(this::class, "passCaptchaAndGetToken: $it")
|
||||||
|
if (it != CaptchaTokenResult.Initial) {
|
||||||
|
synchronized(tokenResult) {
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"passCaptchaAndGetToken: SYNCHRONIZED: $it"
|
||||||
|
)
|
||||||
|
tokenResult.set(wrapResult(it))
|
||||||
|
tokenResult.notifyAll()
|
||||||
|
job?.cancel()
|
||||||
|
logger.debug(
|
||||||
|
this::class,
|
||||||
|
"passCaptchaAndGetToken: NULL RESULT"
|
||||||
|
)
|
||||||
AppSettings.setCaptchaResult(CaptchaTokenResult.Initial)
|
AppSettings.setCaptchaResult(CaptchaTokenResult.Initial)
|
||||||
AppSettings.setCaptchaRedirectUri(null)
|
AppSettings.setCaptchaRedirectUri(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
synchronized(tokenResult) {
|
||||||
|
if (tokenResult.get().getOrNull() == null) {
|
||||||
|
tokenResult.wait()
|
||||||
|
}
|
||||||
|
|
||||||
private fun CaptchaTokenResult.toToken(): String = when (this) {
|
logger.debug(this::class, "passCaptchaAndGetToken: GET VALUE")
|
||||||
is CaptchaTokenResult.Success -> token
|
tokenResult.get().getOrThrow()
|
||||||
CaptchaTokenResult.Cancelled -> throw IOException("Captcha cancelled")
|
}
|
||||||
CaptchaTokenResult.Null -> throw IOException("Captcha result is empty")
|
}
|
||||||
CaptchaTokenResult.Initial -> throw IllegalStateException("Captcha result not ready")
|
|
||||||
|
private fun wrapResult(result: CaptchaTokenResult): Result<String> {
|
||||||
|
return when (result) {
|
||||||
|
// TODO: 03/05/2026, Danil Nikolaev: check again?
|
||||||
|
CaptchaTokenResult.Null -> Result.success("")
|
||||||
|
|
||||||
|
CaptchaTokenResult.Cancelled, CaptchaTokenResult.Initial -> Result.success("")
|
||||||
|
|
||||||
|
is CaptchaTokenResult.Success -> Result.success(result.token)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Request.withSuccessToken(token: String): Request {
|
private fun Request.withSuccessToken(token: String): Request {
|
||||||
@@ -105,10 +131,13 @@ class Error14HandlingInterceptor : Interceptor {
|
|||||||
private fun Request.withCookie(): Request {
|
private fun Request.withCookie(): Request {
|
||||||
return newBuilder().apply { cookie.get()?.let { addHeader("Cookie", it) } }.build()
|
return newBuilder().apply { cookie.get()?.let { addHeader("Cookie", it) } }.build()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private companion object {
|
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
|
||||||
private const val CAPTCHA_ERROR_CODE = 14
|
private inline fun Any.wait() = (this as Object).wait()
|
||||||
private const val CAPTCHA_ERROR_KIND = "need_captcha"
|
|
||||||
private val CAPTCHA_TIMEOUT = 10.minutes
|
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
|
||||||
}
|
private inline fun Any.notify() = (this as Object).notify()
|
||||||
}
|
|
||||||
|
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
|
||||||
|
private inline fun Any.notifyAll() = (this as Object).notifyAll()
|
||||||
|
|||||||
-7
@@ -2,7 +2,6 @@ package dev.meloda.fast.network.service.messages
|
|||||||
|
|
||||||
import com.slack.eithernet.ApiResult
|
import com.slack.eithernet.ApiResult
|
||||||
import dev.meloda.fast.model.api.data.VkChatData
|
import dev.meloda.fast.model.api.data.VkChatData
|
||||||
import dev.meloda.fast.model.api.data.LongPollHistoryResponse
|
|
||||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||||
import dev.meloda.fast.model.api.data.VkMessageData
|
import dev.meloda.fast.model.api.data.VkMessageData
|
||||||
import dev.meloda.fast.model.api.responses.MessagesCreateChatResponse
|
import dev.meloda.fast.model.api.responses.MessagesCreateChatResponse
|
||||||
@@ -45,12 +44,6 @@ interface MessagesService {
|
|||||||
@FieldMap params: Map<String, String>
|
@FieldMap params: Map<String, String>
|
||||||
): ApiResult<ApiResponse<VkLongPollData>, RestApiError>
|
): ApiResult<ApiResponse<VkLongPollData>, RestApiError>
|
||||||
|
|
||||||
@FormUrlEncoded
|
|
||||||
@POST(MessagesUrls.GET_LONG_POLL_HISTORY)
|
|
||||||
suspend fun getLongPollHistory(
|
|
||||||
@FieldMap params: Map<String, String>
|
|
||||||
): ApiResult<ApiResponse<LongPollHistoryResponse>, RestApiError>
|
|
||||||
|
|
||||||
@FormUrlEncoded
|
@FormUrlEncoded
|
||||||
@POST(MessagesUrls.MARK_AS_READ)
|
@POST(MessagesUrls.MARK_AS_READ)
|
||||||
suspend fun markAsRead(
|
suspend fun markAsRead(
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ android {
|
|||||||
dependencies {
|
dependencies {
|
||||||
api(projects.core.common)
|
api(projects.core.common)
|
||||||
api(projects.core.model)
|
api(projects.core.model)
|
||||||
|
api(projects.core.logger)
|
||||||
implementation(projects.core.presentation)
|
implementation(projects.core.presentation)
|
||||||
|
|
||||||
implementation(libs.haze)
|
implementation(libs.haze)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package dev.meloda.fast.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
|
||||||
|
val LocalLogger = compositionLocalOf { FastLogger.getInstance() }
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package dev.meloda.fast.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
|
import dev.meloda.fast.common.model.NetworkState
|
||||||
|
|
||||||
|
val LocalNetworkState = compositionLocalOf { NetworkState.DISCONNECTED }
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.meloda.fast.ui.components
|
package dev.meloda.fast.ui.components
|
||||||
|
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
|
import androidx.compose.foundation.Indication
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.combinedClickable
|
import androidx.compose.foundation.combinedClickable
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
@@ -20,6 +21,8 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.Shape
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3ExpressiveApi::class)
|
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3ExpressiveApi::class)
|
||||||
@@ -30,27 +33,32 @@ fun FastIconButton(
|
|||||||
onLongClick: (() -> Unit)? = null,
|
onLongClick: (() -> Unit)? = null,
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
|
colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
|
||||||
|
containerColor: Color = colors.containerColor(enabled),
|
||||||
|
contentColor: Color = colors.contentColor(enabled),
|
||||||
|
size: Dp = IconButtonTokens.StateLayerSize,
|
||||||
|
shape: Shape = IconButtonTokens.StateLayerShape,
|
||||||
|
alignment: Alignment = Alignment.Center,
|
||||||
interactionSource: MutableInteractionSource? = null,
|
interactionSource: MutableInteractionSource? = null,
|
||||||
|
indication: Indication = ripple(),
|
||||||
content: @Composable () -> Unit
|
content: @Composable () -> Unit
|
||||||
) {
|
) {
|
||||||
Box(
|
Box(
|
||||||
modifier =
|
modifier =
|
||||||
modifier
|
modifier
|
||||||
.minimumInteractiveComponentSize()
|
.minimumInteractiveComponentSize()
|
||||||
.size(IconButtonTokens.StateLayerSize)
|
.size(size)
|
||||||
.clip(IconButtonTokens.StateLayerShape)
|
.clip(shape)
|
||||||
.background(color = colors.containerColor(enabled))
|
.background(containerColor)
|
||||||
.combinedClickable(
|
.combinedClickable(
|
||||||
onClick = onClick,
|
onClick = onClick,
|
||||||
onLongClick = onLongClick,
|
onLongClick = onLongClick,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
indication = ripple()
|
indication = indication
|
||||||
),
|
),
|
||||||
contentAlignment = Alignment.Center
|
contentAlignment = alignment
|
||||||
) {
|
) {
|
||||||
val contentColor = colors.contentColor(enabled)
|
CompositionLocalProvider(LocalContentColor provides contentColor) { content() }
|
||||||
CompositionLocalProvider(LocalContentColor provides contentColor, content = content)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package dev.meloda.fast.ui.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.border
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.layout.sizeIn
|
||||||
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
|
import androidx.compose.foundation.shape.CornerBasedShape
|
||||||
|
import androidx.compose.foundation.shape.CornerSize
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.VerticalDivider
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.Shape
|
||||||
|
import androidx.compose.ui.res.painterResource
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import dev.meloda.fast.ui.util.ImmutableList
|
||||||
|
|
||||||
|
data class SegmentedButtonItem(
|
||||||
|
val key: String,
|
||||||
|
val iconResId: Int
|
||||||
|
)
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SegmentedButtonsRow(
|
||||||
|
items: ImmutableList<SegmentedButtonItem>,
|
||||||
|
onClick: (index: Int) -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
containerShape: CornerBasedShape = RoundedCornerShape(24.dp),
|
||||||
|
containerColor: Color = MaterialTheme.colorScheme.background,
|
||||||
|
borderColor: Color = MaterialTheme.colorScheme.outlineVariant,
|
||||||
|
borderSize: Dp = 1.dp,
|
||||||
|
iconContainerWidth: Dp = 42.dp,
|
||||||
|
iconContainerHeight: Dp = 36.dp,
|
||||||
|
iconSize: Dp = 18.dp,
|
||||||
|
showDividers: Boolean = true
|
||||||
|
) {
|
||||||
|
SegmentedButtonsRow(
|
||||||
|
modifier = modifier.sizeIn(maxHeight = iconContainerHeight + borderSize),
|
||||||
|
items = items.mapIndexed { index, item ->
|
||||||
|
{
|
||||||
|
val first = index == 0
|
||||||
|
val last = index == items.lastIndex
|
||||||
|
|
||||||
|
if (showDividers && !first) {
|
||||||
|
VerticalDivider(modifier = Modifier.padding(vertical = iconContainerHeight / 4))
|
||||||
|
}
|
||||||
|
|
||||||
|
SegmentedButton(
|
||||||
|
onClick = { onClick(index) },
|
||||||
|
iconResId = item.iconResId,
|
||||||
|
modifier = Modifier.size(
|
||||||
|
iconContainerWidth,
|
||||||
|
iconContainerHeight
|
||||||
|
),
|
||||||
|
iconSize = iconSize,
|
||||||
|
shape = containerShape.copy(
|
||||||
|
topStart = if (!first) CornerSize(0.dp) else containerShape.topStart,
|
||||||
|
bottomStart = if (!first) CornerSize(0.dp) else containerShape.bottomStart,
|
||||||
|
topEnd = if (!last) CornerSize(0.dp) else containerShape.topEnd,
|
||||||
|
bottomEnd = if (!last) CornerSize(0.dp) else containerShape.bottomEnd
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
containerShape = containerShape,
|
||||||
|
containerColor = containerColor,
|
||||||
|
borderColor = borderColor,
|
||||||
|
borderSize = borderSize
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SegmentedButtonsRow(
|
||||||
|
items: ImmutableList<@Composable () -> Unit>,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
containerShape: CornerBasedShape = RoundedCornerShape(24.dp),
|
||||||
|
containerColor: Color = MaterialTheme.colorScheme.background,
|
||||||
|
borderColor: Color = MaterialTheme.colorScheme.outlineVariant,
|
||||||
|
borderSize: Dp = 1.dp,
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier = modifier
|
||||||
|
.background(containerColor, containerShape)
|
||||||
|
.border(borderSize, borderColor, containerShape)
|
||||||
|
) {
|
||||||
|
items.forEach { it.invoke() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SegmentedButton(
|
||||||
|
onClick: () -> Unit,
|
||||||
|
iconResId: Int,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
iconSize: Dp = 18.dp,
|
||||||
|
shape: Shape = CircleShape
|
||||||
|
) {
|
||||||
|
FastIconButton(
|
||||||
|
onClick = onClick,
|
||||||
|
modifier = modifier,
|
||||||
|
shape = shape
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
painter = painterResource(iconResId),
|
||||||
|
contentDescription = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -58,7 +58,7 @@ fun AppTheme(
|
|||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val colorScheme: ColorScheme = when {
|
val colorScheme: ColorScheme = predefinedColorScheme ?: when {
|
||||||
useDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
useDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||||
if (useDarkTheme) dynamicDarkColorScheme(context)
|
if (useDarkTheme) dynamicDarkColorScheme(context)
|
||||||
else dynamicLightColorScheme(context)
|
else dynamicLightColorScheme(context)
|
||||||
@@ -82,10 +82,6 @@ fun AppTheme(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val colorPrimary by animateColorAsState(colorScheme.primary)
|
|
||||||
val colorSurface by animateColorAsState(colorScheme.surface)
|
|
||||||
val colorBackground by animateColorAsState(colorScheme.background)
|
|
||||||
|
|
||||||
val typography = if (useSystemFont) {
|
val typography = if (useSystemFont) {
|
||||||
MaterialTheme.typography
|
MaterialTheme.typography
|
||||||
} else {
|
} else {
|
||||||
@@ -118,12 +114,7 @@ fun AppTheme(
|
|||||||
}
|
}
|
||||||
|
|
||||||
MaterialExpressiveTheme(
|
MaterialExpressiveTheme(
|
||||||
colorScheme = (predefinedColorScheme ?: colorScheme)
|
colorScheme = colorScheme,
|
||||||
.copy(
|
|
||||||
primary = colorPrimary,
|
|
||||||
background = colorBackground,
|
|
||||||
surface = colorSurface
|
|
||||||
),
|
|
||||||
typography = typography,
|
typography = typography,
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import androidx.compose.ui.graphics.luminance
|
|||||||
import androidx.compose.ui.graphics.painter.Painter
|
import androidx.compose.ui.graphics.painter.Painter
|
||||||
import androidx.compose.ui.graphics.toArgb
|
import androidx.compose.ui.graphics.toArgb
|
||||||
import androidx.compose.ui.input.key.onKeyEvent
|
import androidx.compose.ui.input.key.onKeyEvent
|
||||||
|
import androidx.compose.ui.platform.LocalConfiguration
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.colorResource
|
import androidx.compose.ui.res.colorResource
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
@@ -113,11 +114,12 @@ fun LazyListState.isScrollingUp(): Boolean {
|
|||||||
@Composable
|
@Composable
|
||||||
fun isNeedToEnableDarkMode(darkMode: DarkMode): Boolean {
|
fun isNeedToEnableDarkMode(darkMode: DarkMode): Boolean {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val configuration = LocalConfiguration.current
|
||||||
|
|
||||||
val appForceDarkMode = darkMode == DarkMode.ENABLED
|
val appForceDarkMode = darkMode == DarkMode.ENABLED
|
||||||
val appBatterySaver = darkMode == DarkMode.AUTO_BATTERY
|
val appBatterySaver = darkMode == DarkMode.AUTO_BATTERY
|
||||||
|
|
||||||
val systemUiNightMode = context.resources.configuration.uiMode
|
val systemUiNightMode = configuration.uiMode
|
||||||
|
|
||||||
val isSystemBatterySaver = context.getSystemService<PowerManager>()?.isPowerSaveMode == true
|
val isSystemBatterySaver = context.getSystemService<PowerManager>()?.isPowerSaveMode == true
|
||||||
val isSystemUsingDarkTheme =
|
val isSystemUsingDarkTheme =
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.meloda.fast.ui.util
|
package dev.meloda.fast.ui.util
|
||||||
|
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
|
import dev.meloda.fast.ui.util.ImmutableList.Companion.toImmutableList
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
class ImmutableList<T>(val values: List<T>) : Collection<T> {
|
class ImmutableList<T>(val values: List<T>) : Collection<T> {
|
||||||
@@ -57,3 +58,9 @@ class ImmutableList<T>(val values: List<T>) : Collection<T> {
|
|||||||
fun <T> emptyImmutableList(): ImmutableList<T> = ImmutableList(emptyList())
|
fun <T> emptyImmutableList(): ImmutableList<T> = ImmutableList(emptyList())
|
||||||
|
|
||||||
fun <T> immutableListOf(vararg elements: T) = ImmutableList(listOf(elements = elements))
|
fun <T> immutableListOf(vararg elements: T) = ImmutableList(listOf(elements = elements))
|
||||||
|
|
||||||
|
inline fun <T> buildImmutableList(builderAction: MutableList<T>.() -> Unit): ImmutableList<T> {
|
||||||
|
val mutableList = mutableListOf<T>()
|
||||||
|
mutableList.apply(builderAction)
|
||||||
|
return mutableList.toImmutableList()
|
||||||
|
}
|
||||||
|
|||||||
@@ -88,6 +88,7 @@
|
|||||||
<string name="message_attachments_video_message">Video message</string>
|
<string name="message_attachments_video_message">Video message</string>
|
||||||
<string name="message_attachments_group_sticker">Group sticker</string>
|
<string name="message_attachments_group_sticker">Group sticker</string>
|
||||||
<string name="message_attachments_sticker_pack_preview">Sticker pack preview</string>
|
<string name="message_attachments_sticker_pack_preview">Sticker pack preview</string>
|
||||||
|
<string name="message_attachments_channel_message">Channel message</string>
|
||||||
|
|
||||||
<string name="chat_interaction_uploading_file">Uploading file</string>
|
<string name="chat_interaction_uploading_file">Uploading file</string>
|
||||||
<string name="chat_interaction_uploading_photo">Uploading photo</string>
|
<string name="chat_interaction_uploading_photo">Uploading photo</string>
|
||||||
@@ -188,6 +189,7 @@
|
|||||||
<string name="title_application_language">Application language</string>
|
<string name="title_application_language">Application language</string>
|
||||||
<string name="action_refresh">Refresh</string>
|
<string name="action_refresh">Refresh</string>
|
||||||
<string name="title_loading">Loading…</string>
|
<string name="title_loading">Loading…</string>
|
||||||
|
<string name="title_no_network">No network…</string>
|
||||||
<string name="title_convos">Conversations</string>
|
<string name="title_convos">Conversations</string>
|
||||||
<string name="title_archive">Archive</string>
|
<string name="title_archive">Archive</string>
|
||||||
<string name="title_friends">Friends</string>
|
<string name="title_friends">Friends</string>
|
||||||
@@ -305,4 +307,7 @@
|
|||||||
<string name="confirm_chat_create_empty_with_title">Are you sure you want to create chat «%s» only with yourself?</string>
|
<string name="confirm_chat_create_empty_with_title">Are you sure you want to create chat «%s» only with yourself?</string>
|
||||||
|
|
||||||
<string name="title_edit_message">Edit message</string>
|
<string name="title_edit_message">Edit message</string>
|
||||||
|
<string name="action_close">Close</string>
|
||||||
|
<string name="action_hide_stacktrace">Hide stacktrace</string>
|
||||||
|
<string name="action_show_stacktrace">Show stacktrace</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -2,4 +2,13 @@
|
|||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar" />
|
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar" />
|
||||||
|
|
||||||
|
<style name="CrashDialogTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
|
||||||
|
<item name="android:windowIsTranslucent">true</item>
|
||||||
|
<item name="android:windowBackground">@android:color/transparent</item>
|
||||||
|
<item name="android:windowContentOverlay">@null</item>
|
||||||
|
<item name="android:windowNoTitle">true</item>
|
||||||
|
<item name="android:windowIsFloating">true</item>
|
||||||
|
<item name="android:backgroundDimEnabled">false</item>
|
||||||
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
+14
-8
@@ -1,7 +1,6 @@
|
|||||||
package dev.meloda.fast.auth.captcha.presentation
|
package dev.meloda.fast.auth.captcha.presentation
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.util.Log
|
|
||||||
import android.webkit.JavascriptInterface
|
import android.webkit.JavascriptInterface
|
||||||
import android.webkit.WebResourceRequest
|
import android.webkit.WebResourceRequest
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
@@ -32,7 +31,9 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
|||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.viewinterop.AndroidView
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
|
import dev.meloda.fast.logger.FastLogger
|
||||||
import dev.meloda.fast.ui.R
|
import dev.meloda.fast.ui.R
|
||||||
|
import dev.meloda.fast.ui.common.LocalLogger
|
||||||
import dev.meloda.fast.ui.components.ActionInvokeDismiss
|
import dev.meloda.fast.ui.components.ActionInvokeDismiss
|
||||||
import dev.meloda.fast.ui.components.FullScreenDialog
|
import dev.meloda.fast.ui.components.FullScreenDialog
|
||||||
import dev.meloda.fast.ui.components.MaterialDialog
|
import dev.meloda.fast.ui.components.MaterialDialog
|
||||||
@@ -46,6 +47,8 @@ fun CaptchaScreen(
|
|||||||
onBack: () -> Unit = {},
|
onBack: () -> Unit = {},
|
||||||
onResult: (String) -> Unit = {}
|
onResult: (String) -> Unit = {}
|
||||||
) {
|
) {
|
||||||
|
val logger = LocalLogger.current
|
||||||
|
|
||||||
if (captchaRedirectUri != null) {
|
if (captchaRedirectUri != null) {
|
||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
@@ -114,7 +117,10 @@ fun CaptchaScreen(
|
|||||||
view: WebView?,
|
view: WebView?,
|
||||||
request: WebResourceRequest?
|
request: WebResourceRequest?
|
||||||
): Boolean {
|
): Boolean {
|
||||||
Log.i(TAG, "shouldOverrideUrlLoading: $request")
|
logger.info(
|
||||||
|
"CaptchaScreen",
|
||||||
|
"WebViewClient(): shouldOverrideUrlLoading(): request: $request"
|
||||||
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +154,8 @@ fun CaptchaScreen(
|
|||||||
// TODO: 03/05/2026, Danil Nikolaev: show error
|
// TODO: 03/05/2026, Danil Nikolaev: show error
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onCloseRequested = { showExitAlert = true }
|
onCloseRequested = { showExitAlert = true },
|
||||||
|
logger = logger
|
||||||
),
|
),
|
||||||
"AndroidBridge"
|
"AndroidBridge"
|
||||||
)
|
)
|
||||||
@@ -176,19 +183,18 @@ fun CaptchaScreen(
|
|||||||
|
|
||||||
class WebCaptchaListener(
|
class WebCaptchaListener(
|
||||||
private val onSuccessTokenReceived: (String) -> Unit,
|
private val onSuccessTokenReceived: (String) -> Unit,
|
||||||
private val onCloseRequested: (String) -> Unit
|
private val onCloseRequested: (String) -> Unit,
|
||||||
|
private val logger: FastLogger
|
||||||
) {
|
) {
|
||||||
private val tag = "WebCaptchaListener"
|
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun VKCaptchaGetResult(arg: String) {
|
fun VKCaptchaGetResult(arg: String) {
|
||||||
onSuccessTokenReceived(arg)
|
onSuccessTokenReceived(arg)
|
||||||
Log.i(tag, "VKCaptchaGetResult($arg)")
|
logger.info(this::class, "VKCaptchaGetResult(): arg: $arg")
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun VKCaptchaCloseCaptcha(arg: String) {
|
fun VKCaptchaCloseCaptcha(arg: String) {
|
||||||
onCloseRequested(arg)
|
onCloseRequested(arg)
|
||||||
Log.i(tag, "VKCaptchaCloseCaptcha($arg)")
|
logger.info(this::class, "VKCaptchaCloseCaptcha(): arg: $arg")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package dev.meloda.fast.auth.login
|
|||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dev.meloda.fast.auth.login.model.CaptchaArguments
|
import dev.meloda.fast.auth.login.model.CaptchaArguments
|
||||||
@@ -28,7 +27,8 @@ import dev.meloda.fast.datastore.AppSettings
|
|||||||
import dev.meloda.fast.datastore.UserSettings
|
import dev.meloda.fast.datastore.UserSettings
|
||||||
import dev.meloda.fast.domain.LoadUserByIdUseCase
|
import dev.meloda.fast.domain.LoadUserByIdUseCase
|
||||||
import dev.meloda.fast.domain.OAuthUseCase
|
import dev.meloda.fast.domain.OAuthUseCase
|
||||||
import dev.meloda.fast.model.database.AccountEntity
|
import dev.meloda.fast.logger.FastLogger
|
||||||
|
import dev.meloda.fast.model.AccountDto
|
||||||
import dev.meloda.fast.network.OAuthErrorDomain
|
import dev.meloda.fast.network.OAuthErrorDomain
|
||||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -48,7 +48,8 @@ class LoginViewModel(
|
|||||||
private val accountsRepository: AccountsRepository,
|
private val accountsRepository: AccountsRepository,
|
||||||
private val loginValidator: LoginValidator,
|
private val loginValidator: LoginValidator,
|
||||||
private val longPollController: LongPollController,
|
private val longPollController: LongPollController,
|
||||||
private val userSettings: UserSettings
|
private val userSettings: UserSettings,
|
||||||
|
private val logger: FastLogger
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val _screenState = MutableStateFlow(LoginScreenState.EMPTY)
|
private val _screenState = MutableStateFlow(LoginScreenState.EMPTY)
|
||||||
val screenState = _screenState.asStateFlow()
|
val screenState = _screenState.asStateFlow()
|
||||||
@@ -100,7 +101,13 @@ class LoginViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun onBackPressed() {
|
fun onBackPressed() {
|
||||||
_screenState.setValue { old -> old.copy(showLogo = true) }
|
_screenState.setValue { old ->
|
||||||
|
old.copy(
|
||||||
|
showLogo = true,
|
||||||
|
loginError = false,
|
||||||
|
passwordError = false
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPasswordVisibilityButtonClicked() {
|
fun onPasswordVisibilityButtonClicked() {
|
||||||
@@ -183,7 +190,7 @@ class LoginViewModel(
|
|||||||
).listenValue(viewModelScope) { state ->
|
).listenValue(viewModelScope) { state ->
|
||||||
state.processState(
|
state.processState(
|
||||||
error = { error ->
|
error = { error ->
|
||||||
Log.d("LoginViewModelImpl", "login: error: $error")
|
logger.error(this::class, "getSilentToken(): ERROR: $error")
|
||||||
|
|
||||||
_screenState.updateValue { copy(isLoading = false) }
|
_screenState.updateValue { copy(isLoading = false) }
|
||||||
|
|
||||||
@@ -229,7 +236,7 @@ class LoginViewModel(
|
|||||||
|
|
||||||
// TODO: 30-Mar-25, Danil Nikolaev: get fast's app token
|
// TODO: 30-Mar-25, Danil Nikolaev: get fast's app token
|
||||||
|
|
||||||
val currentAccount = AccountEntity(
|
val currentAccount = AccountDto(
|
||||||
userId = userId,
|
userId = userId,
|
||||||
accessToken = accessToken,
|
accessToken = accessToken,
|
||||||
fastToken = null,
|
fastToken = null,
|
||||||
@@ -244,7 +251,7 @@ class LoginViewModel(
|
|||||||
UserConfig.exchangeToken = account.exchangeToken
|
UserConfig.exchangeToken = account.exchangeToken
|
||||||
}
|
}
|
||||||
|
|
||||||
accountsRepository.storeAccounts(listOf(currentAccount))
|
accountsRepository.storeAccounts(listOf(currentAccount.mapToEntity()))
|
||||||
|
|
||||||
startLongPoll()
|
startLongPoll()
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user