Skip to content

Commit

Permalink
fix: fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvay committed May 4, 2024
1 parent d445fd6 commit af72d57
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 1,328 deletions.
2 changes: 1 addition & 1 deletion electron/main/services/wallpaper-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Subject } from 'rxjs';
import { omit } from 'lodash';
import { StaticWallpaperEventArg } from '../../../cross/interface';

const DEBUG = !app.isPackaged && false;
const DEBUG = !app.isPackaged && true;

const windowsMap: Map<number, BrowserWindow> = new Map();

Expand Down
11 changes: 10 additions & 1 deletion electron/main/services/wallpaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ export async function setWallpaper(
detachWallpaperWin();
} else {
const extList = typeExtMap.get(rule.wallpaperType) as string[];
let paths = readRuleFilePaths(rule, extList);
let paths: string[] = [];
switch (rule.type) {
case ChangeType.AutoChange:
paths = readRuleFilePaths(rule, extList);
break;
case ChangeType.Fixed:
paths = [filePath];
break;
}

await setStaticWallpaper(
{
path: filePath,
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workpaper",
"version": "0.1.0",
"version": "0.1.1",
"description": "A wallpaper schedule",
"author": {
"name": "Jarvay",
Expand Down Expand Up @@ -36,15 +36,13 @@
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.4",
"ahooks": "^3.7.8",
"animate.css": "^4.1.1",
"antd": "^5.10.2",
"autoprefixer": "^10.4.16",
"axios": "^1.6.8",
"dayjs": "^1.11.10",
"electron": "^30.0.0",
"electron-builder": "^24.6.3",
"electron-devtools-installer": "^3.2.0",
"i": "^0.3.7",
"i18next": "^23.6.0",
"less": "^4.2.0",
"less-loader": "^11.1.3",
Expand All @@ -53,7 +51,6 @@
"mime": "^4.0.1",
"mitt": "^3.0.1",
"node-schedule": "^2.1.1",
"npm": "^10.6.0",
"postcss": "^8.4.31",
"postcss-import": "^16.1.0",
"prettier": "^3.0.3",
Expand Down
35 changes: 17 additions & 18 deletions src/pages/rule/components/WallpaperRuleModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
const WallpaperRuleModal: React.FC<
ModalFormProps<Rule> & { weekdayId?: string | number }
> = (props) => {
const [form] = Form.useForm();
const [form] = Form.useForm<Rule & Record<string, any>>();

const { t } = useTranslation();

Expand Down Expand Up @@ -81,14 +81,10 @@ const WallpaperRuleModal: React.FC<
});
}
}
}, [props.open, props.values]);
}, [props.open]);

