Skip to content

Commit

Permalink
refactor aspect_ratio field to use string type and update related com…
Browse files Browse the repository at this point in the history
…ponents
  • Loading branch information
zurdi15 committed Dec 3, 2024
1 parent c88ac9f commit 5eefdda
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 58 deletions.
4 changes: 2 additions & 2 deletions backend/alembic/versions/0027_platforms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def upgrade() -> None:
batch_op.add_column(
sa.Column(
"aspect_ratio",
sa.Float(),
sa.String(length=10),
nullable=False,
server_default=str(DEFAULT_COVER_ASPECT_RATIO),
server_default=DEFAULT_COVER_ASPECT_RATIO,
)
)
# ### end Alembic commands ###
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/responses/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PlatformSchema(BaseModel):
url_logo: str | None = None
logo_path: str | None = None
firmware: list[FirmwareSchema] = Field(default_factory=list)
aspect_ratio: float = DEFAULT_COVER_ASPECT_RATIO
aspect_ratio: str = DEFAULT_COVER_ASPECT_RATIO
created_at: datetime
updated_at: datetime

Expand Down
8 changes: 4 additions & 4 deletions backend/models/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from models.base import BaseModel
from models.rom import Rom
from sqlalchemy import Float, String, func, select
from sqlalchemy import String, func, select
from sqlalchemy.orm import Mapped, column_property, mapped_column, relationship

if TYPE_CHECKING:
from models.firmware import Firmware


DEFAULT_COVER_ASPECT_RATIO = 2 / 3
DEFAULT_COVER_ASPECT_RATIO = "2 / 3"


class Platform(BaseModel):
Expand All @@ -37,8 +37,8 @@ class Platform(BaseModel):
lazy="selectin", back_populates="platform"
)

aspect_ratio: Mapped[float] = mapped_column(
Float, server_default=str(DEFAULT_COVER_ASPECT_RATIO)
aspect_ratio: Mapped[str] = mapped_column(
String, server_default=DEFAULT_COVER_ASPECT_RATIO
)

# This runs a subquery to get the count of roms for the platform
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/__generated__/models/PlatformSchema.ts

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

36 changes: 0 additions & 36 deletions frontend/src/components/Gallery/AppBar/Platform/Base.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import PlatformIcon from "@/components/common/Platform/Icon.vue";
import storeNavigation from "@/stores/navigation";
import storeRoms from "@/stores/roms";
import { storeToRefs } from "pinia";
import { computed, ref, watch } from "vue";
import { useDisplay } from "vuetify";
// Props
Expand All @@ -20,41 +19,6 @@ const romsStore = storeRoms();
const { currentPlatform } = storeToRefs(romsStore);
const navigationStore = storeNavigation();
const { activePlatformInfoDrawer } = storeToRefs(navigationStore);
const selectedAspectRatio = ref(0);
const aspectRatioOptions = computed(() => [
{
name: "2 / 3",
size: 2 / 3,
source: "SteamGridDB",
},
{
name: "3 / 4",
size: 3 / 4,
source: "IGDB / MobyGames",
},
{
name: "1 / 1",
size: 1 / 1,
source: "Old squared cases",
},
]);
watch(
() => currentPlatform.value?.aspect_ratio,
(aspectRatio) => {
if (aspectRatio) {
// Find the index of the aspect ratio option that matches the current aspect ratio
const defaultAspectRatio = aspectRatioOptions.value.findIndex(
(option) => Math.abs(option.size - aspectRatio) < 0.01, // Handle floating-point precision issues
);
// If a matching aspect ratio option is found, update the selectedAspectRatio
if (defaultAspectRatio !== -1) {
selectedAspectRatio.value = defaultAspectRatio;
}
}
},
{ immediate: true }, // Execute the callback immediately with the current value
);
</script>

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ watch(
if (aspectRatio) {
// Find the index of the aspect ratio option that matches the current aspect ratio
const defaultAspectRatio = aspectRatioOptions.value.findIndex(
(option) => Math.abs(option.size - aspectRatio) < 0.01, // Handle floating-point precision issues
(option) => option.name == aspectRatio,
);
// If a matching aspect ratio option is found, update the selectedAspectRatio
if (defaultAspectRatio !== -1) {
Expand Down Expand Up @@ -91,7 +91,7 @@ async function setAspectRatio() {
.updatePlatform({
platform: {
...currentPlatform.value,
aspect_ratio: selectedOption.size,
aspect_ratio: selectedOption.name,
},
})
.then(({ data }) => {
Expand All @@ -101,7 +101,7 @@ async function setAspectRatio() {
color: "green",
});
if (currentPlatform.value) {
currentPlatform.value.aspect_ratio = selectedOption.size;
currentPlatform.value.aspect_ratio = selectedOption.name;
}
})
.catch((error) => {
Expand Down
21 changes: 11 additions & 10 deletions frontend/src/components/common/Game/Card/Base.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import storeDownload from "@/stores/download";
import storeGalleryView from "@/stores/galleryView";
import storeRoms from "@/stores/roms";
import { type SimpleRom } from "@/stores/roms.js";
import { onMounted, ref } from "vue";
import { onMounted, ref, computed } from "vue";
import { useTheme } from "vuetify";
// Props
const props = withDefaults(
defineProps<{
rom: SimpleRom | SearchRomSchema;
aspectRatio?: number;
aspectRatio?: string | number;
transformScale?: boolean;
titleOnHover?: boolean;
showFlags?: boolean;
Expand Down Expand Up @@ -62,6 +62,13 @@ const card = ref();
const theme = useTheme();
const galleryViewStore = storeGalleryView();
const collectionsStore = storeCollections();
const computedAspectRatio = computed(() => {
const ratio =
props.aspectRatio ||
platfotmsStore.getAspectRatio(props.rom.platform_id) ||
galleryViewStore.defaultAspectRatioCover;
return parseFloat(ratio.toString());
});
// Functions
onMounted(() => {
Expand Down Expand Up @@ -128,13 +135,7 @@ onMounted(() => {
? rom.igdb_url_cover
: rom.moby_url_cover
"
:aspect-ratio="
aspectRatio
? aspectRatio
: platfotmsStore.getAspectRatio(rom.platform_id)
? platfotmsStore.getAspectRatio(rom.platform_id)
: galleryViewStore.defaultAspectRatioCover
"
:aspect-ratio="computedAspectRatio"
>
<div v-bind="props" style="position: absolute; top: 0; width: 100%">
<template v-if="titleOnHover">
Expand Down Expand Up @@ -209,7 +210,7 @@ onMounted(() => {
<v-img
:src="`/assets/default/cover/big_${theme.global.name.value}_missing_cover.png`"
cover
:aspect-ratio="aspectRatio"
:aspect-ratio="computedAspectRatio"
></v-img>
</template>
<template #placeholder>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/stores/platforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export default defineStore("platforms", {
},
getAspectRatio(platformId: number): number {
const platform = this.allPlatforms.find((p) => p.id === platformId);
return platform && platform.aspect_ratio ? platform.aspect_ratio : 2 / 3;
return platform && platform.aspect_ratio
? parseFloat(eval(platform.aspect_ratio as string))
: 2 / 3;
},
},
});

0 comments on commit 5eefdda

Please sign in to comment.