Skip to content

Commit fdab47d

Browse files
committed
refactor custom skin sorting
1 parent ca7e47a commit fdab47d

File tree

5 files changed

+192
-167
lines changed

5 files changed

+192
-167
lines changed

apps/app/src-tauri/src/api/skin_manager.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
1111
skin_get_user_skin_data,
1212
skin_get_heads,
1313
skin_set_skin,
14-
skin_update_skins,
14+
skin_delete_skin,
1515
skin_import_skin,
1616
skin_check_image,
1717
skin_check_skin,
@@ -22,6 +22,8 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
2222
skin_save_skin,
2323
skin_get_skins,
2424
skin_set_cape,
25+
skin_get_order,
26+
skin_save_order,
2527
skin_get_filters,
2628
skin_save_filters
2729
])
@@ -94,22 +96,22 @@ pub async fn skin_cache_users_skins() -> Result<bool> {
9496
// Makes api request to mojang, updating current account's skin data
9597
// invoke('plugin:skin|skin_cache_new_user_skin', { user })
9698
#[tauri::command]
97-
pub async fn skin_cache_new_user_skin(creds: Credentials) -> Result<bool> {
99+
pub async fn skin_cache_new_user_skin(creds: Credentials) -> Result<()> {
98100
Ok(skin_manager::cache_new_user_skin(creds).await?)
99101
}
100102

101103
// Saves the skin data to the manager
102104
// invoke('plugin:skin|skin_save_skin', { user, data, name, model, skinid })
103105
#[tauri::command]
104-
pub async fn skin_save_skin(user: Uuid, data: SkinCache, name: String, model: String, skinid: String) -> Result<bool> {
106+
pub async fn skin_save_skin(user: Uuid, data: SkinCache, name: String, model: String, skinid: String) -> Result<()> {
105107
Ok(skin_manager::save_skin(user, data, name, model, skinid).await?)
106108
}
107109

108110
// Updates skin saves from the manager
109111
// invoke('plugin:skin|skin_update_skins', { id })
110112
#[tauri::command]
111-
pub async fn skin_update_skins(saves: Vec<SkinSave>) -> Result<()> {
112-
Ok(skin_manager::update_skins(saves).await?)
113+
pub async fn skin_delete_skin(id: Uuid) -> Result<()> {
114+
Ok(skin_manager::delete_skin(id).await?)
113115
}
114116

115117
// Gets all account heads from the cache
@@ -126,6 +128,20 @@ pub async fn skin_get_skins() -> Result<Vec<SkinSave>> {
126128
Ok(skin_manager::get_skins().await?)
127129
}
128130

131+
// Gets custom skin order
132+
// invoke('plugin:skin|skin_get_order')
133+
#[tauri::command]
134+
pub async fn skin_get_order(user: Uuid) -> Result<Vec<Uuid>> {
135+
Ok(skin_manager::get_order(user).await?)
136+
}
137+
138+
// Saves custom skin order
139+
// invoke('plugin:skin|skin_save_order')
140+
#[tauri::command]
141+
pub async fn skin_save_order(order: Vec<Uuid>, user: Uuid) -> Result<()> {
142+
Ok(skin_manager::save_order(order, user).await?)
143+
}
144+
129145
// Gets a list of all saved skins in the mojang launcher
130146
// invoke('plugin:skin|skin_get_launcher_names', { path })
131147
#[tauri::command]

apps/app/src/components/ui/AccountsCard.vue

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ const defaultUser = ref()
9595
async function refreshValues() {
9696
defaultUser.value = await get_default_user().catch(handleError)
9797
accounts.value = await users().catch(handleError)
98-
selectedAccount.value = accounts.value.find(
99-
(account) => account.id === defaultUser.value
100-
)
10198
}
10299
defineExpose({
103100
refreshValues,
104101
})
105102
await refreshValues()
103+
selectedAccount.value = accounts.value.find(
104+
(account) => account.id === defaultUser.value
105+
)
106106
107107
const displayAccounts = computed(() =>
108108
accounts.value.filter((account) => defaultUser.value !== account.id),
@@ -119,10 +119,10 @@ async function login() {
119119
const loggedIn = await login_flow().catch(handleSevereError)
120120
121121
if (loggedIn) {
122-
await setAccount(loggedIn)
123-
await refreshValues()
124122
await cache_new_user_skin(loggedIn).catch(handleError)
125123
get_heads()
124+
await setAccount(loggedIn)
125+
await refreshValues()
126126
}
127127
128128
mixpanel_track('AccountLogIn')
@@ -131,7 +131,7 @@ async function login() {
131131
const logout = async (id) => {
132132
await remove_user(id).catch(handleError)
133133
await refreshValues()
134-
if (!selectedAccount.value && accounts.value.length > 0) {
134+
if (accounts.value.length > 0) {
135135
await setAccount(accounts.value[0])
136136
await refreshValues()
137137
} else {

apps/app/src/helpers/skin_manager.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function check_image(path) {
3434
return await invoke('plugin:skin|skin_check_image', { path })
3535
}
3636

37-
// Returns true if skin is not in library
37+
// Returns true if skin is in library
3838
export async function check_skin(skin, id) {
3939
return await invoke('plugin:skin|skin_check_skin', { skin, id })
4040
}
@@ -59,9 +59,19 @@ export async function save_skin(user, data, name, model, skinid) {
5959
return await invoke('plugin:skin|skin_save_skin', { user, data, name, model, skinid })
6060
}
6161

62-
// Updates the skin save
63-
export async function update_skins(saves) {
64-
return await invoke('plugin:skin|skin_update_skins', { saves })
62+
// Deletes the skin save
63+
export async function delete_skin(id) {
64+
return await invoke('plugin:skin|skin_delete_skin', { id })
65+
}
66+
67+
// Gets custom skin order
68+
export async function get_order(user) {
69+
return await invoke('plugin:skin|skin_get_order', { user })
70+
}
71+
72+
// Saves custom skin order
73+
export async function save_order(order, user) {
74+
return await invoke('plugin:skin|skin_save_order', { order, user })
6575
}
6676

6777
// Gets all saved Skins

apps/app/src/pages/SkinManager.vue

+29-64
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ import {
291291
set_skin,
292292
save_skin,
293293
import_skin,
294-
update_skins,
294+
delete_skin,
295+
get_order,
296+
save_order,
295297
get_skins,
296298
get_render,
297299
get_cape_data,
@@ -340,6 +342,7 @@ const importer = ref({
340342
const importType = ref('Mojang')
341343
342344
const skinSaves = ref(await get_skins().catch(handleError))
345+
const skinOrder = ref([])
343346
344347
const search = ref('')
345348
@@ -356,13 +359,6 @@ const filteredResults = computed(() => {
356359
saves = saves.filter((save) => {
357360
return save.user === selectedAccount.value.id
358361
})
359-
} else {
360-
for (let i = 0; i < saves.length; i++) {
361-
if (!Object.prototype.hasOwnProperty.call(saves[i].order, selectedAccount.value.id)) {
362-
shiftSaves(0, true)
363-
saves[i].order[selectedAccount.value.id] = 0
364-
}
365-
}
366362
}
367363
368364
if (Filters.value.sort === 'Name') {
@@ -373,7 +369,7 @@ const filteredResults = computed(() => {
373369
374370
if (Filters.value.sort === 'Custom') {
375371
saves.sort((a, b) => {
376-
return a.order[selectedAccount.value.id] - b.order[selectedAccount.value.id]
372+
return skinOrder.value.indexOf(a.id) - skinOrder.value.indexOf(b.id)
377373
})
378374
}
379375
@@ -391,25 +387,25 @@ const filteredResults = computed(() => {
391387
return saves
392388
})
393389
394-
const moveCard = async (move, skin) => {
395-
let sorted = Array.from(skinSaves.value)
390+
const moveCard = async (move, id) => {
391+
let order = Array.from(skinOrder.value)
396392
if (Filters.value.filter === 'Current user') {
397-
sorted = sorted.filter((save) => {
398-
return save.user === selectedAccount.value.id
393+
let saves = Array.from(skinSaves.value).sort((a, b) => {
394+
return skinOrder.value.indexOf(a.id) - skinOrder.value.indexOf(b.id)
395+
})
396+
order = order.filter((_, i) => {
397+
return saves[i].user === selectedAccount.value.id
399398
})
400399
}
401-
sorted.sort((a, b) => {
402-
return a.order[selectedAccount.value.id] - b.order[selectedAccount.value.id]
403-
})
404-
405-
const targetIndex = sorted.indexOf(skin) + move
406-
if (targetIndex < 0 || targetIndex > sorted.length - 1) return
407-
const current = skin.order[selectedAccount.value.id]
408-
const target = sorted[targetIndex].order[selectedAccount.value.id]
409-
shiftSaves(target < current ? target : target + 1, true)
410-
skin.order[selectedAccount.value.id] = target < current ? target : target + 1
411-
shiftSaves(target < current ? current + 1 : current, false)
412-
await update_skins(skinSaves.value).catch(handleError)
400+
let index = order.indexOf(id)
401+
if (index == 0 && move == -1) return
402+
if (index == order.length - 1 && move == 1) return
403+
404+
let targetIndex = skinOrder.value.indexOf(order[index + move])
405+
let currentIndex = skinOrder.value.indexOf(id)
406+
skinOrder.value.splice(currentIndex, 1)
407+
skinOrder.value.splice(targetIndex, 0, id)
408+
await save_order(skinOrder.value, selectedAccount.value.id)
413409
}
414410
415411
const handleRightClick = (event, item) => {
@@ -462,10 +458,10 @@ const handleOptionsClick = async (args) => {
462458
await handleSkin('upload')
463459
break
464460
case 'left':
465-
if (Filters.value.sort === 'Custom') await moveCard(-1, args.item)
461+
if (Filters.value.sort === 'Custom') await moveCard(-1, args.item.id)
466462
break
467463
case 'right':
468-
if (Filters.value.sort === 'Custom') await moveCard(1, args.item)
464+
if (Filters.value.sort === 'Custom') await moveCard(1, args.item.id)
469465
break
470466
case 'edit':
471467
await edit_skin(args.item)
@@ -482,18 +478,7 @@ const handleOptionsClick = async (args) => {
482478
483479
const deleteSkin = async () => {
484480
skinSaves.value.splice(skinSaves.value.indexOf(selectedSkin.value), 1)
485-
let sorted = skinSaves.value.filter((save) => {
486-
return Object.prototype.hasOwnProperty.call(save.order, selectedAccount.value.id)
487-
})
488-
if (sorted.length > 0) {
489-
sorted.sort((a, b) => {
490-
return a.order[selectedAccount.value.id] - b.order[selectedAccount.value.id]
491-
})
492-
for (let i = selectedSkin.value.order[selectedAccount.value.id]; i < sorted.length; i++) {
493-
sorted[i].order[selectedAccount.value.id]--
494-
}
495-
}
496-
await update_skins(skinSaves.value).catch(handleError)
481+
await delete_skin(selectedSkin.value.id).catch(handleError)
497482
InLibrary.value = await check_skin(skinData.value.skin, selectedAccount.value.id).catch(
498483
handleError,
499484
)
@@ -524,13 +509,12 @@ const next = async () => {
524509
handleError,
525510
)
526511
const model = await get_render(data).catch(handleError)
527-
shiftSaves(0, true)
528-
await update_skins(skinSaves.value).catch(handleError)
529512
await save_skin(selectedAccount.value.id, data, skin.name, model, '').catch(handleError)
530513
skin.selected = false
531514
importedSkins.value++
532515
}
533516
skinSaves.value = await get_skins().catch(handleError)
517+
skinOrder.value = await get_order(selectedAccount.value.id).catch(handleError)
534518
loading.value = false
535519
skinModal.value.hide()
536520
}
@@ -553,6 +537,7 @@ const handleAdd = async () => {
553537
handleError,
554538
)
555539
skinSaves.value = await get_skins().catch(handleError)
540+
skinOrder.value = await get_order(selectedAccount.value.id).catch(handleError)
556541
InLibrary.value = true
557542
}
558543
@@ -595,8 +580,6 @@ const clickCard = (data) => {
595580
596581
const handleSkin = async (state) => {
597582
if (state.includes('save')) {
598-
shiftSaves(0, true)
599-
await update_skins(skinSaves.value).catch(handleError)
600583
const model = await get_render(selectedSkin.value).catch(handleError)
601584
await save_skin(
602585
selectedAccount.value.id,
@@ -606,14 +589,14 @@ const handleSkin = async (state) => {
606589
'',
607590
).catch(handleError)
608591
skinSaves.value = await get_skins().catch(handleError)
592+
skinOrder.value = await get_order(selectedAccount.value.id).catch(handleError)
609593
}
610594
if (state.includes('upload')) {
611595
uploadingSkin.value = true
612596
const capeid = await get_cape_data(selectedSkin.value.cape, 'id').catch(handleError)
613597
const uploadedCape = await set_cape(capeid, selectedAccount.value.access_token).catch(
614598
handleError,
615599
)
616-
console.log(selectedSkin.value.skin)
617600
const uploadedSkin = await set_skin(
618601
selectedSkin.value.skin,
619602
selectedSkin.value.arms,
@@ -707,28 +690,9 @@ const duplicate_skin = async (args) => {
707690
data.cape = args.cape
708691
data.arms = args.arms
709692
data.unlocked_capes = []
710-
shiftSaves(0, true)
711-
await update_skins(skinSaves.value).catch(handleError)
712693
await save_skin(selectedAccount.value.id, data, args.name, args.model, '').catch(handleError)
713694
skinSaves.value = await get_skins().catch(handleError)
714-
}
715-
716-
const shiftSaves = (index, shiftRight) => {
717-
let sorted = skinSaves.value.filter((save) => {
718-
return Object.prototype.hasOwnProperty.call(save.order, selectedAccount.value.id)
719-
})
720-
sorted.sort((a, b) => {
721-
return a.order[selectedAccount.value.id] - b.order[selectedAccount.value.id]
722-
})
723-
if (shiftRight) {
724-
for (let i = sorted.length - 1; i >= index; i--) {
725-
sorted[i].order[selectedAccount.value.id]++
726-
}
727-
} else {
728-
for (let i = index; i < sorted.length; i++) {
729-
sorted[i].order[selectedAccount.value.id]--
730-
}
731-
}
695+
skinOrder.value = await get_order(selectedAccount.value.id).catch(handleError)
732696
}
733697
734698
const openskin = async () => {
@@ -819,6 +783,7 @@ const handleImportType = async () => {
819783
}
820784
821785
watch(selectedAccount, async (newAccount) => {
786+
skinOrder.value = await get_order(newAccount.id).catch(handleError)
822787
await update_render(newAccount.id)
823788
})
824789

0 commit comments

Comments
 (0)