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

Schedule recurring reminders with native notifications #1

Merged
merged 1 commit into from
Feb 7, 2021
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
6 changes: 3 additions & 3 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { app, BrowserWindow } from 'electron';
import * as isDev from 'electron-is-dev';
import { TrayBuilder } from './TrayBuilder';
import { app, BrowserWindow } from 'electron';

let mainWindow: BrowserWindow | null = null;

const createWindow = () => {
mainWindow = new BrowserWindow({
backgroundColor: '#1a3d53',
width: 400,
height: 600,
width: 440,
height: 730,
show: false,
frame: false,
fullscreenable: false,
Expand Down
28 changes: 16 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
"main": "build/electron/main.js",
"homepage": "./",
"dependencies": {
"@chakra-ui/icons": "^1.0.4",
"@chakra-ui/react": "^1.2.1",
"@emotion/react": "^11.1.4",
"@emotion/styled": "^11.0.0",
"@chakra-ui/icons": "^1.0.5",
"@chakra-ui/react": "^1.3.2",
"@emotion/react": "^11.1.5",
"@emotion/styled": "^11.1.5",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.6.3",
"concurrently": "^5.3.0",
"copyfiles": "^2.4.1",
"dexie": "^3.1.0-alpha.6",
"dexie-react-hooks": "^1.0.2",
"electron-is-dev": "^1.2.0",
"emoji-picker-react": "^3.4.2",
"framer-motion": "^3.2.2-rc.1",
"framer-motion": "^3.3.0",
"node-schedule": "^2.0.0",
"react": "^17.0.1",
"react-color": "^2.19.3",
"react-datepicker": "^3.4.1",
"react-dom": "^17.0.1",
"react-hook-form": "^6.14.2",
"react-hook-form": "^6.15.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"react-scripts": "4.0.2",
"typescript": "^4.0.3",
"wait-on": "^5.2.1",
"web-vitals": "^1.1.0"
Expand Down Expand Up @@ -66,12 +68,14 @@
"@electron-forge/maker-squirrel": "^6.0.0-beta.54",
"@electron-forge/maker-zip": "^6.0.0-beta.54",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.22",
"@types/react": "^17.0.0",
"@types/node": "^14.14.25",
"@types/node-schedule": "^1.3.1",
"@types/react": "^17.0.1",
"@types/react-color": "^3.0.4",
"@types/react-datepicker": "^3.1.3",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"electron": "^11.2.1"
"electron": "^11.2.3"
},
"config": {
"forge": {
Expand Down
25 changes: 25 additions & 0 deletions src/JobScheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import schedule from 'node-schedule';
import { Routine } from './database/RoutinesService';

export class JobScheduler {
routine: Routine;

constructor(routine: Routine) {
this.routine = routine;
}

schedule(): schedule.Job | null {
if (!this.routine.scheduleAt) {
return null;
}

return schedule.scheduleJob(this.routine.scheduleAt, () => this.notify());
}

private notify() {
return new Notification(`${this.routine.emoji ?? ''} ${this.routine.title}`, {
icon: '/assets/icon@2x.png',
body: `It's time for your ${this.routine.title} routine!.`,
});
}
}
139 changes: 139 additions & 0 deletions src/NotificationScheduler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import React from 'react';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import { Controller, useFormContext } from 'react-hook-form';
import { Checkbox, Flex, Input, Radio, RadioGroup, Stack, Text } from '@chakra-ui/react';
import { ScheduleFrequency } from './RoutineCreator';

const TimePickerInput = React.forwardRef<HTMLInputElement>((props: any, ref) => {
return <Input {...props} placeholder="Pick a time" ref={ref} width="140px" autoComplete="off" />;
});

type Day = {
value: string; // 0-6
label: string; // Mo Tu We ...
};
const days: Day[] = [
{ value: '1', label: 'Mo' },
{ value: '2', label: 'Tu' },
{ value: '3', label: 'We' },
{ value: '4', label: 'Th' },
{ value: '5', label: 'Fr' },
{ value: '6', label: 'Sa' },
{ value: '0', label: 'Su' },
];

export const NotificationScheduler: React.FC = () => {
const { register, watch, control } = useFormContext();
const { scheduleFrequency, timeOfDay } = watch(['scheduleFrequency', 'timeOfDay']);
const isDaily = scheduleFrequency === ScheduleFrequency.Daily;
const isWeekly = scheduleFrequency === ScheduleFrequency.Weekly;
const isCustomTime = timeOfDay === 'custom';

return (
<Flex direction="column">
<Text mb={2} fontWeight="bold">
How often do you want to be reminded?
</Text>
<Flex direction="column" mb={4}>
<RadioGroup name="scheduleFrequency" defaultValue={ScheduleFrequency.Daily}>
<Stack>
<Radio
value={ScheduleFrequency.Daily}
ref={register}
cursor="pointer"
colorScheme="cyan"
>
Daily On
</Radio>
{/* If the choice is ScheduleFrequency.Daily render the Checkboxes */}
{isDaily ? (
<Stack spacing={3} direction="row" px={6}>
{days.map((item) => (
<Checkbox
name="scheduleDays"
key={item.value}
value={item.value}
colorScheme="green"
ref={register}
defaultChecked={parseInt(item.value) > 0 && parseInt(item.value) < 6}
>
{item.label}
</Checkbox>
))}
</Stack>
) : null}
<Radio
value={ScheduleFrequency.Weekly}
ref={register}
colorScheme="cyan"
cursor="pointer"
>
Once a week on...
</Radio>
{isWeekly ? (
<Controller
as={
<RadioGroup name="scheduleDays">
<Stack spacing={3} direction="row" px={6}>
{days.map(({ value, label }) => (
<Radio key={value} value={value} colorScheme="green" ref={register()}>
{label}
</Radio>
))}
</Stack>
</RadioGroup>
}
name="scheduleDays"
control={control}
defaultValue="1"
/>
) : null}
</Stack>
</RadioGroup>
</Flex>
<Text mb={2} fontWeight="bold">
At what time of day?
</Text>
<Flex direction="column">
<RadioGroup>
<Stack direction="column">
<Radio name="timeOfDay" value="9:00" ref={register} colorScheme="cyan" defaultChecked>
Beginning of the day at 9:00
</Radio>
<Radio name="timeOfDay" value="17:00" colorScheme="cyan" ref={register}>
End of the day at 17:00
</Radio>
<Radio name="timeOfDay" value="custom" colorScheme="cyan" ref={register}>
Pick a time
</Radio>
{isCustomTime ? (
<Flex px={6}>
<Controller
name="customTime"
control={control}
defaultValue={new Date()}
render={(props) => (
<DatePicker
ref={register}
name={props.name}
selected={props.value}
onChange={(date: Date) => props.onChange(date)}
showTimeSelect
showTimeSelectOnly
timeIntervals={15}
timeCaption="Time"
dateFormat="HH:mm"
timeFormat="HH:mm"
customInput={<TimePickerInput {...props} ref={register} />}
/>
)}
/>
</Flex>
) : null}
</Stack>
</RadioGroup>
</Flex>
</Flex>
);
};
Loading