Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Monitoring #107

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "androidx.documentfile:documentfile:1.0.1"

implementation 'com.jakewharton.timber:timber:5.0.1'

implementation "com.github.moxy-community:moxy:2.1.2"
implementation "com.github.moxy-community:moxy-androidx:2.1.2"
implementation "com.github.moxy-community:moxy-ktx:2.1.2"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/space/taran/arknavigator/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import space.taran.arknavigator.mvp.presenter.*
import space.taran.arknavigator.ui.activity.MainActivity
import space.taran.arknavigator.ui.fragments.*
import dagger.Component
import space.taran.arknavigator.mvp.presenter.adapter.PreviewsList
import space.taran.arknavigator.mvp.presenter.adapter.ResourcesGridPresenter
import space.taran.arknavigator.mvp.presenter.adapter.folderstree.FoldersTreePresenter
import space.taran.arknavigator.mvp.presenter.adapter.tagsselector.TagsSelectorPresenter
import javax.inject.Singleton

@Singleton
Expand All @@ -33,4 +35,6 @@ interface AppComponent {
fun inject(galleryFragment: GalleryFragment)
fun inject(resourcesGridPresenter: ResourcesGridPresenter)
fun inject(foldersTreePresenter: FoldersTreePresenter)
fun inject(tagsSelectorPresenter: TagsSelectorPresenter)
fun inject(previewsList: PreviewsList)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import android.util.Log
import space.taran.arknavigator.mvp.model.dao.Database
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import space.taran.arknavigator.mvp.model.IndexCache
import space.taran.arknavigator.mvp.model.IndexingEngine
import space.taran.arknavigator.mvp.model.TagsCache
import space.taran.arknavigator.mvp.model.fsmonitoring.FSMonitoring
import space.taran.arknavigator.mvp.model.repo.FoldersRepo
import space.taran.arknavigator.mvp.model.repo.ResourcesIndexFactory
import javax.inject.Singleton
Expand All @@ -23,4 +30,40 @@ class RepoModule {
Log.d("modules", "creating ResourcesIndexesRepo")
return ResourcesIndexFactory(database.resourceDao())
}

@Singleton
@Provides
fun indexCache(): IndexCache {
return IndexCache()
}

@Singleton
@Provides
fun tagsCache(indexCache: IndexCache): TagsCache {
return TagsCache(indexCache)
}

@Singleton
@Provides
fun appCoroutineScope(): CoroutineScope {
return CoroutineScope(SupervisorJob() + Dispatchers.Default)
}

@Singleton
@Provides
fun fsMonitoring(indexCache: IndexCache, tagsCache: TagsCache, appScope: CoroutineScope): FSMonitoring {
return FSMonitoring(indexCache, tagsCache, appScope)
}

@Singleton
@Provides
fun indexingEngine(
foldersRepo: FoldersRepo,
indexCache: IndexCache,
tagsCache: TagsCache,
resourcesIndexFactory: ResourcesIndexFactory,
fsMonitoring: FSMonitoring
): IndexingEngine {
return IndexingEngine(indexCache, tagsCache, foldersRepo, resourcesIndexFactory, fsMonitoring)
}
}
86 changes: 86 additions & 0 deletions app/src/main/java/space/taran/arknavigator/mvp/model/IndexCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package space.taran.arknavigator.mvp.model

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import space.taran.arknavigator.mvp.model.dao.ResourceId
import space.taran.arknavigator.mvp.model.repo.AggregatedResourcesIndex
import space.taran.arknavigator.mvp.model.repo.Difference
import space.taran.arknavigator.mvp.model.repo.PlainResourcesIndex
import java.nio.file.Path

class IndexCache {
private var indexByRoot = mutableMapOf<Path, PlainResourcesIndex>()
private var aggregatedIndex = AggregatedResourcesIndex(listOf())
private var flowByRootAndFav = mutableMapOf<RootAndFav, MutableStateFlow<Set<ResourceId>?>>()
private var allRootsFlow = MutableStateFlow<Set<ResourceId>?>(null)

suspend fun onIndexChange(root: Path, index: PlainResourcesIndex) {
indexByRoot[root] = index
emitChangesToAffectedRootAndFav(root, index)
}

suspend fun onResourceCreated(root: Path, resourcePath: Path) {
val index = indexByRoot[root]!!
index.reindexRoot(Difference(emptyList(), emptyList(), listOf(resourcePath)))
emitChangesToAffectedRootAndFav(root, index)
}

suspend fun onResourceDeleted(root: Path, resourcePath: Path): ResourceId {
val index = indexByRoot[root]!!
val id = index.metaByPath[resourcePath]!!.id
index.remove(id)
emitChangesToAffectedRootAndFav(root, indexByRoot[root]!!)
return id
}

suspend fun onResourceModified(root: Path, resourcePath: Path): PlainResourcesIndex {
val index = indexByRoot[root]!!
index.reindexRoot(Difference(emptyList(), listOf(resourcePath), emptyList()))
emitChangesToAffectedRootAndFav(root, index)
return index
}

suspend fun onReindexFinish() {
allRootsFlow.emit(aggregatedIndex.listAllIds())
}

fun getIndexByRoot(root: Path) = indexByRoot[root]!!

fun listenResourcesChanges(rootAndFav: RootAndFav): StateFlow<Set<ResourceId>?> {
return if (rootAndFav.isAllRoots()) {
allRootsFlow
} else {
if (flowByRootAndFav[rootAndFav] == null) {
indexByRoot[rootAndFav.root]?.let { index ->
flowByRootAndFav[rootAndFav] = MutableStateFlow(index.listIds(rootAndFav.fav))
} ?: run {
flowByRootAndFav[rootAndFav] = MutableStateFlow(null)
}
}
flowByRootAndFav[rootAndFav]!!
}
}

fun listIds(rootAndFav: RootAndFav): Set<ResourceId> {
return if (rootAndFav.isAllRoots())
aggregatedIndex.listIds(rootAndFav.fav)
else
indexByRoot[rootAndFav.root]?.listIds(rootAndFav.fav) ?: emptySet()
}

fun getPath(resourceId: ResourceId): Path? {
return aggregatedIndex.getPath(resourceId)
}

private suspend fun emitChangesToAffectedRootAndFav(root: Path, index: PlainResourcesIndex) {
aggregatedIndex = AggregatedResourcesIndex(indexByRoot.values)
if (allRootsFlow.value != null)
allRootsFlow.emit(aggregatedIndex.listAllIds())

val affectedRootAndFavs = flowByRootAndFav.keys.filter { it.root == root }
affectedRootAndFavs.forEach {
if (flowByRootAndFav[it]!!.value != index.listIds(it.fav))
flowByRootAndFav[it]!!.emit(index.listIds(it.fav))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package space.taran.arknavigator.mvp.model

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import space.taran.arknavigator.mvp.model.fsmonitoring.FSMonitoring
import space.taran.arknavigator.mvp.model.repo.FoldersRepo
import space.taran.arknavigator.mvp.model.repo.ResourcesIndexFactory
import java.nio.file.Path

class IndexingEngine(
private val indexCache: IndexCache,
private val tagsCache: TagsCache,
private val foldersRepo: FoldersRepo,
private val resourcesIndexFactory: ResourcesIndexFactory,
private val fsMonitoring: FSMonitoring
) {
suspend fun reindex() = withContext(Dispatchers.Default) {
val roots = foldersRepo.query().succeeded.keys
roots.forEach {
val index = resourcesIndexFactory.loadFromDatabase(it)
tagsCache.onIndexChanged(it, index)
indexCache.onIndexChange(it, index)
fsMonitoring.startWatchingRoot(it.toString())
}
indexCache.onReindexFinish()
tagsCache.onReindexFinish()
}

suspend fun index(root: Path) = withContext(Dispatchers.Default) {
val index = resourcesIndexFactory.buildFromFilesystem(root)
indexCache.onIndexChange(root, index)
tagsCache.onIndexChanged(root, index)
fsMonitoring.startWatchingRoot(root.toString())
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/space/taran/arknavigator/mvp/model/RootAndFav.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package space.taran.arknavigator.mvp.model

import java.nio.file.Path

data class RootAndFav (
val root: Path?,
val fav: Path?
) {
init {
if (root == null && fav != null)
throw AssertionError("Combination null root and not null fav isn't allowed")
}

fun isAllRoots(): Boolean {
return root == null
}
}
107 changes: 107 additions & 0 deletions app/src/main/java/space/taran/arknavigator/mvp/model/TagsCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package space.taran.arknavigator.mvp.model

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import space.taran.arknavigator.mvp.model.dao.ResourceId
import space.taran.arknavigator.mvp.model.repo.AggregatedTagsStorage
import space.taran.arknavigator.mvp.model.repo.PlainResourcesIndex
import space.taran.arknavigator.mvp.model.repo.PlainTagsStorage
import space.taran.arknavigator.utils.Tags
import java.nio.file.Path

class TagsCache(val indexCache: IndexCache) {
private var storageByRoot = mutableMapOf<Path, PlainTagsStorage>()
private var aggregatedTagsStorage = AggregatedTagsStorage(listOf())
private var flowByRootAndFav = mutableMapOf<RootAndFav, MutableStateFlow<Tags?>>()
private var allRootsFlow = MutableStateFlow<Tags?>(null)

suspend fun onIndexChanged(root: Path, index: PlainResourcesIndex) {
val storage = PlainTagsStorage.provide(root, index)
storageByRoot[root] = storage
val allIds = index.listAllIds()
storage.cleanup(allIds)
emitChangesToAffectedRootAndFav(root, storage, index)
}

suspend fun onReindexFinish() {
allRootsFlow.emit(aggregatedTagsStorage.getTags(indexCache.listIds(RootAndFav(null, null))))
}

fun listenTagsChanges(rootAndFav: RootAndFav): StateFlow<Tags?> {
return if (rootAndFav.isAllRoots()) {
allRootsFlow
} else {
if (flowByRootAndFav[rootAndFav] == null) {
storageByRoot[rootAndFav.root]?.let { storage ->
val ids = indexCache.listIds(rootAndFav)
flowByRootAndFav[rootAndFav] =
MutableStateFlow(storage.getTags(ids))
} ?: run {
flowByRootAndFav[rootAndFav] = MutableStateFlow(null)
}
}

flowByRootAndFav[rootAndFav]!!
}
}

fun listUntagged(rootAndFav: RootAndFav): Set<ResourceId>? {
val underPrefix = indexCache.listIds(rootAndFav)
return if (rootAndFav.isAllRoots())
aggregatedTagsStorage.listUntaggedResources().intersect(underPrefix)
else
storageByRoot[rootAndFav.root]?.listUntaggedResources()?.intersect(underPrefix)
}

fun groupTagsByResources(ids: Iterable<ResourceId>): Map<ResourceId, Tags> =
ids.map {it to getTags(it)}.toMap()

fun getTags(id: ResourceId): Tags {
return aggregatedTagsStorage.getTags(id)
}

fun getTags(ids: Iterable<ResourceId>): Tags {
return aggregatedTagsStorage.getTags(ids)
}

fun getTags(rootAndFav: RootAndFav): Tags {
return if (rootAndFav.isAllRoots()) {
aggregatedTagsStorage.getTags(indexCache.listIds(rootAndFav))
} else {
storageByRoot[rootAndFav.root]?.getTags(indexCache.listIds(rootAndFav)) ?: emptySet()
}
}

suspend fun remove(resourceId: ResourceId) {
aggregatedTagsStorage.remove(resourceId)
}

suspend fun setTags(rootAndFav: RootAndFav, id: ResourceId, tags: Tags) {
if (rootAndFav.isAllRoots()) {
aggregatedTagsStorage.setTags(id, tags)
emitChangesToAllRootsFlow()
} else {
val root = rootAndFav.root!!
val storage = storageByRoot[root]!!
storage.setTags(id, tags)
emitChangesToAffectedRootAndFav(root, storage, indexCache.getIndexByRoot(root))
}
}

private suspend fun emitChangesToAllRootsFlow() {
aggregatedTagsStorage = AggregatedTagsStorage(storageByRoot.values)
if (allRootsFlow.value != null)
allRootsFlow.emit(aggregatedTagsStorage.getTags(indexCache.listIds(RootAndFav(null, null))))
}

private suspend fun emitChangesToAffectedRootAndFav(root: Path, storage: PlainTagsStorage, index: PlainResourcesIndex) {
emitChangesToAllRootsFlow()

val affectedRootAndFavs = flowByRootAndFav.keys.filter { it.root == root }
affectedRootAndFavs.forEach {
val tags = storage.getTags(index.listIds(it.fav))
if (flowByRootAndFav[it]!!.value != tags)
flowByRootAndFav[it]!!.emit(tags)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package space.taran.arknavigator.mvp.model.fsmonitoring

import android.os.Build
import android.os.FileObserver
import androidx.annotation.RequiresApi
import java.io.File

class DirectoryObserver: FileObserver {
@RequiresApi(Build.VERSION_CODES.Q)
constructor(directory: File): super(directory)
constructor(directory: String): super(directory)

private lateinit var recursiveObs: RecursiveDirectoryObserver
private lateinit var directory: String

override fun onEvent(event: Int, path: String?) {
val eventCode = event and ALL_EVENTS
val eventPath = path?.let { "$directory/$path" }
recursiveObs.onEvent(directory, eventCode, eventPath)
}

companion object {
fun create(recursiveObs: RecursiveDirectoryObserver, directory: String): DirectoryObserver {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
DirectoryObserver(File(directory)).also {
it.recursiveObs = recursiveObs
it.directory = directory
}
} else {
DirectoryObserver(directory).also {
it.recursiveObs = recursiveObs
it.directory = directory
}
}
}
}
}
Loading