Skip to content

Commit 0619eb3

Browse files
committed
Merge branch 'feature/create-codexp-notification-manager' into develop
- Add CodeXPNofiticationManager for display notification when challenge complete or level up. - Fix wrong logic in update nickname.
2 parents bf994e5 + 5db41b9 commit 0619eb3

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.ilovegamecoding.intellijcodexp.manager
2+
3+
import com.github.ilovegamecoding.intellijcodexp.model.CodeXPChallenge
4+
import com.intellij.notification.NotificationGroup
5+
import com.intellij.notification.NotificationGroupManager
6+
import com.intellij.notification.NotificationType
7+
8+
object CodeXPNotificationManager {
9+
private val notifiationGroup: NotificationGroup =
10+
NotificationGroupManager.getInstance().getNotificationGroup("CodeXP")
11+
12+
fun notify(title: String, content: String) {
13+
notifiationGroup.createNotification(title, content, NotificationType.INFORMATION).notify(null)
14+
}
15+
16+
fun notifyChallengeComplete(codeXPChallenge: CodeXPChallenge) {
17+
notify("${codeXPChallenge.name} completed!", "Reward: ${codeXPChallenge.rewardXP} XP")
18+
}
19+
20+
fun notifyLevelUp(nickname: String, level: Int, xpToNextLevel: Long) {
21+
notify(
22+
"Level up!",
23+
"Congratulations $nickname! You are now level $level. You need $xpToNextLevel XP to reach the next level."
24+
)
25+
}
26+
}

src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/services/CodeXPService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.ilovegamecoding.intellijcodexp.services
22

33
import com.github.ilovegamecoding.intellijcodexp.listeners.CodeXPListener
4+
import com.github.ilovegamecoding.intellijcodexp.manager.CodeXPNotificationManager
45
import com.github.ilovegamecoding.intellijcodexp.model.CodeXPChallenge
56
import com.intellij.openapi.application.ApplicationManager
67
import com.intellij.openapi.components.*
@@ -264,6 +265,7 @@ class CodeXPService : PersistentStateComponent<CodeXPService.CodeXPState>, CodeX
264265
if (challenge.progress >= challenge.goal) {
265266
codeXPState.xp += challenge.rewardXP
266267
replaceChallengeWithNew(challenge, event)
268+
CodeXPNotificationManager.notifyChallengeComplete(challenge)
267269
}
268270
}
269271
}

src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPToolWindowFactory.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.github.ilovegamecoding.intellijcodexp.toolWindow
22

3-
import com.github.ilovegamecoding.intellijcodexp.StringUtil
43
import com.github.ilovegamecoding.intellijcodexp.form.CodeXPChallengeForm
54
import com.github.ilovegamecoding.intellijcodexp.form.CodeXPDashboard
65
import com.github.ilovegamecoding.intellijcodexp.listeners.CodeXPListener
6+
import com.github.ilovegamecoding.intellijcodexp.manager.CodeXPNotificationManager
77
import com.github.ilovegamecoding.intellijcodexp.model.CodeXPChallenge
88
import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService
9+
import com.github.ilovegamecoding.intellijcodexp.util.StringUtil
910
import com.intellij.openapi.application.ApplicationManager
1011
import com.intellij.openapi.project.Project
1112
import com.intellij.openapi.wm.ToolWindow
@@ -160,7 +161,7 @@ class CodeXPToolWindowFactory : ToolWindowFactory {
160161
* @param codeXPDashboard CodeXP dashboard.
161162
*/
162163
private fun updateNickname(codeXPService: CodeXPService, codeXPDashboard: CodeXPDashboard) {
163-
codeXPDashboard.tfNickname.text = codeXPService.state.nickname
164+
codeXPService.state.nickname = codeXPDashboard.tfNickname.text
164165
}
165166

166167
/**
@@ -172,10 +173,16 @@ class CodeXPToolWindowFactory : ToolWindowFactory {
172173
private fun updateXPInfo(codeXPService: CodeXPService, codeXPDashboard: CodeXPDashboard) {
173174
val totalXP = codeXPService.state.xp
174175
codeXPDashboard.lblTotalXP.text = StringUtil.numberToStringWithCommas(totalXP)
175-
val (currentLevel, xpIntoCurrentLevel, progressToNextLevel) = calculateLevelAndProgress(
176+
val (currentLevel, xpIntoCurrentLevel, progressToNextLevel, xpToNextLevel) = calculateLevelAndProgress(
176177
totalXP
177178
)
178179

180+
181+
val beforeLevel = codeXPDashboard.lblCurrentLevel.text.toInt()
182+
if (beforeLevel != currentLevel && beforeLevel != 0) {
183+
CodeXPNotificationManager.notifyLevelUp(codeXPService.state.nickname, currentLevel, xpToNextLevel)
184+
}
185+
179186
codeXPDashboard.lblCurrentLevel.text = StringUtil.numberToStringWithCommas(currentLevel.toLong())
180187
codeXPDashboard.lblNextLevel.text = StringUtil.numberToStringWithCommas((currentLevel + 1).toLong())
181188
codeXPDashboard.lblCurrentLevelXP.text = StringUtil.numberToStringWithCommas(xpIntoCurrentLevel)
@@ -261,7 +268,12 @@ class CodeXPToolWindowFactory : ToolWindowFactory {
261268
/**
262269
* Progress to the next level.
263270
*/
264-
val progressToNextLevel: Int
271+
val progressToNextLevel: Int,
272+
273+
/**
274+
* XP needed to reach the next level.
275+
*/
276+
val xpToNextLevel: Long
265277
)
266278

267279
/**
@@ -284,7 +296,7 @@ class CodeXPToolWindowFactory : ToolWindowFactory {
284296
val xpIntoCurrentLevel = totalXP - (xp - currentLevelXP)
285297
val progress = (xpIntoCurrentLevel / currentLevelXP) * 100
286298

287-
return LevelInfo(level, xpIntoCurrentLevel.toLong(), progress.toInt())
299+
return LevelInfo(level, xpIntoCurrentLevel.toLong(), progress.toInt(), currentLevelXP.toLong())
288300
}
289301
}
290302

src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/StringUtil.kt renamed to src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/util/StringUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.ilovegamecoding.intellijcodexp
1+
package com.github.ilovegamecoding.intellijcodexp.util
22

33
object StringUtil {
44
fun numberToStringWithCommas(number: Long): String {

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<toolWindow factoryClass="com.github.ilovegamecoding.intellijcodexp.toolWindow.CodeXPToolWindowFactory"
1313
id="CodeXPToolWindow"/>
1414
<applicationService serviceImplementation="com.github.ilovegamecoding.intellijcodexp.services.CodeXPService"/>
15+
<notificationGroup displayType="BALLOON" id="CodeXP"/>
1516
</extensions>
1617

1718
<application-components>

src/test/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPUtilTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.ilovegamecoding.intellijcodexp
22

3+
import com.github.ilovegamecoding.intellijcodexp.util.StringUtil
34
import com.intellij.testFramework.TestDataPath
45
import com.intellij.testFramework.fixtures.BasePlatformTestCase
56

0 commit comments

Comments
 (0)