Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
feat: Сохранение предпочитаемых переводов
Browse files Browse the repository at this point in the history
  • Loading branch information
cawa-93 committed May 19, 2021
1 parent 6a0b678 commit bf5d117
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@vueuse/core": "4.11.0",
"electron-updater": "4.3.9",
"electron-window-state": "5.0.3",
"idb": "^6.1.0",
"libass-wasm": "4.0.0",
"vue": "3.0.11",
"vue-router": "4.0.8"
Expand Down
15 changes: 10 additions & 5 deletions packages/renderer/src/components/WatchPage/TranslationsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:class="{active: selectedTranslation === translation}"
:to="{params: {translationId: translation.id, episodeNum: selectedEpisodeNum}, hash: currentLocation.hash}"
replace
@click="saveToPreferred(translation)"
>
<win-icon class="play-icon">
&#xF5B0;
Expand All @@ -30,11 +31,13 @@

<script lang="ts">
import type {DeepReadonly, PropType} from 'vue';
import {computed, defineComponent} from 'vue';
import {computed, defineComponent, toRaw} from 'vue';
import {useRoute} from 'vue-router';
import WinIcon from '/@/components/WinIcon.vue';
import type {Translation} from '/@/utils/videoProvider';
import {useBrowserLocation} from '@vueuse/core';
import {savePreferredTranslation} from '/@/utils/translationRecomendations';
export default defineComponent({
name: 'TranslationsList',
Expand Down Expand Up @@ -81,12 +84,14 @@ export default defineComponent({
return result;
});
// const hash = ref(location.hash);
const currentLocation = useBrowserLocation();
// useIntervalFn(() => console.log({native: location.hash, wrapped: l.value.hash}));
// setTimeout(() => hash.value = location.hash, 1000);
return {selectedTranslation, groups, currentLocation};
// Сохранение выбранного перевода в предпочтениях
const saveToPreferred = (translation: DeepReadonly<Translation>) => {
savePreferredTranslation(route.params.seriesId as NumberLike, toRaw(translation) as Translation);
};
return {selectedTranslation, groups, currentLocation, saveToPreferred};
},
});
</script>
Expand Down
1 change: 1 addition & 0 deletions packages/renderer/src/components/WatchPage/WatchPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<label for="active-tab-translations">Переводы</label>
</div>
</div>

<episodes-list
v-if="episodes.length > 0 && sidePanelActiveTab === 'episodes'"
:episodes="episodes"
Expand Down
43 changes: 43 additions & 0 deletions packages/renderer/src/utils/translationRecomendations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {Translation} from '/@/utils/videoProvider';
import type {DBSchema, IDBPDatabase } from 'idb';
import {openDB} from 'idb';


interface TranslationRecommendations extends DBSchema {
preferences: {
value: Translation & {seriesId: number};
key: number;
indexes: {
'by-type': 'sub' | 'voice'
}
};
}

let dbPromise: Promise<IDBPDatabase<TranslationRecommendations>> | null = null;

function getDB() {

if (dbPromise !== null) {
return dbPromise;
}

dbPromise = openDB<TranslationRecommendations>('translation-recommendations', 1, {
upgrade(db: IDBPDatabase<TranslationRecommendations>, oldVersion: number) {
if (oldVersion < 1) {
const preferencesStore = db.createObjectStore('preferences', {keyPath: 'seriesId'});
preferencesStore.createIndex('by-type', 'type');
}
},
});

return dbPromise;
}


export async function savePreferredTranslation(seriesId: NumberLike, translation: Translation): Promise<number> {
if (typeof seriesId !== 'number') {
seriesId = Number(seriesId);
}

return (await getDB()).put('preferences', {...translation, seriesId});
}

0 comments on commit bf5d117

Please sign in to comment.