Skip to content

Commit df0f7f3

Browse files
committed
fix: make DebugStage optional
1 parent 4c3bca4 commit df0f7f3

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

plugins/plugin-servers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/ServersPlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class ServersPlugin(
3535
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().serversRepository.getDefault()
3636
}
3737

38-
public fun getSelectedStage(): DebugStage {
38+
public fun getSelectedStage(): DebugStage? {
3939
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().stagesRepository.getSelectedStage()
4040
}
4141

42-
public fun getDefaultStage(): DebugStage {
42+
public fun getDefaultStage(): DebugStage? {
4343
return getPlugin<ServersPlugin>().getContainer<ServersPluginContainer>().stagesRepository.getDefault()
4444
}
4545
}

plugins/plugin-servers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/DebugStageRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class DebugStageRepository(
3434
}
3535
}
3636

37-
fun getSelectedStage(): DebugStage {
37+
fun getSelectedStage(): DebugStage? {
3838
val stageName = sharedPreferences.getString(SELECTED_STAGE_NAME, null)
3939
val hostsHash = sharedPreferences.getInt(SELECTED_STAGE_HOSTS_HASH, -1)
4040

@@ -48,8 +48,8 @@ internal class DebugStageRepository(
4848
}
4949
}
5050

51-
fun getDefault(): DebugStage {
52-
return preInstalledStages.first { it.isDefault }
51+
fun getDefault(): DebugStage? {
52+
return preInstalledStages.firstOrNull { it.isDefault }
5353
}
5454

5555
suspend fun getStages(): List<DebugStage> {

plugins/plugin-servers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/model/DebugStage.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ public data class DebugStage(
1717
}
1818

1919
override fun equals(other: Any?): Boolean {
20-
val otherServer = other as DebugStage
21-
return this.hosts.size == otherServer.hosts.size && this.hosts == otherServer.hosts
20+
if (this === other) return true
21+
if (javaClass != other?.javaClass) return false
22+
23+
other as DebugStage
24+
25+
if (name != other.name) return false
26+
if (hosts != other.hosts) return false
27+
if (isDefault != other.isDefault) return false
28+
29+
return true
30+
}
31+
32+
override fun hashCode(): Int {
33+
var result = name.hashCode()
34+
result = 31 * result + hosts.hashCode()
35+
result = 31 * result + isDefault.hashCode()
36+
return result
2237
}
2338
}

plugins/plugin-servers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/interceptor/DebugStageInterceptor.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ public class DebugStageInterceptor(private val hostName: String) : Interceptor {
3333
var request = chain.request()
3434
return if (DebugPanel.isInitialized) {
3535
val debugStage = stageRepository.getSelectedStage()
36-
val host = debugStage.hosts[hostName]
37-
if (host != null) {
38-
val newUrl = request.getNewUrl(host)
39-
request = request.newBuilder()
40-
.url(newUrl)
41-
.build()
42-
}
43-
chain.proceed(requestModifier?.invoke(request, debugStage) ?: request)
36+
val modifiedRequest = debugStage?.let { stage ->
37+
val host = stage.hosts[hostName]
38+
if (host != null) {
39+
val newUrl = request.getNewUrl(host)
40+
request = request.newBuilder()
41+
.url(newUrl)
42+
.build()
43+
}
44+
45+
requestModifier?.invoke(request, stage)
46+
} ?: request
47+
48+
chain.proceed(modifiedRequest)
4449
} else {
4550
chain.proceed(request)
4651
}

0 commit comments

Comments
 (0)