From 2d1984c49200facc4806c85edecd42b0ac89bd97 Mon Sep 17 00:00:00 2001 From: Om Godse Date: Tue, 21 Jul 2020 11:40:12 +0530 Subject: [PATCH] Replace constructor with static factory method --- .../notally/viewmodels/BaseNoteModel.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt b/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt index 29dbd949..45ae9f67 100644 --- a/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt +++ b/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt @@ -35,16 +35,9 @@ class BaseNoteModel(app: Application) : AndroidViewModel(app) { private val archivedObserver: NotallyFileObserver init { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - observer = NotallyFileObserver(notesHelper.getNotePath()) - deletedObserver = NotallyFileObserver(notesHelper.getDeletedPath()) - archivedObserver = NotallyFileObserver(notesHelper.getArchivedPath()) - } - else { - observer = NotallyFileObserver(notesHelper.getNotePath().path) - deletedObserver = NotallyFileObserver(notesHelper.getDeletedPath().path) - archivedObserver = NotallyFileObserver(notesHelper.getArchivedPath().path) - } + observer = NotallyFileObserver.getInstance(notesHelper.getNotePath()) + deletedObserver = NotallyFileObserver.getInstance(notesHelper.getDeletedPath()) + archivedObserver = NotallyFileObserver.getInstance(notesHelper.getArchivedPath()) observer.onEventCallback = { event, path -> onEvent(event, path, notes, notesHelper.getNotePath()) @@ -272,9 +265,9 @@ class BaseNoteModel(app: Application) : AndroidViewModel(app) { var onEventCallback: ((event: Int, path: String) -> Unit)? = null @RequiresApi(Build.VERSION_CODES.Q) - constructor(file: File) : super(file, mask) + private constructor(file: File) : super(file, mask) - constructor(filePath: String) : super(filePath, mask) + private constructor(filePath: String) : super(filePath, mask) override fun onEvent(event: Int, path: String?) { /* @@ -291,6 +284,12 @@ class BaseNoteModel(app: Application) : AndroidViewModel(app) { companion object { const val mask = DELETE or CREATE or MODIFY + + fun getInstance(file: File) : NotallyFileObserver { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + NotallyFileObserver(file) + } else NotallyFileObserver(file.path) + } } } } \ No newline at end of file