mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-05-12 05:51:28 +00:00
fix: file watcher disposable by making it project-level service
This commit is contained in:
parent
63f139dd74
commit
92d9d5ee20
2 changed files with 15 additions and 17 deletions
|
|
@ -3,9 +3,9 @@ package ee.carlrobert.codegpt
|
|||
import com.intellij.notification.NotificationAction
|
||||
import com.intellij.notification.NotificationType
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.startup.ProjectActivity
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil
|
||||
import ee.carlrobert.codegpt.completions.you.YouUserManager
|
||||
import ee.carlrobert.codegpt.completions.you.auth.AuthenticationHandler
|
||||
|
|
@ -34,14 +34,12 @@ class CodeGPTProjectActivity : ProjectActivity {
|
|||
if (!ApplicationManager.getApplication().isUnitTestMode
|
||||
&& ConfigurationSettings.getCurrentState().isCheckForNewScreenshots
|
||||
) {
|
||||
val pathToWatch = Paths.get(System.getProperty("user.home"), "Desktop")
|
||||
val fileWatcher = FileWatcher(pathToWatch)
|
||||
fileWatcher.watch {
|
||||
if (listOf("jpg", "jpeg", "png").contains(it.extension)) {
|
||||
showImageAttachmentNotification(project, it.absolutePath)
|
||||
project.service<FileWatcher>()
|
||||
.watch(Paths.get(System.getProperty("user.home"), "Desktop").toFile()) {
|
||||
if (listOf("jpg", "jpeg", "png").contains(it.extension)) {
|
||||
showImageAttachmentNotification(project, it.absolutePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
Disposer.register(project, fileWatcher)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +1,29 @@
|
|||
package ee.carlrobert.codegpt
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.components.Service
|
||||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor
|
||||
import org.apache.commons.io.monitor.FileAlterationMonitor
|
||||
import org.apache.commons.io.monitor.FileAlterationObserver
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
class FileWatcher(private val pathToWatch: Path) : Disposable {
|
||||
@Service(Service.Level.PROJECT)
|
||||
class FileWatcher : Disposable {
|
||||
|
||||
private val fileMonitor =
|
||||
FileAlterationMonitor(500, FileAlterationObserver(pathToWatch.toFile()))
|
||||
private var fileMonitor: FileAlterationMonitor? = null
|
||||
|
||||
fun watch(onFileCreated: (File) -> Unit) {
|
||||
val observer = FileAlterationObserver(pathToWatch.toFile())
|
||||
fun watch(pathToWatch: File, onFileCreated: (File) -> Unit) {
|
||||
val observer = FileAlterationObserver(pathToWatch)
|
||||
observer.addListener(object : FileAlterationListenerAdaptor() {
|
||||
override fun onFileCreate(file: File) {
|
||||
onFileCreated(file)
|
||||
}
|
||||
})
|
||||
fileMonitor.addObserver(observer)
|
||||
fileMonitor.start()
|
||||
fileMonitor = FileAlterationMonitor(500, observer)
|
||||
fileMonitor?.start()
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
fileMonitor.stop()
|
||||
fileMonitor?.stop()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue