Skip to content

Commit

Permalink
Imporve options ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
ziulev committed Apr 8, 2021
1 parent 7cedade commit aef2d44
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
12 changes: 9 additions & 3 deletions src/components/options.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Options = ({
data={options}
keyExtractor={(item) => item.title}
persistentScrollbar={true}
onScrollToIndexFailed={() => console.log('SCROLL FAILED')}
onScrollToIndexFailed={() => null}
renderItem={({ item, index }) => (
<TouchableOpacity onPress={() => onSubmit(index)}>
<Option
Expand Down Expand Up @@ -113,8 +113,14 @@ export const OptionHotkeyHints = ({ option }: { option: SpotterPluginOption }) =
flexDirection: 'row',
alignItems: 'center',
}}>
<OptionHotkeyHint style={{}} placeholder={'tab'}></OptionHotkeyHint>
<OptionHotkeyHint style={{marginLeft: 5}} placeholder={'enter'}></OptionHotkeyHint>
{option.onQuery
? <OptionHotkeyHint style={{}} placeholder={'tab'}></OptionHotkeyHint>
: null
}
{option.action
? <OptionHotkeyHint style={{marginLeft: 5}} placeholder={'enter'}></OptionHotkeyHint>
: null
}
</View>
};

Expand Down
1 change: 0 additions & 1 deletion src/containers/spotter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export const App: FC<{}> = () => {
return;
}

console.log(option)
registries.plugins.activateOption(option);
api.queryInput.setValue('');

Expand Down
6 changes: 2 additions & 4 deletions src/core/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ export declare abstract class SpotterSettingsRegistry {
}

export declare abstract class SpotterHistoryRegistry {
abstract getPluginHistory(): Promise<SpotterHistory>;
abstract getOptionsHistory(): Promise<SpotterHistory>;
abstract increasePluginHistory(plugin: string, query: string): void;
abstract increaseOptionHistory(option: string, query: string): void;
abstract increaseOptionHistory(path: string[], query: string): void;
}

export declare abstract class SpotterShell {
Expand Down Expand Up @@ -115,7 +113,7 @@ export type SpotterOptionBaseImage = string | number | { uri: string } | undefin

export interface SpotterOption {
title: string;
action: SpotterAction;
action?: SpotterAction;
onQuery?: (query: string) => Promise<SpotterOption[]> | SpotterOption[];
subtitle?: string;
keywords?: string[];
Expand Down
28 changes: 9 additions & 19 deletions src/registries/history.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,39 @@ import { SpotterHistory, SpotterHistoryRegistry, SpotterApi } from '../core';

export class HistoryRegistry implements SpotterHistoryRegistry {
private nativeModules: SpotterApi;
private PLUGIN_HISTORY_STORAGE_KEY = 'PLUGIN_HISTORY_STORAGE_KEY_v1';
private OPTIONS_HISTORY_STORAGE_KEY = 'OPTIONS_HISTYRY_STORAGE_KEY_v1';
private defaultValue: SpotterHistory = {};

constructor(nativeModules: SpotterApi) {
this.nativeModules = nativeModules;
}

async getPluginHistory(): Promise<SpotterHistory> {
const history = await this.nativeModules
.storage
.getItem<SpotterHistory>(this.PLUGIN_HISTORY_STORAGE_KEY);
return history ?? this.defaultValue;
}

async getOptionsHistory(): Promise<SpotterHistory> {
const history = await this.nativeModules
.storage
.getItem<SpotterHistory>(this.OPTIONS_HISTORY_STORAGE_KEY);
return history ?? this.defaultValue;
}

async increaseOptionHistory(option: string, query: string) {
async increaseOptionHistory(path: string[], query: string) {
const currentHistory = await this.getOptionsHistory();
const nextHistory = this.increase(currentHistory, option, query);
const nextHistory = this.increase(currentHistory, path, query);
this.nativeModules.storage.setItem(this.OPTIONS_HISTORY_STORAGE_KEY, nextHistory);
}

async increasePluginHistory(option: string, query: string) {
const currentHistory = await this.getPluginHistory();
const nextHistory = this.increase(currentHistory, option, query);
this.nativeModules.storage.setItem(this.PLUGIN_HISTORY_STORAGE_KEY, nextHistory);
}

private increase(
history: SpotterHistory,
title: string,
path: string[],
query: string,
): SpotterHistory {
const currentOptionHistory = history[title] ?? { queries: {}, total: 0 };
if (!path.length) {
return history;
}
const stringPath = path.join('#');
const currentOptionHistory = history[stringPath] ?? { queries: {}, total: 0 };
return {
...history,
...({[title]: {
...({[stringPath]: {
queries: {
...currentOptionHistory.queries,
[query]: currentOptionHistory.queries[query] ? currentOptionHistory.queries[query] + 1 : 1,
Expand Down
38 changes: 25 additions & 13 deletions src/registries/plugins.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class PluginsRegistry implements SpotterPluginsRegistry {
return this.activeOptionSubject$.asObservable();
}

get activeOption(): SpotterPluginOption | null {
return this.activeOptionSubject$.getValue();
}

private currentOptionsSubject$ = new BehaviorSubject<SpotterPluginOption[]>([]);

get currentOptions(): SpotterPluginOption[] {
Expand All @@ -69,7 +73,11 @@ export class PluginsRegistry implements SpotterPluginsRegistry {
const activeOption = this.activeOptionSubject$.value;
if (activeOption && activeOption.onQuery && activeOption.plugin) {
const options = await activeOption.onQuery(query);
this.currentOptionsSubject$.next(options.map(o => ({...o, plugin: activeOption.plugin})));
const sortedOptions = await this.getSortedPluginOptions(
options.map(o => ({...o, plugin: activeOption.plugin})),
query,
);
this.currentOptionsSubject$.next(sortedOptions);
return;
}

Expand Down Expand Up @@ -102,24 +110,23 @@ export class PluginsRegistry implements SpotterPluginsRegistry {
this.currentOptionsSubject$.next(sortedOptions);
}

private getFullHistoryPath(option: string): string {
return this.activeOption ? `${this.activeOption.title}#${option}` : option;
}

private async getSortedPluginOptions(
options: SpotterPluginOption[],
query: string,
): Promise<SpotterPluginOption[]> {

const pluginHistory: SpotterHistory = await this.historyRegistry.getPluginHistory();
const optionsHistory: SpotterHistory = await this.historyRegistry.getOptionsHistory();

return options
.sort((a, b) => (
(pluginHistory[b.title]?.total ?? 0) -
(pluginHistory[a.title]?.total ?? 0)
))
.sort((a, b) => (
(Object.entries(optionsHistory[b.title]?.queries ?? {})
(Object.entries(optionsHistory[this.getFullHistoryPath(b.title)]?.queries ?? {})
.reduce((acc, [q, counter]) => q.startsWith(query) ? acc + counter : acc, 0)
) -
(Object.entries(optionsHistory[a.title]?.queries ?? {})
(Object.entries(optionsHistory[this.getFullHistoryPath(a.title)]?.queries ?? {})
.reduce((acc, [q, counter]) => q.startsWith(query) ? acc + counter : acc, 0)
)
));
Expand All @@ -132,19 +139,24 @@ export class PluginsRegistry implements SpotterPluginsRegistry {
}

public async submitOption(
optionWithIdentifier: SpotterPluginOption,
option: SpotterPluginOption,
query: string,
callback: (success: boolean) => void,
) {
if (!optionWithIdentifier?.action) {
if (!option?.action) {
callback(false);
return;
};

this.historyRegistry.increasePluginHistory(optionWithIdentifier.plugin, query);
this.historyRegistry.increaseOptionHistory(optionWithIdentifier.title, query);
this.historyRegistry.increaseOptionHistory(
[
...(this.activeOption ? [this.activeOption.title] : []),
option.title,
],
query,
);

const success = await optionWithIdentifier.action();
const success = await option.action();

if (typeof success === 'function') {
const options = success();
Expand Down

0 comments on commit aef2d44

Please sign in to comment.