function renderPathItem(
paths: string[],
index: number,
wallpaperType: WallpaperType,
) {
if (!paths?.[index]) return null;
function renderPathItem(path: string, wallpaperType: WallpaperType) {
if (!path) return null;
const size = 72;
let children = null;
switch (wallpaperType) {
Expand All @@ -100,14 +96,14 @@ const WallpaperRuleModal: React.FC<
}}
width={size}
height={size}
src={`file://${paths[index]}`}
src={`file://${path}`}
/>
);
break;
case WallpaperType.Video:
children = (
<video
src={`file://${paths[index]}`}
src={`file://${path}`}
style={{
width: `${size}px`,
maxHeight: `${size}px`,
Expand Down Expand Up @@ -135,9 +131,9 @@ const WallpaperRuleModal: React.FC<
switch (type) {
case ChangeType.Fixed:
return (
<Form.Item noStyle dependencies={['paths']}>
{({ getFieldsValue }) => {
const { paths } = getFieldsValue() as Rule;
<Form.Item noStyle dependencies={['paths', 'type']}>
{({ getFieldValue }) => {
const paths = getFieldValue('paths');
return (
<Form.List
name="paths"
Expand Down Expand Up @@ -167,7 +163,7 @@ const WallpaperRuleModal: React.FC<
noStyle
>
<Space>
{renderPathItem(paths, index, wallpaperType)}
{renderPathItem(paths?.[index], wallpaperType)}

<Button
type="primary"
Expand Down Expand Up @@ -330,7 +326,7 @@ const WallpaperRuleModal: React.FC<
onChange={(e) => {
form.setFieldsValue({
path: undefined,
paths: [undefined],
paths: [''],
});
}}
options={[
Expand All @@ -355,7 +351,10 @@ const WallpaperRuleModal: React.FC<
onChange={(e) => {
form.setFieldsValue({
path: undefined,
paths: [undefined],
paths: [''],
direction: WallpaperDirection.Horizontal,
isRandom: false,
screenRandom: false,
});
}}
options={[
Expand Down Expand Up @@ -432,8 +431,8 @@ const WallpaperRuleModal: React.FC<
)}

<Form.Item noStyle dependencies={['isRandom']}>
{({ getFieldsValue }) => {
const { isRandom } = getFieldsValue() as Rule;
{({ getFieldValue }) => {
const isRandom = getFieldValue('isRandom');
const isVertical =
WallpaperDirection.Vertical === direction;
return (
Expand Down
13 changes: 10 additions & 3 deletions src/pages/rule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ const RuleIndex: React.FC = () => {
const [updateModalOpen, setUpdateModalOpen] = useState(false);
const [currentRow, setCurrentRow] = useState<Rule>();
const [weekday, setWeekday] = useState<Weekday>();
const [loading, setLoading] = useState(false);

const navigate = useNavigate();
const { id: weekdayId } = useParams();

const { t } = useTranslation();

async function refresh() {
setLoading(true);
const rules = await ruleService.get();
setDataSource(rules.filter((rule) => rule.weekdayId === weekdayId));
setLoading(false);
}

useMount(async () => {
Expand Down Expand Up @@ -172,7 +175,10 @@ const RuleIndex: React.FC = () => {
dataIndex: 'interval',
width: 120,
render: (value, record) => {
if (record.wallpaperType === WallpaperType.Video) {
const hideInterval =
record.wallpaperType === WallpaperType.Video ||
record.type === ChangeType.Fixed;
if (hideInterval) {
return '-';
}
return value || '-';
Expand Down Expand Up @@ -271,6 +277,7 @@ const RuleIndex: React.FC = () => {
rowKey="id"
columns={columns}
dataSource={dataSource}
loading={loading}
/>

<WallpaperRule
Expand All @@ -280,9 +287,9 @@ const RuleIndex: React.FC = () => {
}}
values={currentRow}
mode={FormMode.Update}
onChange={() => {
onChange={async () => {
setUpdateModalOpen(false);
refresh();
await refresh();
}}
/>
</Space>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useRef, useState } from 'react';
import styles from '@/pages/wallpaper/static/index.module.less';
import { ipcRenderer } from 'electron';
import { Events } from '../../../../../cross/enums';
import { Events } from '../../../../../../cross/enums';
import { Carousel, CarouselProps } from 'antd';
import { CarouselRef } from 'antd/es/carousel';

Expand All @@ -18,7 +18,7 @@ export interface ImageCarouselProps {
}

const SPEED = 1500;
const ImageCarousel: React.FC<ImageCarouselProps> = (props) => {
const HorizontalImageCarousel: React.FC<ImageCarouselProps> = (props) => {
const { carouselIndex } = props;

const carouselRef = useRef<CarouselRef>();
Expand Down Expand Up @@ -59,4 +59,4 @@ const ImageCarousel: React.FC<ImageCarouselProps> = (props) => {
);
};

export default ImageCarousel;
export default HorizontalImageCarousel;
11 changes: 6 additions & 5 deletions src/pages/wallpaper/static/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import styles from './index.module.less';
import { Carousel, Col, Row } from 'antd';
import { cloneDeep, isEqual, omit, range, shuffle } from 'lodash';
import { CarouselRef } from 'antd/es/carousel';
import ImageCarousel from '@/pages/wallpaper/static/ImageCarousel';
import HorizontalImageCarousel from '@/pages/wallpaper/static/components/HorizontalImageCarousel';

const SPEED = 1500;
const SPEED = 1200;

interface HorizontalIndex {
current: number;
Expand Down Expand Up @@ -48,6 +48,7 @@ const StaticWallpaper: React.FC = () => {
) => void = (_, arg: StaticWallpaperEventArg) => {
const a = cloneDeep(omit(arg, 'path'));
const b = cloneDeep(omit(staticWallpaperArg, 'path'));

if (!isEqual(a, b)) {
setStaticWallpaperArg(arg);
}
Expand Down Expand Up @@ -91,7 +92,7 @@ const StaticWallpaper: React.FC = () => {
setTimeout(() => {
ipcRenderer.send(Events.StaticWallpaperLoaded, Number(displayId));
setReadyToShow(true);
}, 500);
}, 200);
}
}, [loadedColumnIndexSet, staticWallpaperArg?.rule.column]);

Expand All @@ -103,7 +104,7 @@ const StaticWallpaper: React.FC = () => {
switch (staticWallpaperArg?.rule.direction) {
case WallpaperDirection.Horizontal:
children = (
<ImageCarousel
<HorizontalImageCarousel
paths={staticWallpaperArg?.paths}
imgStyle={{
objectFit: settings?.webScaleMode,
Expand Down Expand Up @@ -154,7 +155,7 @@ const StaticWallpaper: React.FC = () => {
} catch (e) {
console.warn(e);
}
}, 100);
}, 150);
}}
>
{shuffledCarouselPaths[index].map((item, i) => {
Expand Down
11 changes: 11 additions & 0 deletions src/services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export abstract class BaseService<
return (await this.getRows(this.getKeyInDB())) || [];
}

async beforeCreate(item: T): Promise<T> {
return item;
}

async create(item: T) {
item = await this.beforeCreate(item);
const list = await this.get();

const md5 = new Md5();
Expand All @@ -33,7 +38,13 @@ export abstract class BaseService<
return this.save(list);
}

async beforeUpdate(item: T): Promise<T> {
return item;
}

async update(item: T) {
item = await this.beforeUpdate(item);

const list = await this.get();
list.forEach((i, index) => {
if (i.id === item.id) {
Expand Down
21 changes: 20 additions & 1 deletion src/services/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { timeToSeconds } from '../../cross/date';
import { Rule } from '../../cross/interface';
import { BaseService } from '@/services/base';
import { ipcRenderer } from 'electron';
import { Events } from '../../cross/enums';
import { ChangeType, Events, WallpaperDirection } from '../../cross/enums';

export class RuleService extends BaseService<'rules', Rule> {
getKeyInDB(): 'rules' {
Expand All @@ -14,6 +14,25 @@ export class RuleService extends BaseService<'rules', Rule> {
await ipcRenderer.invoke(Events.ResetSchedule);
}

async beforeUpsert(item: Rule): Promise<Rule> {
if (item.type === ChangeType.Fixed) {
item.direction = WallpaperDirection.Horizontal;
item.isRandom = false;
item.screenRandom = false;
item.path = '';
}

return item;
}

async beforeCreate(item: Rule): Promise<Rule> {
return await this.beforeUpsert(item);
}

async beforeUpdate(item: Rule): Promise<Rule> {
return await this.beforeUpsert(item);
}

async isConflicts(
start: string,
end: string,
Expand Down
Loading

0 comments on commit af72d57

Please sign in to comment.