-
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.
Merge pull request #1 from nadavfima/development
V1.0.2 Merge
- Loading branch information
Showing
13 changed files
with
345 additions
and
4 deletions.
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
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() | ||
} |
80 changes: 80 additions & 0 deletions
80
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,80 @@ | ||
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 { | ||
|
||
private var lastReminder = ReminderConfigurationProvider.instance.state.lastReminder | ||
|
||
private val timer = Timer("10bis_reminder_timer") | ||
|
||
private val timerTask = | ||
ReminderTimerTask(object : | ||
LastReminderListener { | ||
override fun updateLastReminder(now: LocalDateTime) { | ||
this@ReminderComponent.lastReminder = now | ||
ReminderConfigurationProvider.instance.state.lastReminder = now | ||
|
||
} | ||
|
||
override val lastReminder: LocalDateTime | ||
get() = this@ReminderComponent.lastReminder | ||
}) | ||
|
||
override fun initComponent() { | ||
super.initComponent() | ||
|
||
// delay first trigger by 15 seconds | ||
val initialDelayInMillis = TimeUnit.SECONDS.toMillis(15) | ||
|
||
// schedule task every 30 seconds | ||
val periodInMillis = TimeUnit.SECONDS.toMillis(30) | ||
|
||
timer.schedule( | ||
timerTask, | ||
Date(System.currentTimeMillis() + initialDelayInMillis), | ||
periodInMillis | ||
) | ||
} | ||
|
||
override fun disposeComponent() { | ||
timerTask.cancel() | ||
timer.cancel() | ||
super.disposeComponent() | ||
} | ||
|
||
class ReminderTimerTask(private val lastReminderListener: LastReminderListener) : TimerTask() { | ||
override fun run() { | ||
|
||
val isRemindedForToday = lastReminderListener.lastReminder.isToday() | ||
|
||
val configState = ReminderConfigurationProvider.instance.state | ||
|
||
val reminderHour = configState.reminderHour | ||
val reminderMinutes = configState.reminderMinutes | ||
|
||
val isAfterReminderTime = LocalDateTime.now().isAfter(reminderHour, reminderMinutes) | ||
|
||
if (isRemindedForToday.not() && isAfterReminderTime) { | ||
|
||
// todo - add notification sound? | ||
|
||
// ui notification | ||
ReminderNotification.notifyUser(lastReminderListener) | ||
|
||
} | ||
} | ||
} | ||
|
||
interface LastReminderListener { | ||
fun updateLastReminder(now: LocalDateTime) | ||
|
||
val lastReminder: LocalDateTime | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/com/fimaworks/jetbrains/tenbis/reminder/ReminderConfigurable.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,87 @@ | ||
package com.fimaworks.jetbrains.tenbis.reminder | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.options.Configurable | ||
import com.intellij.openapi.options.Configurable.NoScroll | ||
import java.text.NumberFormat | ||
import javax.swing.* | ||
import javax.swing.text.NumberFormatter | ||
|
||
|
||
class ReminderConfigurable : Configurable, NoScroll, Disposable { | ||
|
||
private val hourFormatter = NumberFormatter(NumberFormat.getIntegerInstance()).also { | ||
it.minimum = 0 | ||
it.maximum = 23 | ||
it.allowsInvalid = true | ||
} | ||
|
||
private val minutesFormatter = NumberFormatter(NumberFormat.getIntegerInstance()).also { | ||
it.minimum = 0 | ||
it.maximum = 59 | ||
it.allowsInvalid = true | ||
} | ||
private val configState | ||
get() = ReminderConfigurationProvider.instance.state | ||
|
||
// ui | ||
private var hourField: JFormattedTextField? = | ||
JFormattedTextField(hourFormatter).also { it.text = configState.reminderHour.toString() } | ||
|
||
private var minutesField: JTextField? = | ||
JFormattedTextField(minutesFormatter).also { it.text = configState.reminderMinutes.toString() } | ||
|
||
override fun getDisplayName(): String = "10bis Plugin Configuration" | ||
|
||
override fun createComponent(): JComponent? { | ||
val dialogPanel = JPanel() | ||
|
||
val timeLabel = JLabel("Reminder Time") | ||
|
||
dialogPanel.add(timeLabel) | ||
dialogPanel.add(createTimeSetting()) | ||
|
||
return dialogPanel | ||
} | ||
|
||
// todo - need to fix ui | ||
private fun createTimeSetting(): JPanel { | ||
|
||
val timePanel = JPanel() | ||
|
||
timePanel.add(hourField) | ||
timePanel.add(JLabel(" : ")) // 10:00 seperator | ||
timePanel.add(minutesField) | ||
|
||
return timePanel | ||
} | ||
|
||
override fun dispose() { | ||
hourField = null | ||
minutesField = null | ||
} | ||
|
||
override fun isModified(): Boolean { | ||
|
||
return configState.reminderHour != hourField!!.text.toIntOrNull() | ||
|| configState.reminderMinutes != minutesField!!.text.toIntOrNull() | ||
} | ||
|
||
override fun apply() { | ||
hourField!!.text.toIntOrNull()?.let { | ||
if (it in 0..23) | ||
configState.reminderHour = it | ||
|
||
} | ||
|
||
minutesField!!.text.toIntOrNull()?.let { | ||
if (it in 0..59) | ||
configState.reminderMinutes = it | ||
} | ||
} | ||
|
||
override fun reset() { | ||
hourField!!.text = configState.reminderHour.toString() | ||
minutesField!!.text = configState.reminderMinutes.toString() | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/com/fimaworks/jetbrains/tenbis/reminder/ReminderConfigurationProvider.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,36 @@ | ||
package com.fimaworks.jetbrains.tenbis.reminder | ||
|
||
import com.intellij.openapi.components.PersistentStateComponent | ||
import com.intellij.openapi.components.ServiceManager | ||
import com.intellij.openapi.components.State | ||
import com.intellij.openapi.components.Storage | ||
import com.intellij.util.xmlb.XmlSerializerUtil | ||
import java.time.LocalDateTime | ||
|
||
@State( | ||
name = "ReminderConfigurationProvider", | ||
storages = [Storage("10bis-plugin.xml")] | ||
) | ||
open class ReminderConfigurationProvider : PersistentStateComponent<ReminderConfigurationProvider.ConfigurationState> { | ||
|
||
private var myState: ConfigurationState = ConfigurationState() | ||
|
||
override fun getState(): ConfigurationState { | ||
return myState | ||
} | ||
|
||
override fun loadState(state: ConfigurationState) { | ||
myState = state | ||
} | ||
|
||
class ConfigurationState { | ||
var lastReminder: LocalDateTime = LocalDateTime.now().minusDays(1) | ||
var reminderHour = 11 | ||
var reminderMinutes = 0 | ||
} | ||
|
||
companion object { | ||
val instance: ReminderConfigurationProvider | ||
get() = ServiceManager.getService(ReminderConfigurationProvider::class.java) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
package com.fimaworks.jetbrains.tenbis.reminder | ||
|
||
import com.fimaworks.jetbrains.tenbis.Constants | ||
import com.intellij.ide.BrowserUtil | ||
import com.intellij.notification.* | ||
import java.time.LocalDateTime | ||
|
||
object ReminderNotification { | ||
|
||
fun notifyUser(lastReminderListener: ReminderComponent.LastReminderListener) { | ||
// 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() | ||
|
||
} | ||
|
||
notification.whenExpired { | ||
// update last reminder to now | ||
lastReminderListener.updateLastReminder(LocalDateTime.now()) | ||
} | ||
|
||
// 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