-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ReminderComponenet & ReminderNotification
- Loading branch information
1 parent
c6b25b6
commit b8a0219
Showing
6 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.fimaworks.jetbrains.tenbis | ||
|
||
object Constants { | ||
const val browserUrl = "https://www.10bis.co.il/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/com/fimaworks/jetbrains/tenbis/extensions/LocalDateTime.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.fimaworks.jetbrains.tenbis.extensions | ||
|
||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
|
||
|
||
/** | ||
* Check if @LocalDateTime is after a certain hour of the day | ||
*/ | ||
fun LocalDateTime.isAfter(hour: Int, minutes: Int): Boolean { | ||
return this.isAfter( | ||
this | ||
.withHour(hour) | ||
.withMinute(minutes) | ||
.withSecond(0) | ||
.withNano(0) | ||
) | ||
} | ||
|
||
/** | ||
* is this @LocalDateTime is today | ||
*/ | ||
fun LocalDateTime.isToday(): Boolean { | ||
|
||
val todayStart = LocalDateTime | ||
.now() | ||
.withHour(0) | ||
.withMinute(0) | ||
.withSecond(0) | ||
.withNano(0) | ||
|
||
val todayEnd = todayStart.plusDays(1) | ||
|
||
val millis = this.millis | ||
|
||
if (millis >= todayStart.millis | ||
&& millis < todayEnd.millis | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* quick conversion to millis | ||
*/ | ||
val LocalDateTime.millis: Long | ||
get() { | ||
return this.toInstant(ZoneOffset.UTC).toEpochMilli() | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/kotlin/com/fimaworks/jetbrains/tenbis/reminder/ReminderComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.fimaworks.jetbrains.tenbis.reminder | ||
|
||
import com.fimaworks.jetbrains.tenbis.extensions.isAfter | ||
import com.fimaworks.jetbrains.tenbis.extensions.isToday | ||
import com.intellij.openapi.components.BaseComponent | ||
import java.time.LocalDateTime | ||
import java.util.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
|
||
class ReminderComponent : BaseComponent { | ||
|
||
// todo - need to save last reminder so we don't popup everytime the user launches IntelliJ | ||
private var lastReminder = LocalDateTime.now().minusDays(1) | ||
|
||
private val timer = Timer("10bis_reminder_timer") | ||
|
||
private val timerTask = | ||
ReminderTimerTask(object : | ||
LastReminderListener { | ||
override fun updateLastReminder(now: LocalDateTime) { | ||
this@ReminderComponent.lastReminder = now | ||
} | ||
|
||
override val lastReminder: LocalDateTime | ||
get() = this@ReminderComponent.lastReminder | ||
}) | ||
|
||
override fun initComponent() { | ||
super.initComponent() | ||
|
||
// schedule task every 1 minute | ||
timer.schedule( | ||
timerTask, | ||
Date(System.currentTimeMillis()), | ||
TimeUnit.MINUTES.toMillis(1) | ||
) | ||
} | ||
|
||
override fun disposeComponent() { | ||
timerTask.cancel() | ||
timer.cancel() | ||
super.disposeComponent() | ||
} | ||
|
||
class ReminderTimerTask(private val lastReminderListener: LastReminderListener) : TimerTask() { | ||
override fun run() { | ||
|
||
val isRemindedForToday = lastReminderListener.lastReminder.isToday() | ||
|
||
// todo - make this configurable | ||
|
||
val reminderHour = 11 | ||
val reminderMinutes = 0 | ||
|
||
val isAfterReminderTime = LocalDateTime.now().isAfter(reminderHour, reminderMinutes) | ||
|
||
if (isRemindedForToday.not() && isAfterReminderTime) { | ||
|
||
// todo - add sound? | ||
|
||
// ui notification | ||
ReminderNotification.remindUser() | ||
|
||
// update last reminder to now | ||
lastReminderListener.updateLastReminder(LocalDateTime.now()) | ||
} | ||
} | ||
} | ||
|
||
interface LastReminderListener { | ||
fun updateLastReminder(now: LocalDateTime) | ||
|
||
val lastReminder: LocalDateTime | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/com/fimaworks/jetbrains/tenbis/reminder/ReminderNotification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.fimaworks.jetbrains.tenbis.reminder | ||
|
||
import com.fimaworks.jetbrains.tenbis.Constants | ||
import com.intellij.ide.BrowserUtil | ||
import com.intellij.notification.* | ||
|
||
object ReminderNotification { | ||
|
||
fun remindUser() { | ||
// prep message | ||
val message = escapeString( | ||
// order button | ||
"<a href='${Constants.browserUrl}'>Order Now</a>" + | ||
// six spaces | ||
" " + | ||
// dismiss buton | ||
"<a href='dismiss'>Dismiss</a>" | ||
)!! | ||
|
||
// create notificaiton | ||
val notification = NotificationGroup("10bis Plugin", NotificationDisplayType.STICKY_BALLOON, false) | ||
.createNotification( | ||
"Did you remember to order food?", | ||
message, | ||
NotificationType.WARNING | ||
) { notification, event -> | ||
if (event != null) { | ||
if (event.url != null) { | ||
BrowserUtil.browse(event.url) | ||
} | ||
} | ||
|
||
// dismiss notification | ||
notification?.expire() | ||
} | ||
|
||
// ping user | ||
notification.notify(null) | ||
} | ||
|
||
private fun escapeString(string: String?): String? { | ||
return if (string == null || !string.contains("\n")) { | ||
string | ||
} else string.replace("\n".toRegex(), "\n<br />") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters