Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
修复: 随机重置内置词库,软件升级后会恢复为原来的顺序 (#94
Browse files Browse the repository at this point in the history
  • Loading branch information
tangshimin committed Oct 25, 2022
1 parent e14e95e commit 56e4d5f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private fun FrameWindowScope.WindowMenuBar(
state.saveGlobalState()
state.loadingFileChooserVisible = false
} else {
state.removeInvalidRecentItem(recentItem)
state.removeRecentItem(recentItem)
JOptionPane.showMessageDialog(null, "文件地址错误:\n${recentItem.path}")
}

Expand Down
69 changes: 61 additions & 8 deletions src/main/kotlin/components/TypingWord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.WindowState
import data.*
import state.MemoryStrategy.*
import dialog.ChapterFinishedDialog
import dialog.ConfirmDialog
import dialog.EditWordDialog
import dialog.SelectChapterDialog
import kotlinx.coroutines.launch
import kotlinx.serialization.ExperimentalSerializationApi
import player.*
Expand All @@ -64,6 +60,13 @@ import javax.swing.JOptionPane
import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.schedule
import androidx.compose.ui.unit.TextUnit
import dialog.*
import state.getResourcesFile
import java.nio.file.Paths
import java.time.LocalDateTime
import javax.swing.JFileChooser
import javax.swing.filechooser.FileSystemView

/**
* 应用程序的核心组件
* @param state 应用程序的状态
Expand Down Expand Up @@ -977,12 +980,62 @@ fun TypingWord(
* @param isShuffle 是否打乱词库
*/
val resetIndex: (isShuffle: Boolean) -> Unit = { isShuffle ->
state.typingWord.index = 0
state.typingWord.chapter = 1
// 如果要打乱顺序
if (isShuffle) {
state.vocabulary.wordList.shuffle()
state.saveCurrentVocabulary()
// 内置词库的地址
val path = getResourcesFile("vocabulary").absolutePath
// 如果要打乱的词库是内置词库,要选择一个地址,保存打乱后的词库,
// 如果不选择地址的话,软件升级后词库会被重置。
if(state.typingWord.vocabularyPath.startsWith(path)){
val fileChooser = state.futureFileChooser.get()
fileChooser.dialogType = JFileChooser.SAVE_DIALOG
fileChooser.dialogTitle = "保存重置后的词库"
val myDocuments = FileSystemView.getFileSystemView().defaultDirectory.path
val fileName = File(state.typingWord.vocabularyPath).nameWithoutExtension
fileChooser.selectedFile = File("$myDocuments${File.separator}$fileName.json")
val userSelection = fileChooser.showSaveDialog(window)
if (userSelection == JFileChooser.APPROVE_OPTION) {
val selectedFile = fileChooser.selectedFile
val vocabularyDirPath = Paths.get(getResourcesFile("vocabulary").absolutePath)
val savePath = Paths.get(selectedFile.absolutePath)
if(savePath.startsWith(vocabularyDirPath)){
JOptionPane.showMessageDialog(null,"不能把词库保存到应用程序安装目录,因为软件更新或卸载时,词库会被重置或者被删除")
}else{
state.vocabulary.wordList.shuffle()
val shuffledList = state.vocabulary.wordList
val vocabulary = Vocabulary(
name = selectedFile.nameWithoutExtension,
type = VocabularyType.DOCUMENT,
language = "english",
size = state.vocabulary.size,
relateVideoPath = state.vocabulary.relateVideoPath,
subtitlesTrackId = state.vocabulary.subtitlesTrackId,
wordList = shuffledList
)

saveVocabulary(vocabulary, selectedFile.absolutePath)
state.changeVocabulary(selectedFile,0)
// changeVocabulary 会把内置词库保存到最近列表,
// 保存后,如果再切换列表,就会有两个名字相同的词库,
// 所以需要把刚刚添加的词库从最近列表删除
for(i in 0 until state.recentList.size){
val recentItem = state.recentList[i]
if(recentItem.name == state.vocabulary.name){
state.removeRecentItem(recentItem)
break
}
}
}
}
}else{
state.vocabulary.wordList.shuffle()
state.saveCurrentVocabulary()
}

}

state.typingWord.index = 0
state.typingWord.chapter = 1
state.saveTypingWordState()
resetChapterTime()
showChapterFinishedDialog = false
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/dialog/GenerateVocabularyDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ fun GenerateVocabularyDialog(
},
recentList = state.recentList,
removeInvalidRecentItem = {
state.removeInvalidRecentItem(it)
state.removeRecentItem(it)
},
familiarVocabulary = familiarVocabulary,
updateFamiliarVocabulary = {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/state/AppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class AppState {

}

fun removeInvalidRecentItem(recentItem: RecentItem) {
fun removeRecentItem(recentItem: RecentItem) {
runBlocking {
launch {
recentList.remove(recentItem)
Expand Down

0 comments on commit 56e4d5f

Please sign in to comment.