Skip to content

Commit

Permalink
[GHP-41537911] feat: add support opening history file
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiationX committed Mar 4, 2024
1 parent 8aa468b commit 2aad112
Show file tree
Hide file tree
Showing 28 changed files with 392 additions and 74 deletions.
49 changes: 29 additions & 20 deletions app-mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,35 @@
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="anilibria.tv"
android:pathPattern="/release/.*"
android:scheme="http" />
<data
android:host="www.anilibria.tv"
android:pathPattern="/release/.*"
android:scheme="http" />
<data
android:host="anilibria.tv"
android:pathPattern="/release/.*"
android:scheme="https" />
<data
android:host="www.anilibria.tv"
android:pathPattern="/release/.*"
android:scheme="https" />

<data
android:host="anilibria.app"
android:scheme="app" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="anilibria.tv" />
<data android:host="www.anilibria.tv" />
<data android:host="iss.ink" />
<data android:pathPattern="/release/.*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="app" />
<data android:host="anilibria.app" />
</intent-filter>

<intent-filter
android:label="Импорт истории"
tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:mimeType="application/json" />
<data android:mimeType="text/json" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.radiationx.anilibria.navigation

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.fragment.app.Fragment
import ru.radiationx.anilibria.ui.activities.SettingsActivity
import ru.radiationx.anilibria.ui.activities.WebPlayerActivity
Expand Down Expand Up @@ -105,8 +106,8 @@ object Screens {
override fun getFragment() = PageFragment.newInstance(pagePath)
}

