Move addRecentDocument from HomeViewModel to ExportViewModel
This commit is contained in:
@@ -35,6 +35,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.fairscan.app.AppContainer
|
||||
import org.fairscan.app.RecentDocument
|
||||
import org.fairscan.app.data.GeneratedPdf
|
||||
import org.fairscan.app.data.PdfFileManager
|
||||
import org.fairscan.app.ui.screens.home.HomeViewModel
|
||||
@@ -53,6 +54,7 @@ class ExportViewModel(container: AppContainer): ViewModel() {
|
||||
private val pdfFileManager = container.pdfFileManager
|
||||
private val imageRepository = container.imageRepository
|
||||
private val settingsRepository = container.settingsRepository
|
||||
private val recentDocumentsDataStore = container.recentDocumentsDataStore
|
||||
private val logger = container.logger
|
||||
|
||||
private val _events = MutableSharedFlow<ExportEvent>()
|
||||
@@ -165,8 +167,7 @@ class ExportViewModel(container: AppContainer): ViewModel() {
|
||||
|
||||
fileInDownloads?.let { mediaScan(context, it) }
|
||||
|
||||
// TODO remove that call: that should be handled through the ExportEvent
|
||||
homeViewModel.addRecentDocument(savedUri, savedName, pdf.pageCount)
|
||||
addRecentDocument(savedUri, savedName, pdf.pageCount)
|
||||
} catch (e: Exception) {
|
||||
logger.e("FairScan", "Failed to save PDF", e)
|
||||
_events.emit(ExportEvent.SaveError)
|
||||
@@ -217,6 +218,27 @@ class ExportViewModel(container: AppContainer): ViewModel() {
|
||||
DocumentFile.fromTreeUri(context, exportDirUri)?.name
|
||||
}
|
||||
}
|
||||
|
||||
fun addRecentDocument(fileUri: Uri, fileName: String, pageCount: Int) {
|
||||
viewModelScope.launch {
|
||||
recentDocumentsDataStore.updateData { current ->
|
||||
val newDoc = RecentDocument.newBuilder()
|
||||
.setFileUri(fileUri.toString())
|
||||
.setFileName(fileName)
|
||||
.setPageCount(pageCount)
|
||||
.setCreatedAt(System.currentTimeMillis())
|
||||
.build()
|
||||
current.toBuilder()
|
||||
.addDocuments(0, newDoc)
|
||||
.also { builder ->
|
||||
while (builder.documentsCount > 3) {
|
||||
builder.removeDocuments(builder.documentsCount - 1)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class PdfGenerationActions(
|
||||
|
||||
@@ -24,9 +24,7 @@ import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import org.fairscan.app.AppContainer
|
||||
import org.fairscan.app.RecentDocument
|
||||
import java.io.File
|
||||
|
||||
class HomeViewModel(appContainer: AppContainer, appContext: Context): ViewModel() {
|
||||
@@ -62,27 +60,6 @@ class HomeViewModel(appContainer: AppContainer, appContext: Context): ViewModel(
|
||||
initialValue = emptyList(),
|
||||
)
|
||||
|
||||
fun addRecentDocument(fileUri: Uri, fileName: String, pageCount: Int) {
|
||||
viewModelScope.launch {
|
||||
recentDocumentsDataStore.updateData { current ->
|
||||
val newDoc = RecentDocument.newBuilder()
|
||||
.setFileUri(fileUri.toString())
|
||||
.setFileName(fileName)
|
||||
.setPageCount(pageCount)
|
||||
.setCreatedAt(System.currentTimeMillis())
|
||||
.build()
|
||||
current.toBuilder()
|
||||
.addDocuments(0, newDoc)
|
||||
.also { builder ->
|
||||
while (builder.documentsCount > 3) {
|
||||
builder.removeDocuments(builder.documentsCount - 1)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun uriExists(context: Context, uri: Uri): Boolean {
|
||||
return if (uri.scheme == "file") {
|
||||
File(uri.path.orEmpty()).exists()
|
||||
|
||||
Reference in New Issue
Block a user