added MAL update status and submit loader

This commit is contained in:
John O'Keefe 2024-08-16 18:15:56 -04:00
parent a433df84db
commit 862c78d3b8
6 changed files with 104 additions and 10 deletions

View File

@ -55,10 +55,29 @@ func (a *App) GetMyAnimeList(count int) MALWatchlist {
return malList
}
func (a *App) MyAnimeListUpdate(anime MALAnime, update interface{}) MyListStatus {
//var body url.Values
func (a *App) MyAnimeListUpdate(anime MALAnime, update MALUploadStatus) MyListStatus {
if update.NumTimesRewatched >= 1 {
update.IsRewatching = true
} else {
update.IsRewatching = false
}
body := url.Values{}
body.Set("status", update.Status)
body.Set("is_rewatching", strconv.FormatBool(update.IsRewatching))
body.Set("score", strconv.Itoa(update.Score))
body.Set("num_watched_episodes", strconv.Itoa(update.NumWatchedEpisodes))
body.Set("num_times_rewatched", strconv.Itoa(update.NumTimesRewatched))
body.Set("comments", update.Comments)
return MyListStatus{}
malUrl := "https://api.myanimelist.net/v2/anime/" + strconv.Itoa(anime.Id) + "/my_list_status"
var status MyListStatus
respBody := MALHelper("PATCH", malUrl, body)
err := json.Unmarshal(respBody, &status)
if err != nil {
log.Printf("Failed to unmarshal json response, %s\n", err)
}
return status
}
func (a *App) GetMyAnimeListAnime(id int) MALAnime {

View File

@ -152,3 +152,12 @@ type MyListStatus struct {
Comments string `json:"comments" ts_type:"comments"`
UpdatedAt string `json:"updated_at" ts_type:"updatedAt"`
}
type MALUploadStatus struct {
Status string `json:"status"`
IsRewatching bool `json:"is_rewatching"`
Score int `json:"score"`
NumWatchedEpisodes int `json:"num_watched_episodes"`
NumTimesRewatched int `json:"num_times_rewatched"`
Comments string `json:"comments"`
}

View File

@ -17,12 +17,12 @@
import type {SimklAnime} from "./simkl/types/simklTypes";
import { get } from 'svelte/store';
import {
AniListUpdateEntry,
AniListUpdateEntry, MyAnimeListUpdate,
SimklSyncEpisodes,
SimklSyncRating,
SimklSyncStatus
} from "../wailsjs/go/main/App";
import type {MALAnime} from "./mal/types/MALTypes";
import type {MALAnime, MALUploadStatus, MyListStatus} from "./mal/types/MALTypes";
import type {AniListUpdateVariables} from "./anilist/types/AniListTypes";
const simklWatch = get(simklWatchList);
@ -32,12 +32,14 @@
let simklAnimeIndex: number
let currentMalAnime: MALAnime
let simklAnime: SimklAnime | undefined = undefined
let submitting = writable(false)
let isSubmitting: boolean
aniListLoggedIn.subscribe((value) => isAniListLoggedIn = value)
malLoggedIn.subscribe((value) => isMalLoggedIn = value)
simklLoggedIn.subscribe((value) => isSimklLoggedIn = value)
malAnime.subscribe((value) => currentMalAnime = value)
submitting.subscribe((value) => isSubmitting = value)
for (let i = 0; i < simklWatch.anime.length; i++) {
if (Number(simklWatch.anime[i].show.ids.mal) === aniListAnime.data.MediaList.media.idMal) {
@ -252,6 +254,7 @@
}
const submitData = async () => {
submitting.set(true)
let body: AniListUpdateVariables = {
mediaId: aniListAnime.data.MediaList.mediaId,
progress: values.progress,
@ -274,6 +277,27 @@
console.log(value)
})
if(malLoggedIn) {
let body: MALUploadStatus = {
status: values.status.mal,
is_rewatching: false,
score: values.score,
num_watched_episodes: values.progress,
num_times_rewatched: values.repeat,
comments: values.notes
}
await MyAnimeListUpdate(currentMalAnime, body).then((value: MyListStatus) => {
currentMalAnime.my_list_status.status = value.status
currentMalAnime.my_list_status.is_rewatching = value.is_rewatching
currentMalAnime.my_list_status.score = value.score
currentMalAnime.my_list_status.num_episodes_watched = value.num_episodes_watched
currentMalAnime.my_list_status.num_times_rewatched = value.num_times_rewatched
currentMalAnime.my_list_status.comments = value.comments
})
}
if (simklLoggedIn) {
if (simklAnime.watched_episodes_count !== values.progress) {
await SimklSyncEpisodes(simklAnime, values.progress).then(() => {
@ -290,12 +314,13 @@
}
if (simklAnime.status !== values.status.simkl) {
SimklSyncStatus(simklAnime, values.status.simkl).then(() => {
await SimklSyncStatus(simklAnime, values.status.simkl).then(() => {
simklAnime.status = values.status.simkl
simklWatch.anime[simklAnimeIndex].status = values.status.simkl
})
}
}
submitting.set(false)
}
</script>
@ -456,11 +481,21 @@
<div class="bg-white rounded-lg shadow max-w-4-4 dark:bg-gray-800">
<div class="w-full mx-auto max-w-screen-xl p-4 md:flex md:items-center md:justify-end">
<Button
<Button disabled={isSubmitting}
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium
rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none
dark:focus:ring-blue-800"
on:click={submitData}>Sync Changes
on:click={async () => {
let submitLoader = document.querySelector("#submit-loader")
submitLoader.classList.toggle("hidden", false)
await submitData()
submitLoader.classList.toggle("hidden", true)
}}>
<svg id="submit-loader" aria-hidden="true" role="status" class="hidden inline w-4 h-4 me-3 text-white animate-spin" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="#E5E7EB"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentColor"/>
</svg>
Sync Changes
</Button>
<Button
class="text-gray-900 bg-white border border-gray-300 focus:outline-none hover:bg-gray-100 focus:ring-4

View File

@ -151,4 +151,13 @@ export interface MyListStatus {
tags: string[];
comments: string;
updated_at: string;
}
export interface MALUploadStatus {
status: string
is_rewatching: boolean
score: number
num_watched_episodes: number
num_times_rewatched: number
comments: string
}

View File

@ -36,7 +36,7 @@ export function LogoutSimkl():Promise<string>;
export function MyAnimeListLogin():Promise<void>;
export function MyAnimeListUpdate(arg1:main.MALAnime,arg2:any):Promise<main.MyListStatus>;
export function MyAnimeListUpdate(arg1:main.MALAnime,arg2:main.MALUploadStatus):Promise<main.MyListStatus>;
export function SimklGetUserWatchlist():Promise<main.SimklWatchList>;

View File

@ -287,6 +287,28 @@ export namespace main {
return a;
}
}
export class MALUploadStatus {
status: string;
is_rewatching: boolean;
score: number;
num_watched_episodes: number;
num_times_rewatched: number;
comments: string;
static createFrom(source: any = {}) {
return new MALUploadStatus(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.status = source["status"];
this.is_rewatching = source["is_rewatching"];
this.score = source["score"];
this.num_watched_episodes = source["num_watched_episodes"];
this.num_times_rewatched = source["num_times_rewatched"];
this.comments = source["comments"];
}
}
export class MALWatchlist {
data: struct { Node struct { Id int "json:\"id\" ts_type:\"id\""; Title string "json:\"title\" ts_type:\"title\""; MainPicture struct { Medium string "json:\"medium\" json:\"medium\""; Large string "json:\"large\" json:\"large\"" } "json:\"main_picture\" json:\"mainPicture\"" } "json:\"node\" json:\"node\""; ListStatus struct { Status string "json:\"status\" ts_type:\"status\""; Score int "json:\"score\" ts_type:\"score\""; NumEpisodesWatched int "json:\"num_episodes_watched\" ts_type:\"numEpisodesWatched\""; IsRewatching bool "json:\"is_rewatching\" ts_type:\"isRewatching\""; UpdatedAt time.Time "json:\"updated_at\" ts_type:\"updatedAt\""; StartDate string "json:\"start_date\" ts_type:\"startDate\""; FinishDate string "json:\"finish_date\" ts_type:\"finishDate\"" } "json:\"list_status\" ts_type:\"listStatus\"" }[];
paging: paging;