class History : BaseAppScreen() {
override fun getFragment() = HistoryFragment()
class History(private val importUri: Uri? = null) : BaseAppScreen() {
override fun getFragment() = HistoryFragment.newInstance(importUri)
}

class Schedule(val day: Int? = null) : BaseAppScreen() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import ru.radiationx.anilibria.navigation.Screens
import ru.radiationx.anilibria.presentation.common.ILinkHandler
import ru.radiationx.anilibria.ui.activities.BaseActivity
Expand Down Expand Up @@ -35,7 +36,11 @@ class IntentActivity : BaseActivity() {
val intent = Screens.Main(intentUri.toString()).getActivityIntent(this)
startActivity(intent)
} else {
systemUtils.externalLink(intentUri.toString())
if (intentUri.scheme?.let { it.startsWith("https") || it.startsWith("http") } == true) {
systemUtils.externalLink(intentUri.toString())
} else {
Toast.makeText(this, "Действие не поддерживается", Toast.LENGTH_SHORT).show()
}
}
}
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import kotlinx.coroutines.launch
import ru.radiationx.anilibria.presentation.common.IErrorHandler
import ru.radiationx.data.analytics.features.UpdaterAnalytics
import ru.radiationx.data.downloader.DownloadedFile
import ru.radiationx.data.downloader.LocalFile
import ru.radiationx.data.downloader.RemoteFileRepository
import ru.radiationx.data.downloader.RemoteFile
import ru.radiationx.data.downloader.toLocalFile
import ru.radiationx.data.entity.domain.updater.UpdateData
import ru.radiationx.data.repository.CheckerRepository
import ru.radiationx.quill.QuillExtra
Expand Down Expand Up @@ -40,7 +42,7 @@ class CheckerViewModel(
MutableStateFlow<Map<UpdateData.UpdateLink, MutableStateFlow<Int>>>(emptyMap())
private val _currentData = MutableStateFlow(CheckerScreenState())

val openDownloadedFileAction = EventFlow<DownloadedFile>()
val openDownloadedFileAction = EventFlow<LocalFile>()

val state = combine(
_currentLoadings,
Expand Down Expand Up @@ -98,7 +100,7 @@ class CheckerViewModel(
coRunCatching {
remoteFileRepository.loadFile(link.url, RemoteFile.Bucket.AppUpdates, progress)
}.onSuccess {
openDownloadedFileAction.set(it)
openDownloadedFileAction.set(it.toLocalFile())
}.onFailure {
errorHandler.handle(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class UpdateCheckerActivity : BaseActivity(R.layout.activity_updater) {
}.launchIn(lifecycleScope)

viewModel.openDownloadedFileAction.observe().onEach {
systemUtils.openDownloadedFile(it)
systemUtils.openLocalFile(it)
}.launchInResumed(this)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.radiationx.anilibria.ui.common

import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonEncodingException
import ru.radiationx.anilibria.presentation.common.IErrorHandler
import ru.radiationx.anilibria.utils.messages.SystemMessenger
import ru.radiationx.data.datasource.remote.ApiError
Expand All @@ -12,7 +14,7 @@ import javax.inject.Inject
* Created by radiationx on 23.02.18.
*/
class ErrorHandler @Inject constructor(
private val systemMessenger: SystemMessenger
private val systemMessenger: SystemMessenger,
) : IErrorHandler {

override fun handle(throwable: Throwable, messageListener: ((Throwable, String?) -> Unit)?) {
Expand All @@ -29,7 +31,8 @@ class ErrorHandler @Inject constructor(
is IOException -> "Нет соединения с интернетом"
is HttpException -> throwable.message
is ApiError -> throwable.userMessage()
else -> throwable.message.orEmpty()
is JsonDataException -> "Неправильный формат данных"
else -> throwable.message ?: "Неизвестная ошибка"
}

private fun ApiError.userMessage() = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.radiationx.anilibria.ui.common

import android.net.Uri
import android.util.Log
import ru.radiationx.anilibria.navigation.BaseAppScreen
import ru.radiationx.anilibria.navigation.Screens
import ru.radiationx.anilibria.presentation.common.ILinkHandler
Expand All @@ -14,13 +16,17 @@ import javax.inject.Inject
* Created by radiationx on 03.02.18.
*/
class LinkRouter @Inject constructor(
private val releaseAnalytics: ReleaseAnalytics
private val releaseAnalytics: ReleaseAnalytics,
) : ILinkHandler {

private val releaseDetail by lazy {
Pattern.compile("\\/release\\/([\\s\\S]*?)\\.html|tracker\\/\\?ELEMENT_CODE=([^&]+)")
}

private val historyImport by lazy {
Pattern.compile("^content[\\s\\S]*?\\.json\$")
}

override fun handle(url: String, router: Router?, doNavigate: Boolean): Boolean {
findScreen(url)?.also { screen ->
if (doNavigate) {
Expand All @@ -42,6 +48,11 @@ class LinkRouter @Inject constructor(
return Screens.ReleaseDetails(code = ReleaseCode(code))
}
}
historyImport.matcher(url).let {
if (it.find()) {
return Screens.History(Uri.parse(url))
}
}
return null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.radiationx.anilibria.ui.fragments.history

import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import ru.radiationx.anilibria.ui.common.ErrorHandler
import ru.radiationx.anilibria.utils.messages.SystemMessenger
import ru.radiationx.data.historyfile.HistoryFileRepository
import ru.radiationx.shared.ktx.coRunCatching
import ru.radiationx.shared_app.common.SystemUtils
import toothpick.InjectConstructor

@InjectConstructor
class HistoryFileViewModel(
private val historyFileRepository: HistoryFileRepository,
private val systemUtils: SystemUtils,
private val errorHandler: ErrorHandler,
private val systemMessenger: SystemMessenger,
) : ViewModel() {

fun onExportClick() {
viewModelScope.launch {
coRunCatching {
historyFileRepository.exportFile()
}.onSuccess {
systemUtils.shareLocalFile(it)
}.onFailure {
errorHandler.handle(it)
}
}
}

fun onImportFileSelected(uri: Uri) {
viewModelScope.launch {
coRunCatching {
historyFileRepository.importFile(uri)
}.onSuccess {
systemMessenger.showMessage("История успешно импортирована")
}.onFailure {
errorHandler.handle(it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ru.radiationx.anilibria.ui.fragments.history

import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.doOnLayout
Expand Down Expand Up @@ -31,7 +33,9 @@ import ru.radiationx.anilibria.ui.fragments.release.list.ReleasesAdapter
import ru.radiationx.anilibria.utils.Dimensions
import ru.radiationx.anilibria.utils.ToolbarHelper
import ru.radiationx.quill.viewModel
import ru.radiationx.shared.ktx.android.getExtra
import ru.radiationx.shared.ktx.android.postopneEnterTransitionWithTimout
import ru.radiationx.shared.ktx.android.putExtra
import ru.radiationx.shared.ktx.android.showWithLifecycle

/**
Expand All @@ -43,6 +47,14 @@ class HistoryFragment :
ReleasesAdapter.ItemListener,
TopScroller {

companion object {
private const val ARG_IMPORT_URI = "import_uri"

fun newInstance(importUri: Uri?) = HistoryFragment().putExtra {
putParcelable(ARG_IMPORT_URI, importUri)
}
}

override var sharedViewLocal: View? = null

override fun getSharedView(): View? {
Expand All @@ -56,6 +68,12 @@ class HistoryFragment :
private val adapter = ReleasesAdapter(
loadMoreListener = { },
loadRetryListener = {},
importListener = {
importLauncher.launch("application/json")
},
exportListener = {
fileViewModel.onExportClick()
},
listener = this,
emptyPlaceHolder = PlaceholderListItem(
R.drawable.ic_history,
Expand All @@ -74,6 +92,13 @@ class HistoryFragment :
}

private val viewModel by viewModel<HistoryViewModel>()
private val fileViewModel by viewModel<HistoryFileViewModel>()

private val importLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) {
if (it != null) {
fileViewModel.onImportFileSelected(it)
}
}

override val statusBarVisible: Boolean = true

Expand Down Expand Up @@ -153,6 +178,12 @@ class HistoryFragment :
viewModel.state.onEach {
showState(it)
}.launchIn(viewLifecycleOwner.lifecycleScope)

val importUri = getExtra<Uri>(ARG_IMPORT_URI)
if (importUri != null) {
fileViewModel.onImportFileSelected(importUri)
arguments?.remove(ARG_IMPORT_URI)
}
}

override fun updateDimens(dimensions: Dimensions) {
Expand Down Expand Up @@ -208,7 +239,7 @@ class HistoryFragment :
private fun showState(state: HistoryScreenState) {
binding.progressBarList.isVisible = state.data.emptyLoading
binding.refreshLayout.isRefreshing = state.data.refreshLoading
adapter.bindState(state.data)
adapter.bindState(state.data, withExport = true)
searchAdapter.items = state.searchItems.map { ReleaseListItem(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ package ru.radiationx.anilibria.ui.fragments.history

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import ru.radiationx.anilibria.model.ReleaseItemState
import ru.radiationx.anilibria.model.loading.DataLoadingController
Expand Down Expand Up @@ -32,7 +39,7 @@ class HistoryViewModel(
private val historyAnalytics: HistoryAnalytics,
private val releaseAnalytics: ReleaseAnalytics,
private val shortcutHelper: ShortcutHelper,
private val systemUtils: SystemUtils
private val systemUtils: SystemUtils,
) : ViewModel() {

private val loadingController = DataLoadingController(viewModelScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ class ReleaseInfoFragment : BaseDimensionsFragment(R.layout.fragment_list), TopS
}.launchInResumed(viewLifecycleOwner)

viewModel.openDownloadedFileAction.observe().onEach {
systemUtils.openDownloadedFile(it)
systemUtils.openLocalFile(it)
}.launchInResumed(viewLifecycleOwner)

viewModel.shareDownloadedFileAction.observe().onEach {
systemUtils.shareDownloadedFile(it)
systemUtils.shareLocalFile(it)
}.launchInResumed(viewLifecycleOwner)
}

Expand Down
Loading

0 comments on commit 2aad112

Please sign in to comment.