-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: ポモドーロタイマーの機能 #134
Merged
Merged
feat: ポモドーロタイマーの機能 #134
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5166415
refactor: 不要なルール無効コメントを削除
Hirotaka-Hanai e093506
feat: ポモドーロタイマーの実装
Hirotaka-Hanai 0a375e1
refactor: 読み上げと通知を呼び出す処理をmainに移動
Hirotaka-Hanai 4d82e4f
feat: タイマーページのデザインの調整
Hirotaka-Hanai 28314ae
feat: 設定項目の追加
Hirotaka-Hanai c7ee8fa
test: ポモドーロタイマーのテストの実装
Hirotaka-Hanai c76bfcb
test: テストの修正
Hirotaka-Hanai dee639c
refactor: ポモドーロタイマーの処理をrendererプロセスに移動
Hirotaka-Hanai f73694a
rename: ファイル名の修正
Hirotaka-Hanai 4ad0585
refactor: 不要なコードの削除
Hirotaka-Hanai 4d6aa15
fix: 時間表示の関数の修正
Hirotaka-Hanai 62740aa
refactor: 不要なコードの削除
Hirotaka-Hanai 3e40f40
fix: 時間表示方法の修正
Hirotaka-Hanai 6b614b8
fix: デバッガ―の警告の解消
Hirotaka-Hanai 5b83a18
fix: 設定項目を増やしたときにデフォルト値が反映されない不具合の修正
Hirotaka-Hanai 233e931
docs: UserPreferenceStoreServiceImplに実装の説明を追加
Hirotaka-Hanai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PomodoroTimerDetails, TimerSession } from '@shared/data/PomodoroTimerDetails'; | ||
import React from 'react'; | ||
|
||
type PomodoroTimerContextType = { | ||
pomodoroTimerDetails: PomodoroTimerDetails | null; | ||
|
||
setTimer: (timerSession: TimerSession) => void; | ||
startTimer: () => void; | ||
pauseTimer: () => void; | ||
stopTimer: () => void; | ||
}; | ||
|
||
const pomodoroTimerContext = React.createContext<PomodoroTimerContextType>({ | ||
pomodoroTimerDetails: null, | ||
|
||
setTimer: () => {}, | ||
startTimer: () => {}, | ||
pauseTimer: () => {}, | ||
stopTimer: () => {}, | ||
}); | ||
|
||
export default pomodoroTimerContext; |
213 changes: 213 additions & 0 deletions
213
src/renderer/src/components/PomodoroTimerContextProvider.tsx
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,213 @@ | ||
import rendererContainer from '../inversify.config'; | ||
import { ReactNode, useCallback, useContext, useEffect, useState } from 'react'; | ||
import AppContext from './AppContext'; | ||
import { TYPES } from '@renderer/types'; | ||
import { PomodoroTimerDetails, TimerSession, TimerState } from '@shared/data/PomodoroTimerDetails'; | ||
import PomodoroTimerContext from './PomodoroTimerContext'; | ||
import { TimerManager } from '@shared/utils/TimerManager'; | ||
import { IUserPreferenceProxy } from '@renderer/services/IUserPreferenceProxy'; | ||
import { DateUtil } from '@shared/utils/DateUtil'; | ||
import { ISpeakEventService } from '@renderer/services/ISpeakEventService'; | ||
import { INotificationService } from '@renderer/services/INotificationService'; | ||
import { PomodoroNotificationSetting } from '@shared/data/PomodoroNotificationSetting'; | ||
|
||
export const PomodoroTimerContextProvider = ({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element => { | ||
const [pomodoroTimerDetails, setPomodoroTimerDetails] = useState<PomodoroTimerDetails | null>( | ||
null | ||
); | ||
const [lastUpdated, setLastUpdate] = useState<Date | null>(null); | ||
|
||
const { userDetails } = useContext(AppContext); | ||
|
||
const TIMER_NAME = 'pomodoroTimer'; | ||
const timer = rendererContainer.get<TimerManager>(TYPES.TimerManager).get(TIMER_NAME); | ||
|
||
const userPreferenceProxy = rendererContainer.get<IUserPreferenceProxy>( | ||
TYPES.UserPreferenceProxy | ||
); | ||
|
||
const speakEventService = rendererContainer.get<ISpeakEventService>(TYPES.SpeakEventSubscriber); | ||
const notificationService = rendererContainer.get<INotificationService>( | ||
TYPES.NotificationSubscriber | ||
); | ||
|
||
const dateUtil = rendererContainer.get<DateUtil>(TYPES.DateUtil); | ||
|
||
const setTimer = useCallback( | ||
async (session: TimerSession): Promise<void> => { | ||
if (userDetails == null) { | ||
return; | ||
} | ||
const userPreference = await userPreferenceProxy.getOrCreate(userDetails.userId); | ||
const initialMinutes = | ||
session === TimerSession.WORK ? userPreference.workingMinutes : userPreference.breakMinutes; | ||
setPomodoroTimerDetails({ | ||
session: session, | ||
state: TimerState.STOPPED, | ||
currentTime: initialMinutes * 60 * 1000, | ||
}); | ||
}, | ||
[userDetails, userPreferenceProxy] | ||
); | ||
|
||
useEffect(() => { | ||
setTimer(TimerSession.WORK); | ||
}, [setTimer]); | ||
|
||
const getNextSession = (session: TimerSession): TimerSession => | ||
session === TimerSession.WORK ? TimerSession.BREAK : TimerSession.WORK; | ||
|
||
const sendNotification = useCallback( | ||
(setting: PomodoroNotificationSetting): void => { | ||
if (pomodoroTimerDetails == null) { | ||
return; | ||
} | ||
const session = pomodoroTimerDetails.session === TimerSession.WORK ? '作業時間' : '休憩時間'; | ||
const time = Math.ceil(pomodoroTimerDetails.currentTime / (60 * 1000)); | ||
const text = setting.template | ||
.replace('{TIME}', time.toString()) | ||
.replace('{SESSION}', session); | ||
console.log(text); | ||
if (setting.announce) { | ||
speakEventService.speak(text); | ||
} | ||
if (setting.sendNotification) { | ||
notificationService.sendNotification(text, 60 * 1000); | ||
} | ||
}, | ||
[notificationService, pomodoroTimerDetails, speakEventService] | ||
); | ||
|
||
const startTimer = useCallback((): void => { | ||
setPomodoroTimerDetails((details) => | ||
details != null | ||
? { | ||
...details, | ||
state: TimerState.RUNNING, | ||
} | ||
: null | ||
); | ||
}, []); | ||
|
||
const pauseTimer = useCallback((): void => { | ||
if (pomodoroTimerDetails == null) { | ||
return; | ||
} | ||
if (pomodoroTimerDetails.state != TimerState.RUNNING) { | ||
return; | ||
} | ||
timer.clear(); | ||
|
||
setPomodoroTimerDetails((details) => { | ||
if (details == null || details.state !== TimerState.RUNNING) { | ||
return details; | ||
} | ||
const diffTime = lastUpdated | ||
? dateUtil.getCurrentDate().getTime() - lastUpdated.getTime() | ||
: 0; | ||
return { | ||
...details, | ||
state: TimerState.PAUSED, | ||
currentTime: details.currentTime - diffTime, | ||
}; | ||
}); | ||
setLastUpdate(null); | ||
}, [dateUtil, lastUpdated, pomodoroTimerDetails, timer]); | ||
|
||
const stopTimer = (): void => { | ||
if (pomodoroTimerDetails == null) { | ||
return; | ||
} | ||
timer.clear(); | ||
|
||
setTimer(pomodoroTimerDetails.session); | ||
setLastUpdate(null); | ||
}; | ||
|
||
// 次の時間の更新のセット | ||
useEffect(() => { | ||
if (pomodoroTimerDetails == null) { | ||
return; | ||
} | ||
|
||
if (pomodoroTimerDetails.state !== TimerState.RUNNING) { | ||
return; | ||
} | ||
|
||
timer.clear(); | ||
|
||
if (pomodoroTimerDetails.currentTime > 0) { | ||
setLastUpdate(dateUtil.getCurrentDate()); | ||
|
||
// 整数秒になるように時間を更新する | ||
const intervalMs = pomodoroTimerDetails.currentTime % 1000 || 1000; | ||
// 次の時間の更新をセットする | ||
timer.addTimeout(() => { | ||
setPomodoroTimerDetails((details) => | ||
details != null | ||
? { | ||
...details, | ||
currentTime: details.currentTime - intervalMs, | ||
} | ||
: null | ||
); | ||
}, intervalMs); | ||
} | ||
}, [dateUtil, pomodoroTimerDetails, timer]); | ||
|
||
// 通知とセッションの切り替え | ||
useEffect(() => { | ||
if (userDetails == null || pomodoroTimerDetails == null) { | ||
return; | ||
} | ||
|
||
if (pomodoroTimerDetails.state !== TimerState.RUNNING) { | ||
return; | ||
} | ||
|
||
const timerEvent = async (): Promise<void> => { | ||
const userPreference = await userPreferenceProxy.getOrCreate(userDetails.userId); | ||
|
||
// 残り時間が0秒になったときの処理 | ||
if (pomodoroTimerDetails.currentTime <= 0) { | ||
sendNotification(userPreference.notifyAtPomodoroComplete); | ||
const session = getNextSession(pomodoroTimerDetails.session); | ||
await setTimer(session); | ||
startTimer(); | ||
return; | ||
} | ||
|
||
// 残り時間n秒前の処理 | ||
const offsetMs = userPreference.notifyBeforePomodoroCompleteTimeOffset * 60 * 1000; | ||
if (pomodoroTimerDetails.currentTime == offsetMs) { | ||
sendNotification(userPreference.notifyBeforePomodoroComplete); | ||
} | ||
}; | ||
timerEvent(); | ||
}, [ | ||
pomodoroTimerDetails, | ||
sendNotification, | ||
setTimer, | ||
startTimer, | ||
userDetails, | ||
userPreferenceProxy, | ||
]); | ||
|
||
return ( | ||
<PomodoroTimerContext.Provider | ||
value={{ | ||
pomodoroTimerDetails, | ||
setTimer, | ||
startTimer, | ||
pauseTimer, | ||
stopTimer, | ||
}} | ||
> | ||
{children} | ||
</PomodoroTimerContext.Provider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const MENU_TIMELINE = { path: '/', name: 'タイムライン' }; | ||
export const MENU_SETTING = { path: '/setting', name: '設定' }; | ||
export const MENU_ACTIVITY_USAGE = { path: '/activity_usage', name: 'アプリ使用時間' }; | ||
export const MENU_POMODORO_TIMER = { path: '/pomodoro_timer', name: 'ポモドーロタイマー' }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここはたぶん、コメントアウトを戻すのを忘れたのではないかと思うので確認してください