Rename Model to State

This commit is contained in:
Marvin Elsen 2024-11-14 20:01:19 +01:00
parent 9096941d58
commit 46ddb69a52
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
7 changed files with 70 additions and 70 deletions

View File

@ -19,7 +19,7 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
class Model( class State(
private val dictionary: SqliteDictionary, private val dictionary: SqliteDictionary,
private val undoManager: UndoManager, private val undoManager: UndoManager,
) { ) {

View File

@ -43,7 +43,7 @@ class WillowApplication : Application() {
} }
val dictionary = SqliteDictionary(connection) val dictionary = SqliteDictionary(connection)
val undoManager = UndoManager() val undoManager = UndoManager()
val model = Model( val state = State(
dictionary, dictionary,
undoManager undoManager
) )
@ -56,11 +56,11 @@ class WillowApplication : Application() {
fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", config.locale.value) fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", config.locale.value)
fxmlLoader.controllerFactory = Callback { type -> fxmlLoader.controllerFactory = Callback { type ->
when (type) { when (type) {
MainController::class.java -> MainController(model) MainController::class.java -> MainController(state)
MenuController::class.java -> MenuController(model, config) MenuController::class.java -> MenuController(state, config)
DetailsController::class.java -> DetailsController(model, config, hostServices) DetailsController::class.java -> DetailsController(state, config, hostServices)
SearchController::class.java -> SearchController(model) SearchController::class.java -> SearchController(state)
SearchResultsController::class.java -> SearchResultsController(model, config, hostServices) SearchResultsController::class.java -> SearchResultsController(state, config, hostServices)
else -> error("Trying to instantiate unknown controller type $type") else -> error("Trying to instantiate unknown controller type $type")
} }
} }

View File

@ -1,6 +1,6 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.State
import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Pronunciation import com.marvinelsen.willow.config.Pronunciation
import com.marvinelsen.willow.config.Script import com.marvinelsen.willow.config.Script
@ -33,7 +33,7 @@ import java.util.ResourceBundle
@Suppress("UnusedPrivateMember", "TooManyFunctions") @Suppress("UnusedPrivateMember", "TooManyFunctions")
class DetailsController( class DetailsController(
private val model: Model, private val state: State,
private val config: Config, private val config: Config,
private val hostServices: HostServices, private val hostServices: HostServices,
) { ) {
@ -112,14 +112,14 @@ class DetailsController(
textProperty().bind( textProperty().bind(
Bindings.createStringBinding( Bindings.createStringBinding(
{ {
val selectedEntry = model.selectedEntry.value val selectedEntry = state.selectedEntry.value
when (config.script.value!!) { when (config.script.value!!) {
Script.SIMPLIFIED -> selectedEntry?.simplified Script.SIMPLIFIED -> selectedEntry?.simplified
Script.TRADITIONAL -> selectedEntry?.traditional Script.TRADITIONAL -> selectedEntry?.traditional
} }
}, },
config.script, config.script,
model.selectedEntry state.selectedEntry
) )
) )
styleProperty().bind( styleProperty().bind(
@ -137,7 +137,7 @@ class DetailsController(
textProperty().bind( textProperty().bind(
Bindings.createStringBinding( Bindings.createStringBinding(
{ {
val selectedEntry = model.selectedEntry.value val selectedEntry = state.selectedEntry.value
when (config.details.pronunciation.value!!) { when (config.details.pronunciation.value!!) {
Pronunciation.PINYIN_WITH_TONE_MARKS -> selectedEntry?.pinyinWithToneMarks Pronunciation.PINYIN_WITH_TONE_MARKS -> selectedEntry?.pinyinWithToneMarks
Pronunciation.PINYIN_WITH_TONE_NUMBERS -> selectedEntry?.pinyinWithToneNumbers Pronunciation.PINYIN_WITH_TONE_NUMBERS -> selectedEntry?.pinyinWithToneNumbers
@ -145,7 +145,7 @@ class DetailsController(
} }
}, },
config.details.pronunciation, config.details.pronunciation,
model.selectedEntry state.selectedEntry
) )
) )
styleProperty().bind( styleProperty().bind(
@ -160,16 +160,16 @@ class DetailsController(
private fun initializeTabPaneDetails() { private fun initializeTabPaneDetails() {
tabPaneDetails.apply { tabPaneDetails.apply {
disableProperty().bind(Bindings.isNull(model.selectedEntry)) disableProperty().bind(Bindings.isNull(state.selectedEntry))
selectionModel.selectedItemProperty().addListener { _, _, selectedTab -> selectionModel.selectedItemProperty().addListener { _, _, selectedTab ->
if (model.selectedEntry.value == null) return@addListener if (state.selectedEntry.value == null) return@addListener
lazyUpdateTabContent(selectedTab.id) lazyUpdateTabContent(selectedTab.id)
} }
} }
model.selectedEntry.addListener { _, _, newEntry -> state.selectedEntry.addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
tabPaneDetails.selectionModel.select(0) tabPaneDetails.selectionModel.select(0)
@ -178,9 +178,9 @@ class DetailsController(
tabCharacters.disableProperty().bind( tabCharacters.disableProperty().bind(
Bindings.createBooleanBinding( Bindings.createBooleanBinding(
{ {
(model.selectedEntry.value?.traditional?.length ?: 0) < 2 (state.selectedEntry.value?.traditional?.length ?: 0) < 2
}, },
model.selectedEntry state.selectedEntry
) )
) )
} }
@ -188,68 +188,68 @@ class DetailsController(
private fun initializeListViewSentences() { private fun initializeListViewSentences() {
listViewSentences.apply { listViewSentences.apply {
cellFactory = SentenceCellFactory(resources, config, hostServices) cellFactory = SentenceCellFactory(resources, config, hostServices)
items = model.sentences items = state.sentences
disableProperty().bind(Bindings.or(model.isFindingSentences, Bindings.isEmpty(model.sentences))) disableProperty().bind(Bindings.or(state.isFindingSentences, Bindings.isEmpty(state.sentences)))
} }
progressIndicatorSentences.visibleProperty().bind(model.isFindingSentences) progressIndicatorSentences.visibleProperty().bind(state.isFindingSentences)
labelNoSentencesFound labelNoSentencesFound
.visibleProperty() .visibleProperty()
.bind(Bindings.and(Bindings.isEmpty(model.sentences), Bindings.not(model.isFindingSentences))) .bind(Bindings.and(Bindings.isEmpty(state.sentences), Bindings.not(state.isFindingSentences)))
} }
private fun initializeListViewWordsContaining() { private fun initializeListViewWordsContaining() {
listViewWordsContaining.apply { listViewWordsContaining.apply {
cellFactory = DictionaryEntryCellFactory(resources, config, hostServices) cellFactory = DictionaryEntryCellFactory(resources, config, hostServices)
items = model.wordsContaining items = state.wordsContaining
disableProperty().bind(Bindings.or(model.isFindingWordsContaining, Bindings.isEmpty(model.wordsContaining))) disableProperty().bind(Bindings.or(state.isFindingWordsContaining, Bindings.isEmpty(state.wordsContaining)))
selectionModel.selectedItemProperty().addListener { _, _, newEntry -> selectionModel.selectedItemProperty().addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
model.deepDive(newEntry) state.deepDive(newEntry)
} }
} }
progressIndicatorWordsContaining.visibleProperty().bind(model.isFindingWordsContaining) progressIndicatorWordsContaining.visibleProperty().bind(state.isFindingWordsContaining)
labelNoWordsContainingFound labelNoWordsContainingFound
.visibleProperty() .visibleProperty()
.bind(Bindings.and(Bindings.isEmpty(model.wordsContaining), Bindings.not(model.isFindingWordsContaining))) .bind(Bindings.and(Bindings.isEmpty(state.wordsContaining), Bindings.not(state.isFindingWordsContaining)))
} }
private fun initializeListViewWordsBeginning() { private fun initializeListViewWordsBeginning() {
listViewWordsBeginning.apply { listViewWordsBeginning.apply {
cellFactory = DictionaryEntryCellFactory(resources, config, hostServices) cellFactory = DictionaryEntryCellFactory(resources, config, hostServices)
items = model.wordsBeginning items = state.wordsBeginning
disableProperty().bind(Bindings.or(model.isFindingWordsBeginning, Bindings.isEmpty(model.wordsBeginning))) disableProperty().bind(Bindings.or(state.isFindingWordsBeginning, Bindings.isEmpty(state.wordsBeginning)))
selectionModel.selectedItemProperty().addListener { _, _, newEntry -> selectionModel.selectedItemProperty().addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
model.deepDive(newEntry) state.deepDive(newEntry)
} }
} }
progressIndicatorWordsBeginning.visibleProperty().bind(model.isFindingWordsBeginning) progressIndicatorWordsBeginning.visibleProperty().bind(state.isFindingWordsBeginning)
labelNoWordsBeginningFound labelNoWordsBeginningFound
.visibleProperty() .visibleProperty()
.bind(Bindings.and(Bindings.isEmpty(model.wordsBeginning), Bindings.not(model.isFindingWordsBeginning))) .bind(Bindings.and(Bindings.isEmpty(state.wordsBeginning), Bindings.not(state.isFindingWordsBeginning)))
} }
private fun initializeListViewCharacters() { private fun initializeListViewCharacters() {
listViewCharacters.apply { listViewCharacters.apply {
cellFactory = DictionaryEntryCellFactory(resources, config, hostServices) cellFactory = DictionaryEntryCellFactory(resources, config, hostServices)
items = model.characters items = state.characters
disableProperty().bind(Bindings.or(model.isFindingCharacters, Bindings.isEmpty(model.characters))) disableProperty().bind(Bindings.or(state.isFindingCharacters, Bindings.isEmpty(state.characters)))
selectionModel.selectedItemProperty().addListener { _, _, newEntry -> selectionModel.selectedItemProperty().addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
model.deepDive(newEntry) state.deepDive(newEntry)
} }
} }
progressIndicatorCharacters.visibleProperty().bind(model.isFindingCharacters) progressIndicatorCharacters.visibleProperty().bind(state.isFindingCharacters)
labelNoCharactersFound labelNoCharactersFound
.visibleProperty() .visibleProperty()
.bind(Bindings.and(Bindings.isEmpty(model.characters), Bindings.not(model.isFindingCharacters))) .bind(Bindings.and(Bindings.isEmpty(state.characters), Bindings.not(state.isFindingCharacters)))
} }
private fun initializeWebViewDefinition() { private fun initializeWebViewDefinition() {
@ -257,7 +257,7 @@ class DetailsController(
engine.userStyleSheetLocation = this::class.java.getResource("/css/definitions.css")!!.toExternalForm() engine.userStyleSheetLocation = this::class.java.getResource("/css/definitions.css")!!.toExternalForm()
} }
model.selectedEntry.addListener { _, _, newEntry -> state.selectedEntry.addListener { _, _, newEntry ->
if (newEntry == null) return@addListener if (newEntry == null) return@addListener
webViewDefinition.engine.loadContent(createDefinitionHtml(newEntry)) webViewDefinition.engine.loadContent(createDefinitionHtml(newEntry))
@ -266,9 +266,9 @@ class DetailsController(
@FXML @FXML
private fun headerOnContextMenuRequested(contextMenuEvent: ContextMenuEvent) { private fun headerOnContextMenuRequested(contextMenuEvent: ContextMenuEvent) {
if (model.selectedEntry.value == null) return if (state.selectedEntry.value == null) return
createContextMenuForEntry(model.selectedEntry.value, resources, hostServices).show( createContextMenuForEntry(state.selectedEntry.value, resources, hostServices).show(
flowPaneHeader.scene.window, flowPaneHeader.scene.window,
contextMenuEvent.screenX, contextMenuEvent.screenX,
contextMenuEvent.screenY contextMenuEvent.screenY
@ -279,24 +279,24 @@ class DetailsController(
private fun lazyUpdateTabContent(selectedTabId: String?) { private fun lazyUpdateTabContent(selectedTabId: String?) {
when (selectedTabId) { when (selectedTabId) {
"tabWords" -> { "tabWords" -> {
if (!model.finishedFindingWordsContaining.value) { if (!state.finishedFindingWordsContaining.value) {
model.findWordsContaining() state.findWordsContaining()
} }
if (!model.finishedFindingWordsBeginning.value) { if (!state.finishedFindingWordsBeginning.value) {
model.findWordsBeginning() state.findWordsBeginning()
} }
} }
"tabCharacters" -> { "tabCharacters" -> {
if (model.finishedFindingCharacters.value) return if (state.finishedFindingCharacters.value) return
model.findCharacters() state.findCharacters()
} }
"tabSentences" -> { "tabSentences" -> {
if (model.finishedFindingSentences.value) return if (state.finishedFindingSentences.value) return
model.findSentences() state.findSentences()
} }
else -> {} else -> {}

View File

@ -1,10 +1,10 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.State
import javafx.fxml.FXML import javafx.fxml.FXML
@Suppress("UnusedPrivateProperty", "UnusedPrivateMember") @Suppress("UnusedPrivateProperty", "UnusedPrivateMember")
class MainController(private val model: Model) { class MainController(private val state: State) {
@FXML @FXML
private fun initialize() { private fun initialize() {
// no-op // no-op

View File

@ -1,6 +1,6 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.State
import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.ui.dialogs.PreferencesDialog import com.marvinelsen.willow.ui.dialogs.PreferencesDialog
import javafx.application.Platform import javafx.application.Platform
@ -11,7 +11,7 @@ import javafx.scene.control.MenuItem
import java.util.ResourceBundle import java.util.ResourceBundle
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
class MenuController(private val model: Model, private val config: Config) { class MenuController(private val state: State, private val config: Config) {
@FXML @FXML
private lateinit var resources: ResourceBundle private lateinit var resources: ResourceBundle
@ -26,8 +26,8 @@ class MenuController(private val model: Model, private val config: Config) {
@FXML @FXML
private fun initialize() { private fun initialize() {
menuItemCopyPronunciation.disableProperty().bind(Bindings.isNull(model.selectedEntry)) menuItemCopyPronunciation.disableProperty().bind(Bindings.isNull(state.selectedEntry))
menuItemCopyHeadword.disableProperty().bind(Bindings.isNull(model.selectedEntry)) menuItemCopyHeadword.disableProperty().bind(Bindings.isNull(state.selectedEntry))
} }
@FXML @FXML
@ -47,11 +47,11 @@ class MenuController(private val model: Model, private val config: Config) {
@FXML @FXML
private fun onMenuItemCopyPronunciationAction() { private fun onMenuItemCopyPronunciationAction() {
model.copyPronunciationOfSelectedEntry() state.copyPronunciationOfSelectedEntry()
} }
@FXML @FXML
private fun onMenuItemCopyHeadwordAction() { private fun onMenuItemCopyHeadwordAction() {
model.copyHeadwordOfSelectedEntry() state.copyHeadwordOfSelectedEntry()
} }
} }

View File

@ -1,13 +1,13 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.State
import com.marvinelsen.willow.domain.SearchMode import com.marvinelsen.willow.domain.SearchMode
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.scene.control.Button import javafx.scene.control.Button
import javafx.scene.control.TextField import javafx.scene.control.TextField
import javafx.scene.control.ToggleGroup import javafx.scene.control.ToggleGroup
class SearchController(private val model: Model) { class SearchController(private val state: State) {
@FXML @FXML
private lateinit var buttonUndo: Button private lateinit var buttonUndo: Button
@ -26,8 +26,8 @@ class SearchController(private val model: Model) {
textFieldSearch.textProperty().addListener { _, _, _ -> search() } textFieldSearch.textProperty().addListener { _, _, _ -> search() }
searchModeToggleGroup.selectedToggleProperty().addListener { _, _, _ -> search() } searchModeToggleGroup.selectedToggleProperty().addListener { _, _, _ -> search() }
buttonUndo.disableProperty().bind(model.canUndo.not()) buttonUndo.disableProperty().bind(state.canUndo.not())
buttonRedo.disableProperty().bind(model.canRedo.not()) buttonRedo.disableProperty().bind(state.canRedo.not())
} }
private fun search() { private fun search() {
@ -38,14 +38,14 @@ class SearchController(private val model: Model) {
return return
} }
model.search(searchQuery, searchMode) state.search(searchQuery, searchMode)
} }
fun onButtonRedoAction() { fun onButtonRedoAction() {
model.redoSelection() state.redoSelection()
} }
fun onButtonUndoAction() { fun onButtonUndoAction() {
model.undoSelection() state.undoSelection()
} }
} }

View File

@ -1,6 +1,6 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.State
import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.domain.entities.DictionaryEntry import com.marvinelsen.willow.domain.entities.DictionaryEntry
import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory
@ -13,7 +13,7 @@ import javafx.scene.control.ProgressIndicator
import java.util.ResourceBundle import java.util.ResourceBundle
class SearchResultsController( class SearchResultsController(
private val model: Model, private val state: State,
private val config: Config, private val config: Config,
private val hostServices: HostServices, private val hostServices: HostServices,
) { ) {
@ -35,21 +35,21 @@ class SearchResultsController(
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
listViewSearchResults.cellFactory = DictionaryEntryCellFactory(resources, config, hostServices) listViewSearchResults.cellFactory = DictionaryEntryCellFactory(resources, config, hostServices)
listViewSearchResults.items = model.searchResults listViewSearchResults.items = state.searchResults
listViewSearchResults listViewSearchResults
.disableProperty() .disableProperty()
.bind(Bindings.or(model.isSearching, Bindings.isEmpty(model.searchResults))) .bind(Bindings.or(state.isSearching, Bindings.isEmpty(state.searchResults)))
progressIndicatorEntries.visibleProperty().bind(model.isSearching) progressIndicatorEntries.visibleProperty().bind(state.isSearching)
labelNoEntriesFound labelNoEntriesFound
.visibleProperty() .visibleProperty()
.bind(Bindings.and(Bindings.isEmpty(model.searchResults), Bindings.not(model.isSearching))) .bind(Bindings.and(Bindings.isEmpty(state.searchResults), Bindings.not(state.isSearching)))
listViewSearchResults.selectionModel.selectedItemProperty().addListener { _, _, newValue: DictionaryEntry? -> listViewSearchResults.selectionModel.selectedItemProperty().addListener { _, _, newValue: DictionaryEntry? ->
if (newValue == null) { if (newValue == null) {
return@addListener return@addListener
} }
model.normalSelect(newValue) state.normalSelect(newValue)
} }
} }
} }