Skip to content

Commit

Permalink
Providers by language
Browse files Browse the repository at this point in the history
  • Loading branch information
Carleslc committed Aug 28, 2020
1 parent 94d3969 commit ff076cb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/components/AnimeSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export default {
},
},
methods: {
...mapMutations('store', ['setProvider', 'setAlternativeTitle']),
...mapMutations('store', ['setProviderByTitle', 'setAlternativeTitle']),
updateProvider(provider) {
this.setProvider({
this.setProviderByTitle({
title: this.title,
provider,
});
Expand Down
15 changes: 11 additions & 4 deletions src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
<div class="col-auto text-h4">My Anime</div>
</div>
<div v-else class="col row justify-end items-center no-wrap" @mousedown.prevent>
<provider-select ref="providerSelect" v-model="provider" dark icon class="col-auto q-mx-auto gt-xsm" />
<provider-select
ref="providerSelect"
:value="provider"
dark
icon
class="col-auto q-mx-auto gt-xsm"
@input="setProvider"
/>
<div class="col-auto q-gutter-x-lg q-mr-auto row justify-between gt-md">
<status-select
v-model="airingStatusFilter"
Expand Down Expand Up @@ -76,7 +83,7 @@
</q-item-section>
<q-item-section class="q-pt-sm">
<q-item-label v-t="'selectProvider'" header class="q-px-sm" />
<provider-select v-model="provider" dark icon class="q-pr-xs" />
<provider-select :value="provider" dark icon class="q-pr-xs" @input="setProvider" />
</q-item-section>
<q-item-section class="q-pt-lg">
<q-item-label v-t="'animeStatus'" header class="q-px-sm" />
Expand Down Expand Up @@ -165,7 +172,7 @@ export default {
},
computed: {
...mapState('store', ['api', 'userFetched']),
...mapGetters('store', ['isLoading', 'isFetched', 'hasUsername']),
...mapGetters('store', ['provider', 'isLoading', 'isFetched', 'hasUsername']),
hiddenIfSettings() {
return this.settings ? 'display: none' : undefined;
},
Expand All @@ -185,7 +192,7 @@ export default {
this.info = !this.isRecurringUser;
},
methods: {
...mapMutations('store', ['loading', 'updateFetched']),
...mapMutations('store', ['setProvider', 'updateFetched']),
...mapActions('store', ['fetchAnimes']),
overlappingFooter(offset) {
const footerHeight = '41px';
Expand Down
52 changes: 26 additions & 26 deletions src/mixins/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export const defaults = {
language: i18n.locale,
username: '',
status: 'watching',
provider: providers[0],
providersByLanguage: {
es: providers[0],
en: providers[0],
},
providersByAnimeTitle: {},
genreFilter: [],
airingStatusFilter: defaultConfig.airingStatuses.map((status) => status.value),
Expand All @@ -157,19 +160,15 @@ export default {
if (!this.isRecurrentUser) {
Object.keys(defaults).forEach((key) => {
if (this.$q.localStorage.has(key)) {
let value = this.$q.localStorage.getItem(key);
const value = this.$q.localStorage.getItem(key);

const getProvider = (label) => providers.find((provider) => provider.label === label) || providers[0];

// convert provider labels to provider objects
if (key === 'provider') {
value = getProvider(value);
} else if (key === 'providersByAnimeTitle') {
const providersByAnimeTitle = {};
Object.entries(value).forEach(([title, providerLabel]) => {
providersByAnimeTitle[title] = getProvider(providerLabel);
if (['providersByLanguage', 'providersByAnimeTitle'].includes(key)) {
Object.entries(value).forEach(([providerKey, providerLabel]) => {
value[providerKey] = getProvider(providerLabel);
});
value = providersByAnimeTitle;
}

if (value !== undefined && value !== null) {
Expand All @@ -180,25 +179,26 @@ export default {
}

// add watchers to save configuration
Object.keys(defaults)
.filter((key) => key !== 'provider' && key !== 'providersByAnimeTitle') // avoid saving entire objects
.forEach((key) => this.$watch(key, (value) => this.$q.localStorage.set(key, value), { deep: true }));
Object.keys(defaults).forEach((key) => {
if (['providersByLanguage', 'providersByAnimeTitle'].includes(key)) {
// save provider label only
this.$watch(
key,
(value) => {
const providerLabels = {};
Object.entries(value).forEach(([providerKey, provider]) => {
providerLabels[providerKey] = provider.label;
});
this.$q.localStorage.set(key, providerLabels);
},
{ deep: true }
);
} else {
this.$watch(key, (value) => this.$q.localStorage.set(key, value), { deep: true });
}
});
},
watch: {
// save provider label only
provider(provider) {
this.$q.localStorage.set('provider', provider.label);
},
providersByAnimeTitle: {
handler(providersByAnimeTitle) {
const saveProviders = {};
Object.entries(providersByAnimeTitle).forEach(([title, provider]) => {
saveProviders[title] = provider.label;
});
this.$q.localStorage.set('providersByAnimeTitle', saveProviders);
},
deep: true,
},
language(locale) {
if (i18n.locale !== locale) {
i18n.locale = locale;
Expand Down
4 changes: 2 additions & 2 deletions src/model/providers/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default class Provider {
let encoded = encodeURIComponent(
s
.toLowerCase()
.replace(/[^- a-z0-9]+/, '')
.replace(/[^- a-z0-9]+/g, '')
.replace(/\s+/g, sep)
.replace(new RegExp(`(${sep}){2,}`), sep)
.replace(new RegExp(`(${sep}){2,}`, 'g'), sep)
);
if (encoded[encoded.length - 1] === sep) {
encoded = encoded.substring(0, encoded.length - 1);
Expand Down
7 changes: 5 additions & 2 deletions src/store/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ export default {
hasUsername({ username }) {
return !isBlank(username);
},
providerByAnimeTitle({ provider, providersByAnimeTitle }) {
return (title) => providersByAnimeTitle[title] || provider;
providerByAnimeTitle({ providersByAnimeTitle }, getters) {
return (title) => providersByAnimeTitle[title] || getters.provider;
},
providers({ language }) {
return Object.freeze(
providers.filter((provider) => !provider.value.languages || provider.value.languages.includes(language))
);
},
provider({ providersByLanguage, language }) {
return providersByLanguage[language];
},
titleByAnimeId({ api, titlesByAnimeId }) {
return (id) => titlesByAnimeId[api.name][id];
},
Expand Down
7 changes: 5 additions & 2 deletions src/store/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export default {
anime.status = 'watching';
}
},
setProvider(state, { title, provider }) {
if (provider === state.provider) {
setProvider(state, provider) {
state.providersByLanguage[state.language] = provider;
},
setProviderByTitle(state, { title, provider }) {
if (provider === state.providersByLanguage[state.language]) {
Vue.delete(state.providersByAnimeTitle, title);
} else {
Vue.set(state.providersByAnimeTitle, title, provider);
Expand Down
4 changes: 2 additions & 2 deletions src/store/store/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function newState() {
language: defaults.language,
username: defaults.username,
status: defaults.status,
provider: defaults.provider,
titlesByAnimeId: defaults.titlesByAnimeId,
providersByLanguage: defaults.providersByLanguage,
providersByAnimeTitle: defaults.providersByAnimeTitle,
titlesByAnimeId: defaults.titlesByAnimeId,
airingStatusFilter: defaults.airingStatusFilter,
typeFilter: defaults.typeFilter,
genreFilter: defaults.genreFilter,
Expand Down

0 comments on commit ff076cb

Please sign in to comment.