Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,46 @@ enum class PomodoroMode(
val emoji: String,
val sessionMinutes: Int,
val breakMinutes: Int,
val sessionsPerRound: Int
val sessionsPerRound: Int,
val longBreakMinutes: Int,
val longBreakAfter: Int
) {
CLASSIC(
displayName = "Classic Pomodoro",
emoji = "🍅",
sessionMinutes = 25,
breakMinutes = 5,
sessionsPerRound = 4
sessionsPerRound = 4,
longBreakMinutes = 15,
longBreakAfter = 4
),
DEEP_WORK(
displayName = "Deep Work",
emoji = "⚡",
sessionMinutes = 52,
breakMinutes = 17,
sessionsPerRound = 3
sessionMinutes = 50,
breakMinutes = 10,
sessionsPerRound = 2,
longBreakMinutes = 30,
longBreakAfter = 2
),
CUSTOM(
displayName = "Custom",
emoji = "⚙️",
sessionMinutes = 25,
breakMinutes = 5,
sessionsPerRound = 4
sessionsPerRound = 4,
longBreakMinutes = 15,
longBreakAfter = 4
);

fun toSettings(): PomodoroSettings {
return PomodoroSettings(
mode = this,
sessionMinutes = sessionMinutes,
breakMinutes = breakMinutes,
sessionsPerRound = sessionsPerRound
sessionsPerRound = sessionsPerRound,
longBreakMinutes = longBreakMinutes,
longBreakAfter = longBreakAfter
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ data class PomodoroSettings(
val mode: PomodoroMode = PomodoroMode.CLASSIC,
val sessionMinutes: Int,
val breakMinutes: Int,
val sessionsPerRound: Int
val sessionsPerRound: Int,
val longBreakMinutes: Int = breakMinutes,
val longBreakAfter: Int = sessionsPerRound
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.akshayashokcode.devfocus.services.pomodoro

import com.github.akshayashokcode.devfocus.model.PomodoroMode
import com.github.akshayashokcode.devfocus.model.PomodoroSettings
import com.intellij.openapi.components.Service
import kotlinx.coroutines.CoroutineScope
Expand All @@ -16,28 +17,35 @@
@Service(Service.Level.PROJECT)
class PomodoroTimerService {
companion object {
private const val DEFAULT_MINUTES = 25
private const val ONE_SECOND = 1000L
}

enum class TimerState { IDLE, RUNNING, PAUSED }

private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

private var remainingTimeMs: Long = TimeUnit.MINUTES.toMillis(DEFAULT_MINUTES.toLong())
private var job: Job? = null

private var settings = PomodoroMode.CLASSIC.toSettings()
private var remainingTimeMs: Long = TimeUnit.MINUTES.toMillis(settings.sessionMinutes.toLong())

private val _timeLeft = MutableStateFlow(formatTime(remainingTimeMs))
val timeLeft: StateFlow<String> = _timeLeft

private val _state = MutableStateFlow(TimerState.IDLE)
val state: StateFlow<TimerState> = _state

private var settings = PomodoroSettings(25, 5, 4) // default
private val _currentSession = MutableStateFlow(1)
val currentSession: StateFlow<Int> = _currentSession

private val _settings = MutableStateFlow(settings)
val settingsFlow: StateFlow<PomodoroSettings> = _settings

Check warning on line 41 in src/main/kotlin/com/github/akshayashokcode/devfocus/services/pomodoro/PomodoroTimerService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Property "settingsFlow" is never used

fun start() {
if (_state.value == TimerState.RUNNING) return

// Cancel any existing job to ensure only one timer is running
job?.cancel()

_state.value = TimerState.RUNNING
job = coroutineScope.launch {
while (remainingTimeMs > 0 && isActive) {
Expand All @@ -59,9 +67,14 @@
}

fun reset() {
// Cancel any running job
job?.cancel()
remainingTimeMs = TimeUnit.MINUTES.toMillis(DEFAULT_MINUTES.toLong())
job = null

// Reset to initial state
remainingTimeMs = TimeUnit.MINUTES.toMillis(settings.sessionMinutes.toLong())
_timeLeft.value = formatTime(remainingTimeMs)
_currentSession.value = 1
_state.value = TimerState.IDLE
}

Expand All @@ -72,10 +85,27 @@
return String.format("%02d:%02d", minutes, seconds)
}

fun applySettings(settings: PomodoroSettings) {
this.settings = settings
remainingTimeMs = TimeUnit.MINUTES.toMillis(settings.sessionMinutes.toLong())
fun applySettings(newSettings: PomodoroSettings) {
// Cancel any running job when settings change
job?.cancel()
job = null

settings = newSettings
_settings.value = newSettings
remainingTimeMs = TimeUnit.MINUTES.toMillis(newSettings.sessionMinutes.toLong())
_timeLeft.value = formatTime(remainingTimeMs)
_currentSession.value = 1
_state.value = TimerState.IDLE
}

fun applyMode(mode: PomodoroMode) {
applySettings(mode.toSettings())
}

fun getSettings(): PomodoroSettings = settings

fun getProgress(): Float {
val totalMs = TimeUnit.MINUTES.toMillis(settings.sessionMinutes.toLong())
return if (totalMs > 0) remainingTimeMs.toFloat() / totalMs.toFloat() else 0f
}
}
Original file line number Diff line number Diff line change
@@ -1,67 +1,145 @@
package com.github.akshayashokcode.devfocus.toolWindow

import com.github.akshayashokcode.devfocus.model.PomodoroMode
import com.github.akshayashokcode.devfocus.model.PomodoroSettings
import com.github.akshayashokcode.devfocus.services.pomodoro.PomodoroTimerService
import com.github.akshayashokcode.devfocus.ui.components.CircularTimerPanel
import com.github.akshayashokcode.devfocus.ui.components.SessionIndicatorPanel
import com.github.akshayashokcode.devfocus.ui.settings.PomodoroSettingsPanel
import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBPanel
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collectLatest
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.*
import javax.swing.*

class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>>(BorderLayout()) {

private val timerService = project.getService(PomodoroTimerService::class.java) ?: error("PomodoroTimerService not available")

private val timeLabel = JLabel("25:00").apply {
// Mode selector
private val modeComboBox = JComboBox(PomodoroMode.entries.toTypedArray()).apply {
selectedItem = PomodoroMode.CLASSIC
}

// Info label showing current mode settings
private val infoLabel = JLabel("📊 25 min work • 5 min break").apply {
horizontalAlignment = SwingConstants.CENTER
font = font.deriveFont(32f)
font = font.deriveFont(Font.PLAIN, 12f)
}

// Circular timer display
private val circularTimer = CircularTimerPanel()

// Session indicator with tomato icons
private val sessionIndicator = SessionIndicatorPanel()

// Control buttons
private val startButton = JButton("Start")
private val pauseButton = JButton("Pause")
private val resetButton = JButton("Reset")

// Custom settings panel (only visible when Custom mode selected)
private val settingsPanel = PomodoroSettingsPanel { session, breakTime, sessions ->
timerService.applySettings(PomodoroSettings(session, breakTime, sessions))
timerService.applySettings(PomodoroSettings(PomodoroMode.CUSTOM, session, breakTime, sessions))
updateInfoLabel(session, breakTime)
updateProgressBar(sessions)
}

private val scope = CoroutineScope(Dispatchers.Default)
private var stateJob: Job? = null
private var timeJob: Job? = null
private var sessionJob: Job? = null

init {
val buttonPanel = JPanel(FlowLayout()).apply {
buildUI()
setupListeners()
observeTimer()
updateSettingsPanelVisibility()
}

private fun buildUI() {
// Top panel with mode selector
val topPanel = JPanel(BorderLayout(5, 5)).apply {
border = BorderFactory.createEmptyBorder(10, 10, 5, 10)
add(modeComboBox, BorderLayout.CENTER)
}

// Info panel
val infoPanel = JPanel(FlowLayout(FlowLayout.CENTER)).apply {
add(infoLabel)
}

// Timer panel
val timerPanel = JPanel(BorderLayout()).apply {
border = BorderFactory.createEmptyBorder(20, 10, 20, 10)
add(circularTimer, BorderLayout.CENTER)
}

// Progress panel
val progressPanel = JPanel(BorderLayout(5, 5)).apply {
border = BorderFactory.createEmptyBorder(0, 20, 10, 20)
add(sessionIndicator, BorderLayout.CENTER)
}

// Button panel
val buttonPanel = JPanel(FlowLayout(FlowLayout.CENTER, 10, 5)).apply {
add(startButton)
add(pauseButton)
add(resetButton)
}

val centerPanel = JPanel(BorderLayout()).apply {
add(timeLabel, BorderLayout.CENTER)
add(buttonPanel, BorderLayout.SOUTH)
// Center content
val centerPanel = JPanel().apply {
layout = BoxLayout(this, BoxLayout.Y_AXIS)
add(infoPanel)
add(timerPanel)
add(progressPanel)
add(buttonPanel)
}

add(topPanel, BorderLayout.NORTH)
add(centerPanel, BorderLayout.CENTER)
add(settingsPanel, BorderLayout.SOUTH)

setupListeners()
observeTimer()
}

private fun setupListeners() {
startButton.addActionListener { timerService.start() }
pauseButton.addActionListener { timerService.pause() }
resetButton.addActionListener { timerService.reset() }

modeComboBox.addActionListener {
val selectedMode = modeComboBox.selectedItem as PomodoroMode
if (selectedMode != PomodoroMode.CUSTOM) {
timerService.applyMode(selectedMode)
updateInfoLabel(selectedMode.sessionMinutes, selectedMode.breakMinutes)
updateProgressBar(selectedMode.sessionsPerRound)
}
updateSettingsPanelVisibility()
}
}

private fun updateSettingsPanelVisibility() {
val isCustom = modeComboBox.selectedItem == PomodoroMode.CUSTOM
settingsPanel.isVisible = isCustom
revalidate()
repaint()
}

private fun updateInfoLabel(sessionMin: Int, breakMin: Int) {
infoLabel.text = "📊 $sessionMin min work • $breakMin min break"
}

private fun updateProgressBar(totalSessions: Int) {
sessionIndicator.updateSessions(timerService.currentSession.value, totalSessions)
}

private fun observeTimer() {
timeJob = scope.launch {
timerService.timeLeft.collectLatest {
timerService.timeLeft.collectLatest { time ->
SwingUtilities.invokeLater {
timeLabel.text = it
val progress = timerService.getProgress()
circularTimer.updateTimer(time, progress, false)
}
}
}
Expand All @@ -72,6 +150,17 @@ class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>
startButton.isEnabled = it != PomodoroTimerService.TimerState.RUNNING
pauseButton.isEnabled = it == PomodoroTimerService.TimerState.RUNNING
resetButton.isEnabled = it != PomodoroTimerService.TimerState.IDLE
// Disable mode selector when timer is active (running or paused)
modeComboBox.isEnabled = it == PomodoroTimerService.TimerState.IDLE
}
}
}

sessionJob = scope.launch {
timerService.currentSession.collectLatest { session ->
SwingUtilities.invokeLater {
val settings = timerService.getSettings()
sessionIndicator.updateSessions(session, settings.sessionsPerRound)
}
}
}
Expand All @@ -80,6 +169,7 @@ class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>
fun dispose() {
stateJob?.cancel()
timeJob?.cancel()
sessionJob?.cancel()
scope.cancel()
}
}
Loading
Loading