Skip to content

Commit

Permalink
added ReminderComponenet & ReminderNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-fima committed Nov 20, 2019
1 parent c6b25b6 commit b8a0219
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/kotlin/com/fimaworks/jetbrains/tenbis/Constants.kt
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/"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.intellij.openapi.actionSystem.AnActionEvent
class OrderFoodAction : AnAction() {
override fun actionPerformed(p0: AnActionEvent) {
// doesn't get any simpler than this
BrowserUtil.browse("https://www.10bis.co.il/")
BrowserUtil.browse(Constants.browserUrl)
}

// should the button be hidden after the order was made?
override fun update(e: AnActionEvent) {
super.update(e)
}
}
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()
}
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
}
}
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
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
// 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 />")
}
}
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@
</action>

</actions>

<application-components>
<component>
<implementation-class>com.fimaworks.jetbrains.tenbis.reminder.ReminderComponent</implementation-class>
</component>
</application-components>

</idea-plugin>

0 comments on commit b8a0219

Please sign in to comment.