Skip to content

Commit

Permalink
Remove auto update, add manual update
Browse files Browse the repository at this point in the history
  • Loading branch information
SnekNOTSnake committed Dec 1, 2022
1 parent 0b6a82d commit 068f6bc
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 39 deletions.
10 changes: 6 additions & 4 deletions Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ interface PopulatedSchedule {
sat: Series[]
}

interface Settings {
interface MyStore {
theme: Theme
cwd: string | null
lastPosterPath: string
lastUpdateCheck: number
neverCheckUpdate: boolean
}

interface Metadata {
Expand Down Expand Up @@ -115,15 +117,15 @@ interface AdvFilter {

interface Window {
myAPI: {
changeTheme: (theme: Theme) => Promise<Settings>
getSettings: () => Promise<Settings>
changeTheme: (theme: Theme) => Promise<MyStore>
getSettings: () => Promise<MyStore>
getSeries: () => Promise<Series[]>
editSeries: (series: Series) => Promise<Series>
changePoster: (series: Series) => Promise<Series>
openItem: (fullPath: string) => Promise<void>
getSchedule: () => Schedule
changeSchedule: (schedule: Schedule) => Promise<Schedule>

onUpdateSettings: (listener: (newSettings: Settings) => void) => void
onUpdateSettings: (listener: (newSettings: MyStore) => void) => void
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "my-personal-list",
"description": "My personal anime tracking list",
"version": "1.3.0",
"version": "1.4.0",
"scripts": {
"start": "concurrently \"yarn start:renderer\" \"yarn start:main\" --kill-others",
"start:main": "tsc && electron .",
Expand Down
1 change: 1 addition & 0 deletions packages/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum IPCKey {
RemoveUnusedPosters = 'RemoveUnusedPosters',
OpenDataDir = 'OpenDataDir',
ChangeDataDir = 'ChangeDataDir',
CheckForUpdate = 'CheckForUpdate',
}

export const DATA_FILE = 'mpl.json'
Expand Down
3 changes: 2 additions & 1 deletion packages/common/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contextBridge.exposeInMainWorld('myAPI', {
ipc.invoke(IPCKey.ChangeSchedule, schedule),

// Settings subscription
onUpdateSettings: (listener: (newSettings: Settings) => void) => {
onUpdateSettings: (listener: (newSettings: MyStore) => void) => {
ipc.on(IPCKey.ChangeTheme, async (e, theme) => {
const settings = await ipc.invoke(IPCKey.ChangeTheme, theme)
listener(settings)
Expand All @@ -29,3 +29,4 @@ contextBridge.exposeInMainWorld('myAPI', {
// Menu-Main communications
ipc.on(IPCKey.RemoveUnusedPosters, () => ipc.invoke(IPCKey.RemoveUnusedPosters))
ipc.on(IPCKey.OpenDataDir, () => ipc.invoke(IPCKey.OpenDataDir))
ipc.on(IPCKey.CheckForUpdate, () => ipc.invoke(IPCKey.CheckForUpdate))
23 changes: 14 additions & 9 deletions packages/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { app } from 'electron'
import { autoUpdater } from 'electron-updater'
import Store from 'electron-store'

import { initializeIpcEvents } from './ipcEvents'
import { createMainMenu } from './menu'
import { initializeUpdater } from './updater'
import { createMainWindow, win } from './windowManager'

const initializeAutoUpdate = () => {
autoUpdater.autoDownload = true
autoUpdater.autoInstallOnAppQuit = true
autoUpdater.checkForUpdatesAndNotify()
}
const store = new Store<MyStore>({
defaults: {
cwd: null,
theme: 'light',
lastPosterPath: app ? app.getPath('home') : '/',
lastUpdateCheck: 0,
neverCheckUpdate: false,
},
})

app.on('ready', async () => {
createMainWindow()
createMainMenu()
initializeIpcEvents()
initializeAutoUpdate()
createMainMenu(store)
initializeIpcEvents(store)
initializeUpdater(store)

// Install React Extension if in dev mode
if (!app.isPackaged) {
Expand Down
46 changes: 26 additions & 20 deletions packages/main/ipcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
IpcMainInvokeEvent,
shell,
Notification,
app,
} from 'electron'
import { nanoid } from 'nanoid'
import { autoUpdater } from 'electron-updater'

import {
IPCKey,
Expand All @@ -23,20 +23,20 @@ import {
ensureSchedule,
ensureSeries,
exists,
getUpdateAvailableMsg,
read,
sanitizeSchedule,
sanitizeSeries,
trimSeries,
write,
} from './util'

/* Events */

export class Events {
store: Store<Settings>
store: Store<MyStore>
dialog: Dialog

constructor(store: Store<Settings>, dialog: Dialog) {
constructor(store: Store<MyStore>, dialog: Dialog) {
this.store = store
this.dialog = dialog
}
Expand All @@ -46,7 +46,7 @@ export class Events {
return this.store.store
}

onGetSettings = (e: IpcMainInvokeEvent): Settings => {
onGetSettings = (e: IpcMainInvokeEvent): MyStore => {
return this.store.store
}

Expand Down Expand Up @@ -162,7 +162,7 @@ export class Events {
}

onOpenDataDir = () => {
const cwd = store.get('cwd')
const cwd = this.store.get('cwd')
if (!cwd)
return new Notification({
title: 'Error Opening Data Directory',
Expand All @@ -172,12 +172,12 @@ export class Events {
shell.openPath(cwd)
}

onChangeDataDir = async (): Promise<Settings> => {
onChangeDataDir = async (): Promise<MyStore> => {
const res = await dialog.showOpenDialog({
properties: ['openDirectory'],
})

if (res.canceled) return store.store
if (res.canceled) return this.store.store
// Make sure the selected has `anime` directory in it
const dirExists = await exists(path.join(res.filePaths[0], 'anime'))
if (!dirExists) {
Expand All @@ -186,11 +186,11 @@ export class Events {
body: "Data directory must contain 'anime' dir",
urgency: 'critical',
}).show()
return store.store
return this.store.store
}

store.set('cwd', res.filePaths[0])
return store.store
this.store.set('cwd', res.filePaths[0])
return this.store.store
}

onGetSchedule = async (e: IpcMainInvokeEvent): Promise<Schedule> => {
Expand Down Expand Up @@ -220,20 +220,24 @@ export class Events {

return sanitized
}

onCheckForUpdate = async (e: IpcMainInvokeEvent) => {
const res = await autoUpdater.checkForUpdates()
if (!res) return alert('No new update')

const update = confirm(getUpdateAvailableMsg(res.updateInfo))
if (!update) return

await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
}
}

/* End of Events */

let initialized = false
export const store = new Store<Settings>({
defaults: {
cwd: null,
theme: 'light',
lastPosterPath: app ? app.getPath('home') : '/',
},
})

export const initializeIpcEvents = () => {

export const initializeIpcEvents = (store: Store<MyStore>) => {
const events = new Events(store, dialog)

ipcMain.handle(IPCKey.ChangeTheme, events.onChangeTheme)
Expand All @@ -247,6 +251,7 @@ export const initializeIpcEvents = () => {
ipcMain.handle(IPCKey.ChangeDataDir, events.onChangeDataDir)
ipcMain.handle(IPCKey.GetSchedule, events.onGetSchedule)
ipcMain.handle(IPCKey.ChangeSchedule, events.onChangeSchedule)
ipcMain.handle(IPCKey.CheckForUpdate, events.onCheckForUpdate)

initialized = true
}
Expand All @@ -264,6 +269,7 @@ export const releaseIpcEvents = () => {
ipcMain.removeAllListeners(IPCKey.ChangeDataDir)
ipcMain.removeAllListeners(IPCKey.GetSchedule)
ipcMain.removeAllListeners(IPCKey.ChangeSchedule)
ipcMain.removeAllListeners(IPCKey.CheckForUpdate)

initialized = false
}
18 changes: 14 additions & 4 deletions packages/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Menu, MenuItemConstructorOptions } from 'electron'
import Store from 'electron-store'

import { IPCKey } from '../common/constants'
import { store } from './ipcEvents'

const createTemplate = (): MenuItemConstructorOptions[] => [
const createTemplate = (
store: Store<MyStore>,
): MenuItemConstructorOptions[] => [
{
label: 'MyPersonalList',
submenu: [
{ label: 'About My Personal List' },
{
label: 'Check For Updates',
click: async (menuItem, browser) => {
if (!browser) return
browser.webContents.send(IPCKey.CheckForUpdate)
},
},
{ type: 'separator' },
{
label: 'Theme',
Expand Down Expand Up @@ -101,7 +111,7 @@ const createTemplate = (): MenuItemConstructorOptions[] => [
},
]

export const createMainMenu = () => {
const template = Menu.buildFromTemplate(createTemplate())
export const createMainMenu = (store: Store<MyStore>) => {
const template = Menu.buildFromTemplate(createTemplate(store))
Menu.setApplicationMenu(template)
}
33 changes: 33 additions & 0 deletions packages/main/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Store from 'electron-store'
import { autoUpdater } from 'electron-updater'
import { getUpdateAvailableMsg } from './util'

export const initializeUpdater = async (store: Store<MyStore>) => {
const lastCheck = store.get('lastUpdateCheck')
const neverCheckUpdate = store.get('neverCheckUpdate')
const checked = lastCheck <= new Date().getTime() + 24 * 60 * 60 * 1000
if (neverCheckUpdate || checked) return

const res = await autoUpdater.checkForUpdates()
store.set('lastUpdateCheck', new Date().getTime())
if (!res) return

const answer = prompt(
getUpdateAvailableMsg(res.updateInfo),
'Answer with "now" for update now, "never" for never, or ignore it for ask again tomorrow.',
)

switch (answer) {
case 'now':
await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
break

case 'never':
store.set('neverCheckUpdate', true)
break

default:
break
}
}
8 changes: 8 additions & 0 deletions packages/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs/promises'
import { constants } from 'fs'
import prettier from 'prettier'
import { UpdateInfo } from 'electron-updater'

Object.typedKeys = Object.keys as any

Expand Down Expand Up @@ -136,3 +137,10 @@ export const sanitizeSchedule = (schedule: Schedule): Schedule => {

return newSchedule
}

export const getUpdateAvailableMsg = (
updateInfo: UpdateInfo,
additional?: string,
) =>
`Update available: ${updateInfo.version}. Update now? It will download in the background. When it's finished downloading, it will forcefully (sorry) close the app and install. ` +
additional

0 comments on commit 068f6bc

Please sign in to comment.