Skip to content

Commit 8b28616

Browse files
BrayanDSOlukstbit
authored andcommitted
refactor: make getServerPort non static
so it uses the provided `prefs` object instead of relying on the `Prefs` object
1 parent 1c8e2c6 commit 8b28616

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ abstract class AbstractFlashcardViewer :
562562

563563
setContentView(getContentViewAttr(fullscreenMode))
564564

565-
val port = StudyScreenRepository.getServerPort()
565+
val port = StudyScreenRepository().getServerPort()
566566
server = AnkiServer(this, port).also { it.start() }
567567
// Make ACTION_PROCESS_TEXT for in-app searching possible on > Android 4.0
568568
delegate.isHandleNativeActionModesEnabled = true

AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ReviewerViewModel(
116116
val pageDownFlow = MutableSharedFlow<Unit>()
117117
val statesMutationEvalFlow = MutableSharedFlow<String>()
118118

119-
override val server: AnkiServer = AnkiServer(this, StudyScreenRepository.getServerPort()).also { it.start() }
119+
override val server: AnkiServer = AnkiServer(this, StudyScreenRepository().getServerPort()).also { it.start() }
120120
private val stateMutationKey = TimeManager.time.intTimeMS().toString()
121121
private var typedAnswer = ""
122122

AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/StudyScreenRepository.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.net.BindException
2626
import java.net.ServerSocket
2727

2828
class StudyScreenRepository(
29-
prefs: PrefsRepository = Prefs,
29+
private val prefs: PrefsRepository = Prefs,
3030
) {
3131
val isMarkShownInToolbar: Boolean
3232
val isFlagShownInToolbar: Boolean
@@ -42,23 +42,21 @@ class StudyScreenRepository(
4242
isFlagShownInToolbar = isToolbarShown && ViewerAction.FLAG_MENU in actions
4343
}
4444

45-
companion object {
46-
fun getServerPort(): Int {
47-
if (!Prefs.useFixedPortInReviewer) return 0
48-
return try {
49-
ServerSocket(Prefs.reviewerPort)
50-
.use {
51-
it.reuseAddress = true
52-
it.localPort
53-
}.also {
54-
if (Prefs.reviewerPort == 0) {
55-
Prefs.reviewerPort = it
56-
}
45+
fun getServerPort(): Int {
46+
if (!prefs.useFixedPortInReviewer) return 0
47+
return try {
48+
ServerSocket(prefs.reviewerPort)
49+
.use {
50+
it.reuseAddress = true
51+
it.localPort
52+
}.also {
53+
if (prefs.reviewerPort == 0) {
54+
prefs.reviewerPort = it
5755
}
58-
} catch (_: BindException) {
59-
Timber.w("Fixed port %d under use. Using dynamic port", Prefs.reviewerPort)
60-
0
61-
}
56+
}
57+
} catch (_: BindException) {
58+
Timber.w("Fixed port %d under use. Using dynamic port", prefs.reviewerPort)
59+
0
6260
}
6361
}
6462
}

0 commit comments

Comments
 (0)