Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
fix: app crashs onFinish in macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 27, 2020
1 parent c768e7c commit 160f7cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 83 deletions.
29 changes: 1 addition & 28 deletions src/renderer/components/Setting/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import React from 'react';
import { TimerActionTypes, TimerState } from '../Timer/action';
import styled from 'styled-components';
import { Button, Col, Icon, message, notification, Popconfirm, Row, Slider, Switch } from 'antd';
import { deleteAllUserData, exportDBData } from '../../monitor/sessionManager';
import { writeFile } from 'fs';
import { deleteAllUserData } from '../../monitor/sessionManager';
import { shell, remote, app } from 'electron';
import { promisify } from 'util';
import { DistractingListModalButton } from './DistractingList';
import { isShallowEqualByKeys } from '../../utils';
import pkg from '../../../../package.json';

const dialog = remote.dialog;

const Container = styled.div`
padding: 12px 36px;
`;
Expand Down Expand Up @@ -148,25 +144,6 @@ export const Setting: React.FunctionComponent<Props> = React.memo(
});
}

async function onExportingData() {
const { canceled, filePath } = await dialog.showSaveDialog(remote.getCurrentWindow(), {
title: 'Pomodoro Data Export',
defaultPath: 'pomodoroDB.dat',
filters: [
{
name: 'Data File',
extensions: ['dat'],
},
],
});

if (!canceled && filePath) {
const data = await exportDBData();
await promisify(writeFile)(filePath, JSON.stringify(data), { encoding: 'utf-8' });
message.success('Data Exported');
}
}

function openIssuePage() {
shell.openExternal('https://github.com/zxch3n/PomodoroLogger/issues/new');
}
Expand Down Expand Up @@ -256,10 +233,6 @@ export const Setting: React.FunctionComponent<Props> = React.memo(
/>

<h4>Data Management</h4>
<ButtonWrapper>
<Button onClick={onExportingData}>Export Data</Button>
<br />
</ButtonWrapper>
<ButtonWrapper>
<Popconfirm title={'Sure to delete?'} onConfirm={onDeleteData}>
<Button type="danger">Delete All Data</Button>
Expand Down
1 change: 0 additions & 1 deletion src/renderer/components/Timer/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ class Timer extends Component<Props, State> {
// Resting session
await this.props.timerFinished();
} else {
restartDBWorkers();
this.stagedSession.spentTimeInHour += this.extendedTimeInMinute / 60;
this.extendedTimeInMinute = 0;
if (this.props.timer.boardId !== undefined) {
Expand Down
48 changes: 22 additions & 26 deletions src/renderer/components/Visualization/ProjectTrend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import { PomodoroRecord } from '../../monitor/type';
import { workers } from '../../workers';

const Container = styled.div`
position: relative;
transition: background-color 1s;
background-color: rgba(255, 255, 223, 0);
:hover {
background-color: rgba(23, 43, 223, 0.1);
position: relative;
transition: background-color 1s;
background-color: rgba(255, 255, 223, 0);
:hover {
background-color: rgba(23, 43, 223, 0.1);
h1 {
opacity: 1;
}
}
h1 {
opacity: 1;
color: #0074e9;
opacity: 0;
transition: opacity 0.5s;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
h1 {
color: #0074E9;
opacity: 0;
transition: opacity 0.5s;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
`;

const NoHover = styled.div``;
Expand All @@ -39,19 +39,15 @@ interface Props {
}

export const ProjectTrend: React.FC<Props> = (props: Props) => {
const {hoverEffect=false} = props;
const { hoverEffect = false } = props;
const ref = useRef();
const C = hoverEffect? Container : NoHover;
const C = hoverEffect ? Container : NoHover;
return (
// @ts-ignore
<C ref={ref}>
{props.data.length > 0 ? (
<>
{
hoverEffect? (
<h1>TREND</h1>
) : undefined
}
{hoverEffect ? <h1>TREND</h1> : undefined}
<Trend
width={props.width}
height={props.height}
Expand All @@ -61,7 +57,7 @@ export const ProjectTrend: React.FC<Props> = (props: Props) => {
radius={10.1}
strokeWidth={1.5}
strokeLinecap={'butt'}
maxData={16}
maxDada={16}
/>
</>
) : undefined}
Expand Down
40 changes: 12 additions & 28 deletions src/renderer/monitor/sessionManager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import dbs from '../dbs';
import { promisify } from 'util';
import { workers } from '../workers';
import { PomodoroRecord } from './type';
import { dbBaseDir } from '../../config';
import * as fs from 'fs';
import nedb from 'nedb';

const [find, insert, remove] = [dbs.sessionDB.find, dbs.sessionDB.insert, dbs.sessionDB.remove].map(
m => promisify(m.bind(dbs.sessionDB))
);

const dbWorkers = workers.dbWorkers;
export function renameIllegalName(record: PomodoroRecord) {
for (const app in record.apps) {
const appRow = record.apps[app];
Expand Down Expand Up @@ -63,31 +59,34 @@ export async function addSession(record: PomodoroRecord) {
// TODO: invoke ML inference
}
// @ts-ignore
await insert(record).catch(err => console.error(err));
await dbWorkers.sessionDB.insert(record).catch((err) => console.error(err));
}

export async function removeSession(startTime: number) {
// @ts-ignore
await remove({ startTime });
await dbWorkers.sessionDB.remove({ startTime });
}

export async function getTodaySessions(): Promise<PomodoroRecord[]> {
const todayStartTime = new Date(new Date().toDateString()).getTime();
// @ts-ignore
return ((await find({ startTime: { $gt: todayStartTime } })) as unknown) as PomodoroRecord[];
const ans = ((await dbWorkers.sessionDB.find(
{ startTime: { $gt: todayStartTime } },
{}
)) as unknown) as PomodoroRecord[];
return ans;
}

export async function getAllSession(): Promise<PomodoroRecord[]> {
// @ts-ignore
return (await find({})) as PomodoroRecord[];
return (await dbWorkers.sessionDB.find({}, {})) as PomodoroRecord[];
}

export async function loadDB(path: string): Promise<nedb> {
const db = new nedb({ filename: path });
return new Promise((resolve, reject) => {
let times = 0;
const load = () => {
db.loadDatabase(err => {
db.loadDatabase((err) => {
if (!err) {
resolve(db);
return;
Expand All @@ -111,7 +110,7 @@ export function loadDBSync(path: string) {
const db = new nedb({ filename: path });
let times = 0;
const load = () => {
db.loadDatabase(err => {
db.loadDatabase((err) => {
if (!err) {
return;
}
Expand Down Expand Up @@ -151,21 +150,6 @@ export async function deleteAllUserData() {
}
}

/* istanbul ignore next */
export async function exportDBData() {
const [projectDB, sessionDB, settingDB] = await Promise.all([
promisify(dbs.projectDB.find.bind(dbs.projectDB, {}, {}))(),
promisify(dbs.sessionDB.find.bind(dbs.sessionDB, {}, {}))(),
promisify(dbs.settingDB.find.bind(dbs.settingDB, {}, {}))()
]);

return {
projectDB,
sessionDB,
settingDB
};
}

export function getIndexToTitleApp(record: PomodoroRecord): [string, string][] {
const indexToTitle: [string, string][] = [];
for (const app in record.apps) {
Expand Down

0 comments on commit 160f7cd

Please sign in to comment.