Skip to content

Commit 8b84df4

Browse files
committed
fix: sync floating window progress via SharedPreferences listener
悬浮窗添加 SharedPreferences 监听器,Activity 侧点击列表/搜索跳转时自动同步进度, 移除 restartFloatingService 调用
1 parent 921e6fb commit 8b84df4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

app/src/main/java/com/example/epubspoon/service/FloatingService.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.content.ClipData
1010
import android.content.ClipboardManager
1111
import android.content.Context
1212
import android.content.Intent
13+
import android.content.SharedPreferences
1314
import android.graphics.PixelFormat
1415
import android.os.Handler
1516
import android.os.IBinder
@@ -38,6 +39,19 @@ class FloatingService : Service() {
3839
private var segments: List<String> = emptyList()
3940
private var currentIndex: Int = 0
4041

42+
/**
43+
* 监听 SharedPreferences 变化,Activity 侧修改进度时实时同步到悬浮窗
44+
*/
45+
private val prefsListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
46+
if (key != null && md5.isNotBlank() && key == "progress_$md5") {
47+
val newIndex = storage.loadProgress(md5)
48+
if (newIndex != currentIndex && newIndex in segments.indices) {
49+
currentIndex = newIndex
50+
updateProgressText()
51+
}
52+
}
53+
}
54+
4155
companion object {
4256
private const val CHANNEL_ID = "epubspoon_floating"
4357
private const val NOTIFICATION_ID = 1
@@ -99,6 +113,9 @@ class FloatingService : Service() {
99113
setupFloatingView()
100114
Log.d("EpubSpoon", "FloatingView added to WindowManager")
101115

116+
// 监听进度变化(Activity 侧点击列表/搜索跳转时同步)
117+
storage.registerChangeListener(prefsListener)
118+
102119
return START_NOT_STICKY
103120
}
104121

@@ -222,6 +239,7 @@ class FloatingService : Service() {
222239

223240
override fun onDestroy() {
224241
super.onDestroy()
242+
storage.unregisterChangeListener(prefsListener)
225243
if (::floatingView.isInitialized) {
226244
try { windowManager.removeView(floatingView) } catch (_: Exception) {}
227245
}

app/src/main/java/com/example/epubspoon/ui/MainActivity.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ Keep this format consistent for every passage I send. No need to confirm or repe
160160
private fun setupRecyclerView() {
161161
segmentAdapter = SegmentAdapter { index ->
162162
viewModel.selectSegment(index)
163-
// 如果悬浮窗正在运行,重启以同步进度
164-
if (floatingServiceRunning) {
165-
restartFloatingService()
166-
}
167163
}
168164
binding.rvSegments.apply {
169165
layoutManager = LinearLayoutManager(this@MainActivity)
@@ -184,7 +180,6 @@ Keep this format consistent for every passage I send. No need to confirm or repe
184180
val targetIndex = (asNumber - 1).coerceIn(0, state.bookData.segments.size - 1)
185181
viewModel.selectSegment(targetIndex)
186182
binding.rvSegments.scrollToPosition(targetIndex)
187-
if (floatingServiceRunning) restartFloatingService()
188183
Toast.makeText(this, "已跳转到第 ${targetIndex + 1}", Toast.LENGTH_SHORT).show()
189184
return
190185
}
@@ -197,7 +192,6 @@ Keep this format consistent for every passage I send. No need to confirm or repe
197192
if (segments[idx].contains(query, ignoreCase = true)) {
198193
viewModel.selectSegment(idx)
199194
binding.rvSegments.scrollToPosition(idx)
200-
if (floatingServiceRunning) restartFloatingService()
201195
Toast.makeText(this, "找到:第 ${idx + 1}", Toast.LENGTH_SHORT).show()
202196
return
203197
}

0 commit comments

Comments
 (0)