提交 a0568d2e 编写于 作者: AndroidLeaves's avatar AndroidLeaves

add 每日安排

上级 184fd47c
......@@ -27,9 +27,14 @@ const { BaseWidget } = require(dependencyFileName);
class Widget extends BaseWidget {
defaultPreference = {
reminderColors: {
0: '#333333',
1: '#ff002b',
5: '#ff7f51',
9: '#f6aa1c',
}
};
constructor(scriptName) {
super(scriptName);
this.changeBgMode2OnLineBg(
......@@ -113,44 +118,80 @@ class Widget extends BaseWidget {
// ------------
leftStack.addSpacer(10);
let textSpan = leftStack.addText(`已完成:${finishedCount}项`);
textSpan.textColor = new Color('#333');
textSpan.font = Font.regularSystemFont(14);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ------------
leftStack.addSpacer(4);
textSpan = leftStack.addText(`未完成:${unfinishedCount}项`);
textSpan.textColor = new Color('#333');
textSpan.font = Font.regularSystemFont(14);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ------------
leftStack.addSpacer(4);
textSpan = leftStack.addText(`完成比例:${Math.floor(finishedCount / totalCount)}%`);
textSpan.textColor = new Color('#333');
textSpan.font = Font.regularSystemFont(14);
textSpan = leftStack.addText(`完成率:${Math.floor((finishedCount / totalCount) * 100)}%`);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ========================================
const rightStackWidth = width * 0.62;
const rightStack = contentStack.addStack();
rightStack.size = new Size(rightStackWidth, height);
rightStack.layoutVertically();
const perHeight = height / 7;
rightStack.addSpacer(12);
const perHeight = height / 6;
rightStack.addSpacer(10);
const list = unfinishedList.slice(0, unfinishedList.length > 4 ? 4 : unfinishedList.length);
list.forEach(rd => {
let rdStack = rightStack.addStack();
rdStack.url = 'x-apple-reminderkit://';
rdStack.size = new Size(rightStackWidth - 20, perHeight);
rdStack.setPadding(0, 8, 0, 8);
rdStack.centerAlignContent();
rdStack.backgroundColor = new Color('#000000', 0.07);
rdStack.cornerRadius = 4;
textSpan = rdStack.addText(`@ ${rd.title}`)
textSpan.textColor = new Color(`${rd.calendar.color.hex}`);
textSpan.font = Font.regularSystemFont(14);
if (list.length !== 0) {
const completeIcoStack = rightStack.addStack();
completeIcoStack.addSpacer();
img = await this.getImageByUrl('https://s3.uuu.ovh/imgs/2022/12/11/3b32161d338eaf49.png');
imgSpan = completeIcoStack.addImage(img);
completeIcoStack.addSpacer();
imgSpan.imageSize = new Size(rightStackWidth / 3, rightStackWidth / 3);
//
rightStack.addSpacer(10);
let completeTipsStack = rightStack.addStack();
completeTipsStack.addSpacer();
textSpan = completeTipsStack.addText(`已完成今天所有任务安排`);
textSpan.textColor = new Color('#666');
textSpan.font = Font.systemFont(14);
textSpan.lineLimit = 1;
completeTipsStack.addSpacer();
//
rightStack.addSpacer(4);
completeTipsStack = rightStack.addStack();
completeTipsStack.addSpacer();
textSpan = completeTipsStack.addText(`可以进行规划明天事项了`);
textSpan.textColor = new Color('#666');
textSpan.font = Font.systemFont(14);
textSpan.lineLimit = 1;
rdStack.addSpacer();
rightStack.addSpacer(12);
});
completeTipsStack.addSpacer();
//
rightStack.addSpacer(4);
completeTipsStack = rightStack.addStack();
completeTipsStack.addSpacer();
textSpan = completeTipsStack.addText(`好的坚持是成功的开端`);
textSpan.textColor = new Color('#666');
textSpan.font = Font.systemFont(14);
textSpan.lineLimit = 1;
completeTipsStack.addSpacer();
} else {
list.forEach(rd => {
let rdStack = rightStack.addStack();
rdStack.url = 'x-apple-reminderkit://';
rdStack.size = new Size(rightStackWidth - 20, perHeight);
rdStack.setPadding(0, 8, 0, 8);
rdStack.centerAlignContent();
rdStack.backgroundColor = new Color(rd.isOverdue ? `${rd.color}` : '#000000', 0.06);
rdStack.cornerRadius = 4;
textSpan = rdStack.addText(`@ ${rd.title}`)
textSpan.textColor = new Color(`${rd.color}`, 0.8);
textSpan.font = Font.semiboldSystemFont(13);
textSpan.lineLimit = 1;
rdStack.addSpacer();
rightStack.addSpacer(10);
});
}
return widget;
}
......@@ -166,7 +207,6 @@ class Widget extends BaseWidget {
// title, notes,isCompleted, isOverdue,
// priority:0,1,5,9,
// calendar.color.hex
const rd = new Reminder();
const calendar = await Calendar.defaultForReminders();
const originalRdList = await Reminder.scheduled([calendar]);
const reminderList = originalRdList.map(item => ({
......@@ -175,6 +215,7 @@ class Widget extends BaseWidget {
isCompleted: item.isCompleted,
isOverdue: item.isOverdue,
priority: item.priority == 0 ? 10 : item.priority,
color: this.defaultPreference.reminderColors[item.priority],
calendar: item.calendar
}));
const unfinishedList = reminderList.filter(reminder => !reminder.isCompleted);
......
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: user-astronaut;
/**
* Author:LSP
* Date:2022-12-11
*/
// -------------------------------------------------------
// 是否是开发环境,配合手机端调试使用,正式发布设置为false
const isDev = false;
console.log(`开发环境 👉👉👉👉👉 ${isDev ? 'DEV' : 'RELEASE'}`);
console.log(`----------------------------------------`);
const remoteRoot = 'https://gitcode.net/enoyee/scriptable/-/raw/master/';
// 依赖包目录
const fm = FileManager.local();
const rootDir = fm.documentsDirectory();
const cacheDir = fm.joinPath(rootDir, 'LSP');
const dependencyFileName = isDev ? "_LSP.js" : `${cacheDir}/_LSP.js`;
// 下载依赖包
await downloadLSPDependency();
// -------------------------------------------------------
if (typeof require === 'undefined') require = importModule
// 引入相关方法
const { BaseWidget } = require(dependencyFileName);
// @定义小组件
class Widget extends BaseWidget {
defaultPreference = {
reminderColors: {
0: '#333333',
1: '#ff002b',
5: '#ff7f51',
9: '#f6aa1c',
}
};
constructor(scriptName) {
super(scriptName);
this.changeBgMode2OnLineBg(
'https://www.toptal.com/designers/subtlepatterns/uploads/double-bubble-outline.png',
);
}
async getAppViewOptions() {
return {
widgetProvider: {
small: false, // 是否提供小号组件
medium: true, // 是否提供中号组件
large: true, // 是否提供大号组件
},
// 预览界面的组件设置item
settingItems: [
{
name: 'avatar',
label: '头像设置',
type: 'cell',
icon: { name: 'scribble', color: '#9d4edd', },
needLoading: false,
alert: {
title: '头像设置',
options: [
{
key: 'avatar',
hint: '请输入头像链接',
}
]
},
showDesc: false
},
],
};
}
async render({ widgetSetting, family }) {
let widget;
switch (family) {
case 'medium':
widget = await this.provideMediumWidget();
break;
case 'large':
widget = await this.provideLargeWidget();
break;
}
widget.setPadding(6, 12, 6, 12);
return widget;
}
/**
* 中型组件
* @returns
*/
async provideMediumWidget() {
const reminderObj = await this.loadReminderList();
const { finishedList, unfinishedList } = reminderObj;
const finishedCount = finishedList.length;
const unfinishedCount = unfinishedList.length;
const totalCount = finishedCount + unfinishedCount;
// ========================================
const widget = new ListWidget();
const { width, height } = this.getWidgetSize('中号');
const contentStack = widget.addStack();
contentStack.layoutHorizontally();
// ========================================
const leftStack = contentStack.addStack();
leftStack.layoutVertically();
const leftStackWidth = width * 0.38;
leftStack.size = new Size(leftStackWidth, height);
leftStack.setPadding(0, 16, 0, 10);
const avatarUrl = 'https://s3.uuu.ovh/imgs/2022/12/09/4c8c4d09b6542713.png';
// ------------
const avatarStack = leftStack.addStack();
let img = await this.getImageByUrl(avatarUrl);
let imgSpan = avatarStack.addImage(img);
avatarStack.addSpacer();
imgSpan.imageSize = new Size(leftStackWidth / 2, leftStackWidth / 2);
// ------------
leftStack.addSpacer(10);
let textSpan = leftStack.addText(`已完成:${finishedCount}项`);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ------------
leftStack.addSpacer(4);
textSpan = leftStack.addText(`未完成:${unfinishedCount}项`);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ------------
leftStack.addSpacer(4);
textSpan = leftStack.addText(`完成率:${Math.floor((finishedCount / totalCount) * 100)}%`);
textSpan.textColor = new Color('#444');
textSpan.font = Font.mediumSystemFont(14);
textSpan.lineLimit = 1;
// ========================================
const rightStackWidth = width * 0.62;
const rightStack = contentStack.addStack();
rightStack.size = new Size(rightStackWidth, height);
rightStack.layoutVertically();
const perHeight = height / 6;
const list = unfinishedList.slice(0, unfinishedList.length > 4 ? 4 : unfinishedList.length);
rightStack.addSpacer(10);
if (list.length === 0) {
rightStack.url = 'x-apple-reminderkit://';
const completeIcoStack = rightStack.addStack();
completeIcoStack.addSpacer();
img = await this.getImageByUrl('https://s3.uuu.ovh/imgs/2022/12/11/13440f7a07d814d3.png');
rightStack.backgroundImage = img;
['', '', '', ''].forEach(_ => {
let rdStack = rightStack.addStack();
rdStack.url = 'x-apple-reminderkit://';
rdStack.size = new Size(rightStackWidth - 20, perHeight);
rdStack.backgroundColor = new Color('#19a5ff', 0.1);
rdStack.cornerRadius = 4;
rdStack.addSpacer();
rightStack.addSpacer(10);
});
} else {
list.forEach(rd => {
let rdStack = rightStack.addStack();
rdStack.url = 'x-apple-reminderkit://';
rdStack.size = new Size(rightStackWidth - 20, perHeight);
rdStack.setPadding(0, 8, 0, 8);
rdStack.centerAlignContent();
rdStack.backgroundColor = new Color(rd.isOverdue ? `${rd.color}` : '#000000', 0.06);
rdStack.cornerRadius = 4;
textSpan = rdStack.addText(`@ ${rd.title}`)
textSpan.textColor = new Color(`${rd.color}`, 0.8);
textSpan.font = Font.semiboldSystemFont(13);
textSpan.lineLimit = 1;
rdStack.addSpacer();
rightStack.addSpacer(10);
});
}
return widget;
}
/**
* 大型组件
* @returns
*/
async provideLargeWidget() {
}
// --------------------------NET START--------------------------
async loadReminderList() {
// title, notes,isCompleted, isOverdue,
// priority:0,1,5,9,
// calendar.color.hex
const calendar = await Calendar.defaultForReminders();
const originalRdList = await Reminder.scheduled([calendar]);
const reminderList = originalRdList.map(item => ({
title: item.title,
notes: item.notes,
isCompleted: item.isCompleted,
isOverdue: item.isOverdue,
priority: item.priority == 0 ? 10 : item.priority,
color: this.defaultPreference.reminderColors[item.priority],
calendar: item.calendar
}));
const unfinishedList = reminderList.filter(reminder => !reminder.isCompleted);
unfinishedList.sort(function (a, b) { return a.priority - b.priority });
const finishedList = reminderList.filter(reminder => reminder.isCompleted);
console.log(`总共:${originalRdList.length}`);
return { finishedList, unfinishedList };
}
// --------------------------NET END--------------------------
}
await new Widget(Script.name()).run();
// =================================================================================
// =================================================================================
async function downloadLSPDependency() {
let fm = FileManager.local();
const dependencyURL = `${remoteRoot}_LSP.js`;
if (isDev) {
const iCloudPath = FileManager.iCloud().documentsDirectory();
const localIcloudDependencyExit = fm.isFileStoredIniCloud(`${iCloudPath}/_LSP.js`);
const localDependencyExit = fm.fileExists(`${rootDir}/_LSP.js`);
const fileExist = localIcloudDependencyExit || localDependencyExit;
console.log(`🚀 DEV开发依赖文件${fileExist ? '已存在 ✅' : '不存在 🚫'}`);
if (!fileExist) {
console.log(`🤖 DEV 开始下载依赖~`);
await downloadFile2Scriptable('_LSP', dependencyURL);
}
return
}
//////////////////////////////////////////////////////////
console.log(`----------------------------------------`);
const remoteDependencyExit = fm.fileExists(`${cacheDir}/_LSP.js`);
console.log(`🚀 RELEASE依赖文件${remoteDependencyExit ? '已存在 ✅' : '不存在 🚫'}`);
console.log(`----------------------------------------`);
// ------------------------------
if (!remoteDependencyExit) { // 下载依赖
// 创建根目录
if (!fm.fileExists(cacheDir)) {
fm.createDirectory(cacheDir, true);
}
// 下载
console.log('🤖 RELEASE开始下载依赖~');
console.log(`----------------------------------------`);
const req = new Request(dependencyURL);
const moduleJs = await req.load();
if (moduleJs) {
fm.write(fm.joinPath(cacheDir, '/_LSP.js'), moduleJs);
console.log('✅ LSP远程依赖环境下载成功!');
console.log(`----------------------------------------`);
} else {
console.error('🚫 获取依赖环境脚本失败,请重试!');
console.log(`----------------------------------------`);
}
}
}
/**
* 获取保存的文件名
* @param {*} fileName
* @returns
*/
function getSaveFileName(fileName) {
const hasSuffix = fileName.lastIndexOf(".") + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
};
/**
* 保存文件到Scriptable软件目录,app可看到
* @param {*} fileName
* @param {*} content
* @returns
*/
function saveFile2Scriptable(fileName, content) {
try {
const fm = FileManager.iCloud();
let fileSimpleName = getSaveFileName(fileName);
const filePath = fm.joinPath(fm.documentsDirectory(), fileSimpleName);
fm.writeString(filePath, content);
return true;
} catch (error) {
return false;
}
};
/**
* 下载js文件到Scriptable软件目录
* @param {*} moduleName 名称
* @param {*} url 在线地址
* @returns
*/
async function downloadFile2Scriptable(moduleName, url) {
const req = new Request(url);
const content = await req.loadString();
return saveFile2Scriptable(`${moduleName}`, content);
};
// =================================================================================
// =================================================================================
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册