Skip to content

Commit

Permalink
1.0.7
Browse files Browse the repository at this point in the history
Improve separate desktop/mobile configuration.
  • Loading branch information
alangrainger committed Aug 18, 2024
1 parent 674090b commit 4a4cd4c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "lazy-plugins",
"name": "Lazy Plugin Loader",
"version": "1.0.6",
"version": "1.0.7",
"minAppVersion": "1.6.0",
"description": "Load plugins with a delay on startup, so that you can get your app startup down into the sub-second loading time.",
"author": "Alan Grainger",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lazy-plugins",
"version": "1.0.6",
"version": "1.0.7",
"description": "Load plugins with a delay on startup, so that you can get your app startup down into the sub-second loading time.",
"main": "main.js",
"scripts": {
Expand Down
61 changes: 39 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Platform, Plugin, PluginManifest } from 'obsidian'
import { DEFAULT_SETTINGS, LazySettings, LoadingMethod, SettingsTab } from './settings'
import {
DEFAULT_SETTINGS,
DeviceSettings,
LazySettings,
LoadingMethod,
SettingsTab
} from './settings'

const lazyPluginId = require('../manifest.json').id

export default class LazyPlugin extends Plugin {
settings: LazySettings
data: LazySettings
settings: DeviceSettings
manifests: PluginManifest[]
pendingTimeouts: NodeJS.Timeout[] = []

Expand Down Expand Up @@ -77,24 +84,43 @@ export default class LazyPlugin extends Plugin {
}

/**
* Get the startup type for a given pluginId, depending on whether the user
* is on desktop or mobile. Fallback to Obsidian's current loading method
* (enabled/disabled) if no configuration is found for this plugin.
* Get the startup type for a given pluginId, falling back to Obsidian's current
* loading method (enabled/disabled) if no configuration is found for this plugin.
*/
getPluginStartup (pluginId: string): LoadingMethod {
let value
if (Platform.isMobile) value = this.settings.plugins?.[pluginId]?.startupMobile
return value ||
this.settings.plugins?.[pluginId]?.startupType ||
return this.settings.plugins?.[pluginId]?.startupType ||
this.settings.defaultStartupType ||
(this.app.plugins.enabledPlugins.has(pluginId) ? LoadingMethod.instant : LoadingMethod.disabled)
}

async loadSettings () {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
this.data = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())

// Migration from from plugin version <1.0.6
// This will be deleted in a few weeks.
if (this.data.hasOwnProperty('plugins')) {
['plugins', 'defaultStartupType', 'shortDelaySeconds', 'longDelaySeconds'].forEach(key => {
// @ts-ignore
this.data.desktop[key] = this.data[key]
// @ts-ignore
delete this.data[key]
})
}

// If user has dual mobile/desktop settings enabled
if (this.data.dualConfigs && Platform.isMobile) {
if (!this.data.mobile) {
// No existing configuration - copy the desktop one
this.data.mobile = Object.assign({}, this.data.desktop)
}
this.settings = this.data.mobile
} else {
this.settings = this.data.desktop
}
}

async saveSettings () {
await this.saveData(this.settings)
await this.saveData(this.data)
}

/**
Expand All @@ -106,10 +132,7 @@ export default class LazyPlugin extends Plugin {
for (const plugin of this.manifests) {
if (!this.settings.plugins?.[plugin.id]?.startupType) {
// There is no existing setting for this plugin, so create one
await this.updatePluginSettings(plugin.id,
this.settings.defaultStartupType ||
(this.app.plugins.enabledPlugins.has(plugin.id) ? LoadingMethod.instant : LoadingMethod.disabled)
)
await this.updatePluginSettings(plugin.id, this.getPluginStartup(plugin.id))
}
}
}
Expand All @@ -118,13 +141,7 @@ export default class LazyPlugin extends Plugin {
* Update an individual plugin's configuration in the settings file
*/
async updatePluginSettings (pluginId: string, startupType: LoadingMethod) {
const settings = this.settings.plugins[pluginId] || { startupType }
if (Platform.isMobile) {
settings.startupMobile = startupType
} else {
settings.startupType = startupType
}
this.settings.plugins[pluginId] = settings
this.settings.plugins[pluginId] = { startupType }
await this.saveSettings()
}

Expand Down
46 changes: 35 additions & 11 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@ import LazyPlugin from './main'

interface PluginSettings {
startupType: LoadingMethod;
startupMobile?: LoadingMethod;
}

export interface LazySettings {
export interface DeviceSettings {
[key: string]: any;

shortDelaySeconds: number;
longDelaySeconds: number;
defaultStartupType: LoadingMethod | null;
plugins: { [pluginId: string]: PluginSettings };
showConsoleLog: boolean;
}

export const DEFAULT_SETTINGS: LazySettings = {
export const DEFAULT_DEVICE_SETTINGS: DeviceSettings = {
shortDelaySeconds: 5,
longDelaySeconds: 15,
defaultStartupType: null,
plugins: {},
showConsoleLog: false
plugins: {}
}

export interface LazySettings {
dualConfigs: boolean;
showConsoleLog: boolean;
desktop: DeviceSettings;
mobile?: DeviceSettings;
}

export const DEFAULT_SETTINGS: LazySettings = {
dualConfigs: false,
showConsoleLog: false,
desktop: DEFAULT_DEVICE_SETTINGS
}

export enum LoadingMethod {
Expand Down Expand Up @@ -55,6 +65,25 @@ export class SettingsTab extends PluginSettingTab {

containerEl.empty()

new Setting(containerEl)
.setName('Separate desktop/mobile configuration')
.setDesc('Enable this if you want to have different settings depending whether you\'re using a desktop or mobile device. ' +
'All of the settings below can be configured differently on desktop and mobile.')
.addToggle(toggle => {
toggle
.setValue(this.lazyPlugin.data.dualConfigs)
.onChange(async (value) => {
this.lazyPlugin.data.dualConfigs = value
await this.lazyPlugin.saveSettings()
// Refresh the settings to make sure the mobile section is configured
await this.lazyPlugin.loadSettings()
})
})

new Setting(containerEl)
.setName('Global plugin delay settings')
.setHeading()

// Create the two timer settings fields
Object.entries({
shortDelaySeconds: 'Short delay (seconds)',
Expand All @@ -71,10 +100,6 @@ export class SettingsTab extends PluginSettingTab {
}))
})

new Setting(containerEl)
.setName('Global plugin delay settings')
.setHeading()

new Setting(containerEl)
.setName('Default startup type for new plugins')
.addDropdown(dropdown => {
Expand All @@ -90,7 +115,6 @@ export class SettingsTab extends PluginSettingTab {

new Setting(containerEl)
.setName('Individual plugin delay settings')
.setDesc('These settings can be set differently on a desktop or mobile device.')
.setHeading()

new Setting(containerEl)
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"1.0.3": "1.6.0",
"1.0.4": "1.6.0",
"1.0.5": "1.6.0",
"1.0.6": "1.6.0"
"1.0.6": "1.6.0",
"1.0.7": "1.6.0"
}

0 comments on commit 4a4cd4c

Please sign in to comment.