Skip to content

Commit

Permalink
Merge branch 're/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
xyoye committed Mar 9, 2024
2 parents 4972c07 + 320ed5e commit 388b593
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class BindExtraSourceActivity :
VideoItemLayout.initVideoLayout(dataBinding, it)
}

viewModel.searchTextFlow.collectAtStarted(this) {
dataBinding.searchEt.setText(it)
}

viewModel.segmentTitleLiveData.observe(this) {
SegmentWordDialog(this, it) { searchText ->
dataBinding.searchEt.setText(searchText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.LinkedList


/**
Expand All @@ -26,10 +27,14 @@ import org.json.JSONObject
class BindExtraSourceViewModel : BaseViewModel() {

companion object {
private const val MAX_CACHE_SIZE = 50
private const val MAX_SEGMENT_CACHE_SIZE = 50
private const val MAX_SEARCH_TEXT_CACHE_SIZE = 25

// 分词结果缓存
private val segmentCache = LruCache<String, List<String>>(MAX_CACHE_SIZE)
private val segmentCache = LruCache<String, List<String>>(MAX_SEGMENT_CACHE_SIZE)

// 搜索记录缓存,格式为 <file_directory, file_name, searched_text>。
private val searchTextCache = LinkedList<Triple<String, String, String>>()
}

private lateinit var storageFile: StorageFile
Expand All @@ -43,20 +48,27 @@ class BindExtraSourceViewModel : BaseViewModel() {
}.stateIn(viewModelScope, SharingStarted.Lazily, storageFile)
}

private val _searchTextFlow = MutableSharedFlow<String>()
private val _searchTextFlow = MutableSharedFlow<String>(1)
val searchTextFlow = _searchTextFlow.collectable

private val _segmentTitleLiveData = MediatorLiveData<List<String>>()
val segmentTitleLiveData: LiveData<List<String>> = _segmentTitleLiveData

fun setStorageFile(storageFile: StorageFile) {
this.storageFile = storageFile
val cachedSearchText: String? = matchSearchTextCache(storageFile)
if (cachedSearchText != null) {
viewModelScope.launch {
_searchTextFlow.emit(cachedSearchText)
}
}
}

fun setSearchText(text: String) {
viewModelScope.launch {
_searchTextFlow.emit(text)
}
addSearchTextCache(storageFile, text)
}

fun segmentTitle(storageFile: StorageFile) {
Expand Down Expand Up @@ -110,4 +122,72 @@ class BindExtraSourceViewModel : BaseViewModel() {
}
return words
}

private fun matchSearchTextCache(target: StorageFile): String? {
for (cachedTriple in searchTextCache) {
val cachedFileDir: String = cachedTriple.first
val cachedFileName: String = cachedTriple.second
val cachedText: String = cachedTriple.third
val targetDir = parseFileDir(target.filePath())
val targetName = target.fileName()
if (!target.isFile()) {
continue
}
// 比较所在目录。
if (targetDir != cachedFileDir) {
continue
}
// 比较文件名。
var ptr = 0
val diff = LinkedList<Int>()
while (ptr < targetName.length || ptr < cachedFileName.length) {
val a = if (ptr < targetName.length) targetName[ptr] else '*'
val b = if (ptr < cachedFileName.length) cachedFileName[ptr] else '*'
if (a != b) {
diff.addLast(ptr)
}
ptr++
}
if (diff.size == 0) {
// 无差异。
return cachedText
}
if (diff.size > 2) {
// 差异过多。
continue
}
if (diff.any { !targetName[it].isDigit() || !cachedFileName[it].isDigit() }) {
// 差异包含数字以外的内容。
continue
}
if (diff.size == 2 && (diff[1] - diff[0] != 1)) {
// 多段不相邻差异。
continue
}
// 命中缓存。
if (cachedText.matches(Regex(".* \\d{1,2}$"))) {
// 缓存搜索结果末尾包含集数,删除集数内容。
// 不作替换处理是避免错误处理文件名中类似"11"、"12"这样的差异。
return cachedText.replace(Regex(" \\d{1,2}$"), "")
}
return cachedText
}
return null
}

private fun addSearchTextCache(file: StorageFile, text: String) {
val dir = parseFileDir(file.filePath())
val name = file.fileName()
searchTextCache.addFirst(Triple(dir, name, text))
if (searchTextCache.size > MAX_SEARCH_TEXT_CACHE_SIZE) {
searchTextCache.removeLast()
}
}

private fun parseFileDir(fullPath: String): String {
if (fullPath.contains(Regex("/"))) {
return fullPath.replace(Regex("/[^/]*$"), "")
}
return "/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class BindDanmuSourceFragmentViewModel : BaseViewModel() {
hideLoading()

_searchedAnimeFlow.emit(result)
// 自动选择第一个动画。
if (result.isNotEmpty()) {
_selectedAnimeFlow.emit(result[0])
}
}
}

Expand Down

0 comments on commit 388b593

Please sign in to comment.