Skip to content

Commit 705f186

Browse files
committed
Fix: exclude lastOpened from comparison
- if a connection was re-opened again from the recent connections view then a new entry was added in the tree - instead the last opened timestamp needs to be updated - the fix excludes the timestamp from equals/hashcode/compare and also uses a set instead of a list to keep only unique entries
1 parent 0936a9b commit 705f186

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/main/kotlin/com/coder/gateway/models/RecentWorkspaceConnection.kt

+45-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.coder.gateway.models
33
import com.intellij.openapi.components.BaseState
44
import com.intellij.util.xmlb.annotations.Attribute
55

6-
class RecentWorkspaceConnection() : BaseState() {
6+
class RecentWorkspaceConnection() : BaseState(), Comparable<RecentWorkspaceConnection> {
77
constructor(hostname: String, prjPath: String, openedAt: String, productCode: String, buildNumber: String, source: String) : this() {
88
coderWorkspaceHostname = hostname
99
projectPath = prjPath
@@ -30,4 +30,48 @@ class RecentWorkspaceConnection() : BaseState() {
3030

3131
@get:Attribute
3232
var downloadSource by string()
33+
34+
override fun equals(other: Any?): Boolean {
35+
if (this === other) return true
36+
if (javaClass != other?.javaClass) return false
37+
if (!super.equals(other)) return false
38+
39+
other as RecentWorkspaceConnection
40+
41+
if (coderWorkspaceHostname != other.coderWorkspaceHostname) return false
42+
if (projectPath != other.projectPath) return false
43+
if (ideProductCode != other.ideProductCode) return false
44+
if (ideBuildNumber != other.ideBuildNumber) return false
45+
if (downloadSource != other.downloadSource) return false
46+
47+
return true
48+
}
49+
50+
override fun hashCode(): Int {
51+
var result = super.hashCode()
52+
result = 31 * result + (coderWorkspaceHostname?.hashCode() ?: 0)
53+
result = 31 * result + (projectPath?.hashCode() ?: 0)
54+
result = 31 * result + (ideProductCode?.hashCode() ?: 0)
55+
result = 31 * result + (ideBuildNumber?.hashCode() ?: 0)
56+
result = 31 * result + (downloadSource?.hashCode() ?: 0)
57+
return result
58+
}
59+
60+
override fun compareTo(other: RecentWorkspaceConnection): Int {
61+
val i = other.coderWorkspaceHostname?.let { coderWorkspaceHostname?.compareTo(it) }
62+
if (i != null && i != 0) return i
63+
64+
val j = other.projectPath?.let { projectPath?.compareTo(it) }
65+
if (j != null && j != 0) return j
66+
67+
val k = other.ideProductCode?.let { ideProductCode?.compareTo(it) }
68+
if (k != null && k != 0) return k
69+
70+
val l = other.ideBuildNumber?.let { ideBuildNumber?.compareTo(it) }
71+
if (l != null && l != 0) return l
72+
73+
val m = other.downloadSource?.let { downloadSource?.compareTo(it) }
74+
if (m != null && m != 0) return m
75+
return 0
76+
}
3377
}

src/main/kotlin/com/coder/gateway/models/RecentWorkspaceConnectionState.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,20 @@ import com.intellij.util.xmlb.annotations.XCollection
55

66
class RecentWorkspaceConnectionState : BaseState() {
77
@get:XCollection
8-
var recentConnections by list<RecentWorkspaceConnection>()
8+
var recentConnections by treeSet<RecentWorkspaceConnection>()
9+
10+
fun add(connection: RecentWorkspaceConnection): Boolean {
11+
// if the item is already there but with a different last update timestamp, remove it
12+
recentConnections.remove(connection)
13+
// and add it again with the new timestamp
14+
val result = recentConnections.add(connection)
15+
if (result) incrementModificationCount()
16+
return result
17+
}
18+
19+
fun remove(connection: RecentWorkspaceConnection): Boolean {
20+
val result = recentConnections.remove(connection)
21+
if (result) incrementModificationCount()
22+
return result
23+
}
924
}

src/main/kotlin/com/coder/gateway/services/CoderRecentWorkspaceConnectionsService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import com.intellij.openapi.diagnostic.Logger
1515
class CoderRecentWorkspaceConnectionsService : PersistentStateComponent<RecentWorkspaceConnectionState> {
1616
var myState = RecentWorkspaceConnectionState()
1717

18-
fun addRecentConnection(connection: RecentWorkspaceConnection) = myState.recentConnections.add(connection)
18+
fun addRecentConnection(connection: RecentWorkspaceConnection) = myState.add(connection)
1919

20-
fun removeConnection(connection: RecentWorkspaceConnection) = myState.recentConnections.remove(connection)
20+
fun removeConnection(connection: RecentWorkspaceConnection) = myState.remove(connection)
2121

2222
fun getAllRecentConnections() = myState.recentConnections
2323

0 commit comments

Comments
 (0)