Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAK-58 IJ: Start timed flow modes from status bar widget #102

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/kotlin/co/makerflow/intellijplugin/MyIcons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ class MyIcons {

@JvmField
val pluginIcon = IconLoader.getIcon("/icons/pluginIcon.svg", javaClass)

@JvmField
val startTimer = IconLoader.getIcon("/icons/startTimer.svg", javaClass)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package co.makerflow.intellijplugin.actions.flowmode

class FiftyMinutesFlowModeAction :
FlowModeAction("Flow Mode (50 minutes)", "Begin a timed flow mode session for 50 minutes", duration = 50)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package co.makerflow.intellijplugin.actions.flowmode

import co.makerflow.intellijplugin.services.FlowModeService
import co.makerflow.intellijplugin.services.toFlow
import co.makerflow.intellijplugin.state.FlowState
import com.intellij.ide.actions.searcheverywhere.PossibleSlowContributor
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

abstract class FlowModeAction(
text: String,
description: String,
private val duration: Int? = null
) :
AnAction(text, description, null), PossibleSlowContributor {
private val startFlowModeCoroutineScope = CoroutineScope(Dispatchers.IO)
private val stopFlowModeCoroutineScope = CoroutineScope(Dispatchers.IO)
private fun stopFlowMode() {
FlowState.instance.processing = true
val flowModeService = service<FlowModeService>()
ApplicationManager.getApplication().invokeLater {
stopFlowModeCoroutineScope.launch {
flowModeService.stopFlowMode()
FlowState.instance.currentFlow = null
}.invokeOnCompletion {
FlowState.instance.processing = false
}
}
}

private fun startFlowMode() {
FlowState.instance.processing = true
val flowModeService = service<FlowModeService>()
ApplicationManager.getApplication().invokeLater {
startFlowModeCoroutineScope.launch {
flowModeService.startFlowMode(null, duration)?.let { flowMode ->
FlowState.instance.currentFlow = flowMode.toFlow()
}
}.invokeOnCompletion {
FlowState.instance.processing = false
}
}
}

override fun actionPerformed(e: AnActionEvent) {
if (FlowState.instance.currentFlow == null) {
startFlowMode()
} else {
stopFlowMode()
}
}

override fun update(e: AnActionEvent) {
val flowState = FlowState.instance
if (duration != null) {
e.presentation.isEnabled = !flowState.processing && flowState.currentFlow == null
}
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package co.makerflow.intellijplugin.actions.flowmode

import co.makerflow.intellijplugin.state.Flow
import co.makerflow.intellijplugin.state.FlowState
import co.makerflow.intellijplugin.state.FlowStateChangeNotifier
import com.intellij.ide.actions.searcheverywhere.PossibleSlowContributor
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
import com.intellij.util.ui.UIUtil

const val STOP_FLOW_MODE_DIRECTIVE = "Stop Flow Mode"

private const val STOP_FLOW_MODE_DESCRIPTION = "Stop the ongoing Flow Mode session"

class FlowModeActionGroup : DefaultActionGroup(), PossibleSlowContributor {
init {
UIUtil.invokeLaterIfNeeded {
if (FlowState.instance.currentFlow != null) {
updateTemplateForStoppingFlowMode()
} else {
updateTemplateForStartingFlowMode()
}
}
ApplicationManager.getApplication().messageBus.connect()
.subscribe(FlowStateChangeNotifier.FLOW_STATE_CHANGE_TOPIC, object : FlowStateChangeNotifier {
override fun updated(flow: Flow?, processing: Boolean) {
UIUtil.invokeLaterIfNeeded {
if (flow != null) {
updateTemplateForStoppingFlowMode()
updateTimedFlowModeActions()
} else {
updateTemplateForStartingFlowMode()
updateTimedFlowModeActions()
}
}
}
})
}

override fun update(e: AnActionEvent) {
if (FlowState.instance.currentFlow != null) {
e.presentation.text = STOP_FLOW_MODE_DIRECTIVE
e.presentation.description = STOP_FLOW_MODE_DESCRIPTION
e.presentation.isEnabled = false
} else {
e.presentation.text = "Choose Flow Mode Session Type"
e.presentation.description = "Choose a Flow Mode type to start a new session"
e.presentation.isEnabled = true
}
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

private fun updateTemplateForStoppingFlowMode() {
update(buildActionEvent(this))
val toggleFlowModeAction = ActionManager.getInstance()
.getAction("co.makerflow.intellijplugin.actions.flowmode.ToggleFlowModeAction")
toggleFlowModeAction.update(
buildActionEvent(toggleFlowModeAction)
)
}

private fun updateTemplateForStartingFlowMode() {
update(buildActionEvent(this))
val toggleFlowModeAction = ActionManager.getInstance()
.getAction("co.makerflow.intellijplugin.actions.flowmode.ToggleFlowModeAction")
toggleFlowModeAction.update(
buildActionEvent(toggleFlowModeAction)
)
}

private fun updateTimedFlowModeActions() {
val twentyFiveMinutesFlowModeAction = ActionManager.getInstance()
.getAction("co.makerflow.intellijplugin.actions.flowmode.TwentyFiveMinutesFlowModeAction")
twentyFiveMinutesFlowModeAction.update(
buildActionEvent(twentyFiveMinutesFlowModeAction)
)
val fiftyMinutesFlowModeAction = ActionManager.getInstance()
.getAction("co.makerflow.intellijplugin.actions.flowmode.FiftyMinutesFlowModeAction")
fiftyMinutesFlowModeAction.update(
buildActionEvent(fiftyMinutesFlowModeAction)
)
val seventyFiveMinutesFlowModeAction = ActionManager.getInstance()
.getAction("co.makerflow.intellijplugin.actions.flowmode.SeventyFiveMinutesFlowModeAction")
seventyFiveMinutesFlowModeAction.update(
buildActionEvent(seventyFiveMinutesFlowModeAction)
)
}

private fun buildActionEvent(action: AnAction) =
AnActionEvent.createFromAnAction(
action,
null,
"",
DataContext.EMPTY_CONTEXT
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.makerflow.intellijplugin.actions.flowmode

class SeventyFiveMinutesFlowModeAction :
FlowModeAction(
"Flow Mode (75 minutes)",
"Begin a timed flow mode session for 75 minutes",
duration = 75
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package co.makerflow.intellijplugin.actions.flowmode

import co.makerflow.intellijplugin.state.FlowState
import com.intellij.openapi.actionSystem.AnActionEvent

private const val START_FLOW_MODE_DIRECTIVE = "Start Flow Mode (Without Timer)"
private const val START_FLOW_MODE_DESCRIPTION = "Begin an untimed Flow Mode session"

class ToggleFlowModeAction :
FlowModeAction(
START_FLOW_MODE_DIRECTIVE,
START_FLOW_MODE_DESCRIPTION
) {
override fun update(e: AnActionEvent) {
if (FlowState.instance.currentFlow == null) {
e.presentation.text = START_FLOW_MODE_DIRECTIVE
e.presentation.description = START_FLOW_MODE_DESCRIPTION
} else {
e.presentation.text = STOP_FLOW_MODE_DIRECTIVE
e.presentation.description = "Stop the ongoing Flow Mode session"
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package co.makerflow.intellijplugin.actions.flowmode

class TwentyFiveMinutesFlowModeAction :
FlowModeAction("Flow Mode (25 minutes)", "Begin a timed flow mode session for 25 minutes", duration = 25)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,19 @@ class TaskPresentationComponent(
try {
val flowMode = when (it.selectedItem) {
FLOW_MODE_DROPDOWN_WITHOUT_TIMER -> {
flowModeService.startFlowMode(value, time = null)
flowModeService.startFlowMode(value, duration = null)
}

FLOW_MODE_DROPDOWN_25_MINUTES -> {
flowModeService.startFlowMode(value, time = 25)
flowModeService.startFlowMode(value, duration = 25)
}

FLOW_MODE_DROPDOWN_50_MINUTES -> {
flowModeService.startFlowMode(value, time = 50)
flowModeService.startFlowMode(value, duration = 50)
}

FLOW_MODE_DROPDOWN_75_MINUTES -> {
flowModeService.startFlowMode(value, time = 75)
flowModeService.startFlowMode(value, duration = 75)
}

else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class FlowModeService {
suspend fun startFlowMode(): FlowMode? {
return startFlowMode(null, null)
}
suspend fun startFlowMode(todo: TypedTodo?, time: Int?): FlowMode? {
suspend fun startFlowMode(todo: TypedTodo?, duration: Int?): FlowMode? {

return coroutineScope {
var flowMode: FlowMode? = null
launch {
val flowModeApi = flowModeApi()
val startFlowModeResponse = flowModeApi.startFlowMode("jetbrains",
false,
time,
duration,
todo?.sourceType?.name,
todo?.type,
todo?.let { todoUtil.determineTodoId(todo) })
Expand Down
Loading