mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
download pdf file and store it locally
This commit is contained in:
parent
9fc81ed50d
commit
a466887dc7
2 changed files with 41 additions and 16 deletions
|
|
@ -37,14 +37,15 @@ class PDFReaderActivity: AppCompatActivity() {
|
|||
|
||||
@Composable
|
||||
fun PDFReaderLoadingContainer(slug: String, pdfReaderViewModel: PDFReaderViewModel) {
|
||||
val context = LocalContext.current
|
||||
val pdfReaderParams: PDFReaderParams? by pdfReaderViewModel.pdfReaderParamsLiveData.observeAsState(null)
|
||||
|
||||
if (pdfReaderParams == null) {
|
||||
pdfReaderViewModel.loadItem(slug = slug)
|
||||
pdfReaderViewModel.loadItem(slug = slug, context)
|
||||
}
|
||||
|
||||
if (pdfReaderParams != null) {
|
||||
PDFDocumentView(urlString = pdfReaderParams!!.item.pageURLString)
|
||||
PDFDocumentView(documentUri = pdfReaderParams!!.localFileUri)
|
||||
} else {
|
||||
// TODO: add a proper loading view
|
||||
Text("Loading...")
|
||||
|
|
@ -53,7 +54,7 @@ fun PDFReaderLoadingContainer(slug: String, pdfReaderViewModel: PDFReaderViewMod
|
|||
|
||||
@OptIn(ExperimentalPSPDFKitApi::class)
|
||||
@Composable
|
||||
fun PDFDocumentView(urlString: String) {
|
||||
fun PDFDocumentView(documentUri: Uri) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val pdfActivityConfiguration = remember {
|
||||
|
|
@ -64,8 +65,7 @@ fun PDFDocumentView(urlString: String) {
|
|||
}
|
||||
|
||||
val pdfDocumentState = rememberDocumentState(
|
||||
documentUri = Uri.parse("file:///android_asset/test.pdf"),
|
||||
// documentUri = Uri.parse(urlString),
|
||||
documentUri = documentUri,
|
||||
configuration = pdfActivityConfiguration
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package app.omnivore.omnivore.ui.reader
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
|
|
@ -8,13 +10,18 @@ import app.omnivore.omnivore.models.LinkedItem
|
|||
import app.omnivore.omnivore.networking.Networker
|
||||
import app.omnivore.omnivore.networking.linkedItem
|
||||
import com.google.gson.Gson
|
||||
import com.pspdfkit.document.download.DownloadJob
|
||||
import com.pspdfkit.document.download.DownloadRequest
|
||||
import com.pspdfkit.document.download.Progress
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
data class PDFReaderParams(
|
||||
val item: LinkedItem,
|
||||
val articleContent: ArticleContent
|
||||
val articleContent: ArticleContent,
|
||||
val localFileUri: Uri
|
||||
)
|
||||
|
||||
@HiltViewModel
|
||||
|
|
@ -24,22 +31,40 @@ class PDFReaderViewModel @Inject constructor(
|
|||
): ViewModel() {
|
||||
val pdfReaderParamsLiveData = MutableLiveData<PDFReaderParams?>(null)
|
||||
|
||||
fun loadItem(slug: String) {
|
||||
fun loadItem(slug: String, context: Context) {
|
||||
viewModelScope.launch {
|
||||
val articleQueryResult = networker.linkedItem(slug)
|
||||
|
||||
val article = articleQueryResult.item ?: return@launch
|
||||
|
||||
val articleContent = ArticleContent(
|
||||
title = article.title,
|
||||
htmlContent = article.content ?: "",
|
||||
highlightsJSONString = Gson().toJson(articleQueryResult.highlights),
|
||||
contentStatus = "SUCCEEDED",
|
||||
objectID = "",
|
||||
labelsJSONString = Gson().toJson(articleQueryResult.labels)
|
||||
)
|
||||
val request = DownloadRequest.Builder(context)
|
||||
.uri(article.pageURLString)
|
||||
.build()
|
||||
|
||||
pdfReaderParamsLiveData.value = PDFReaderParams(article, articleContent)
|
||||
val job = DownloadJob.startDownload(request)
|
||||
|
||||
job.setProgressListener(object : DownloadJob.ProgressListenerAdapter() {
|
||||
override fun onProgress(progress: Progress) {
|
||||
// progressBar.setProgress((100f * progress.bytesReceived / progress.totalBytes).toInt())
|
||||
}
|
||||
|
||||
override fun onComplete(output: File) {
|
||||
val articleContent = ArticleContent(
|
||||
title = article.title,
|
||||
htmlContent = article.content ?: "",
|
||||
highlightsJSONString = Gson().toJson(articleQueryResult.highlights),
|
||||
contentStatus = "SUCCEEDED",
|
||||
objectID = "",
|
||||
labelsJSONString = Gson().toJson(articleQueryResult.labels)
|
||||
)
|
||||
|
||||
pdfReaderParamsLiveData.value = PDFReaderParams(article, articleContent, Uri.fromFile(output))
|
||||
}
|
||||
|
||||
override fun onError(exception: Throwable) {
|
||||
// handleDownloadError(exception)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue