Use locally stored images for cards when possible
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Hamcha 2019-09-20 11:30:30 +02:00
parent e0bf8c39df
commit 916af31051
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
5 changed files with 87 additions and 16 deletions

View file

@ -28,9 +28,8 @@ h1.loading-message {
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { Action, Getter } from "vuex-class";
import { loadSets } from "@/mlpccg/set";
import { AppState } from "./store/types";
import { getCards } from "./mlpccg/database";
import { AppState } from "@/store/types";
import { refreshCardSource, loadSets, getCards } from "@/mlpccg";
@Component({
components: {}
@ -54,6 +53,7 @@ export default class App extends Vue {
private async loadCards() {
this.showLoading("Downloading data for all sets");
await loadSets();
await refreshCardSource();
this.hideLoading();
this.setLoaded(true);
}

View file

@ -0,0 +1,50 @@
<template>
<img :src="imageURL" />
</template>
<style lang="scss" scoped>
</style>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import { cardImageURL } from "../../mlpccg";
@Component({
components: {}
})
export default class CardImage extends Vue {
@Prop()
private id!: string;
private loaded!: boolean;
private loadedURL!: string;
private data() {
return {
loaded: false,
loadedURL: ""
};
}
private mounted() {
this.fetchImage();
this.$watch("id", () => {
this.loaded = false;
this.fetchImage();
});
}
private async fetchImage() {
const url = await cardImageURL(this.id);
this.loaded = true;
this.loadedURL = url;
}
private get imageURL(): string {
if (this.loaded) {
return this.loadedURL;
}
return require("@/assets/images/cardback.webp");
}
}
</script>

View file

@ -6,7 +6,7 @@
v-for="(card, i) in cards"
:key="i + card.data.ID"
>
<img :src="imageURL(card.data.ID)" />
<CardImage :id="card.data.ID" />
</article>
</section>
</template>
@ -45,9 +45,12 @@ $padding: 10px;
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import { Card, CardSlot, cardImageURL } from "@/mlpccg";
import CardImage from "@/components/Cards/CardImage.vue";
@Component({
components: {}
components: {
CardImage
}
})
export default class CardPicker extends Vue {
@Prop()

View file

@ -1,15 +1,37 @@
import { Database } from "./database";
const imgBaseURL = "https://mcg.zyg.ovh/images/cards/";
let imageSource: "local" | "remote" = "remote";
export function cardImageURL(cardid: string): string {
export function remoteImageURL(cardid: string): string {
return `${imgBaseURL}${cardid}.webp`;
}
export async function cardImageSource() {
export async function cardImageURL(cardid: string): Promise<string> {
if (!Database) {
return "remote";
return remoteImageURL(cardid);
}
switch (cardImageSource()) {
case "local":
const card = await Database.images.get(`${cardid}.webp`);
if (!card) {
return remoteImageURL(cardid);
}
return URL.createObjectURL(card.image);
//TODO
case "remote":
return remoteImageURL(cardid);
}
}
export function cardImageSource() {
return imageSource;
}
export async function refreshCardSource() {
if (!Database) {
return;
}
const count = await Database.images.count();
return count > 1900 ? "local" : "remote";
imageSource = count > 1900 ? "local" : "remote";
}

View file

@ -98,7 +98,7 @@
import { Component, Vue } from "vue-property-decorator";
import TopNav from "@/components/Navigation/TopNav.vue";
import { TaskRunner } from "@/workers";
import { cardImageSource } from "@/mlpccg";
import { cardImageSource, refreshCardSource } from "@/mlpccg";
@Component({
components: { TopNav }
@ -125,22 +125,18 @@ export default class SettingsView extends Vue {
});
worker.on("finish", async _ => {
this.downloadState = null;
this.cardImageSource = await cardImageSource();
await refreshCardSource();
});
}
private data() {
return {
cardImageSource: "remote",
cardImageSource: cardImageSource(),
downloadState: null,
downloadProgress: null
};
}
private async mounted() {
this.cardImageSource = await cardImageSource();
}
private get imageSource() {
switch (this.cardImageSource) {
case "local":