TarheelGrad1998 / gallery-card

A custom card for Home Assistant that will display images and/or videos from a folder in the style of a gallery.
99 stars 33 forks source link

Sort Media files by formatted name #29

Closed kalhimeo closed 2 years ago

kalhimeo commented 2 years ago

For media folders, if file_name_format is defined and contains at least the year, month and day , automatically sort pictures by most recent

PS: I am not very experienced with this kind of code so my edit is limited, it may be nice to make the sorting by file_name_format a config option.

TarheelGrad1998 commented 2 years ago

Yeah, I definitely want to make that a config, as I've advertised sorting other ways in the files component.

Also, I want to move the sorting so it applies to both media as well as file source. That will have the nice advantage of being able to sort multiple sources together.

Anyway, I'll merge the commit, and I can make the updates from here. Thanks!

kalhimeo commented 2 years ago

sounds great, thanks for consideration :-)

TarheelGrad1998 commented 2 years ago

Updates here, if you want to take a look: https://github.com/TarheelGrad1998/gallery-card/commit/c0ebc518ed24b5ddb53b4130840941f0db9ef6e4#diff-55be0a59b91aaf13302be0eeedc3463c77d461f000bf586cd03d99678e2df492

(Feels like I made that harder than it should be. My dev skills are slowly dying, lol)

kalhimeo commented 2 years ago

I may be wrong (because my dev skills still have to grow a lot ^^), but if I understand the updated code correctly you have inserted the "date sorting" part to process all resources (either coming from media, file or camera) but I don't see where do you insert the "date" of each file in the array (in the "loadfiles" function) . Also the camera resource by nature cannot have a date.

An other problem that I have found with the sorting "after loading resources" is that it does not cope correctly with the "maxfiles" feature, as newer files may not be loaded since the maxfiles is applied before sorting them all. This problem also exist with my commit.

De : TarheelGrad1998 @.> Envoyé : mercredi 5 janvier 2022 22:39 À : TarheelGrad1998/gallery-card @.> Cc : kalhimeo @.>; Author @.> Objet : Re: [TarheelGrad1998/gallery-card] Sort Media files by formatted name (PR #29)

Updates here, if you want to take a look: c0ebc51#diff-55be0a59b91aaf13302be0eeedc3463c77d461f000bf586cd03d99678e2df492https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTarheelGrad1998%2Fgallery-card%2Fcommit%2Fc0ebc518ed24b5ddb53b4130840941f0db9ef6e4%23diff-55be0a59b91aaf13302be0eeedc3463c77d461f000bf586cd03d99678e2df492&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=f6vISeKKF2qC7bMkcc83eIoDpbzXn0gA4FIqTvXFjio%3D&reserved=0

(Feels like I made that harder than it should be. My dev skills are slowly dying, lol)

- Reply to this email directly, view it on GitHubhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTarheelGrad1998%2Fgallery-card%2Fpull%2F29%23issuecomment-1006098031&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=jVOXugcTwdCXhhuXD0lh8MWSQEyntCUOprNC7%2BuLtn4%3D&reserved=0, or unsubscribehttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABQPTLIFVUA37DQKOIV7NBDUUS255ANCNFSM5K5EWPYA&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=AYl5ABOsDOQH3xUqpX3b2CUmqZa%2FGooiGQ8jB8Zvjnc%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=pGB3gRRzVhI92NJ%2Bf88gUl7SV82daizsV%2BtRKBOuzqc%3D&reserved=0 or Androidhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=HlzOCJQL7pIXK1vIsYJEzEisgag5G5P7JDKRNmLrmO4%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>

kalhimeo commented 2 years ago

Ok sorry by having a second look I realized that _createFileResource is also called from the _loadFilesResources function, I have missed that 😊

Still a problem with _loadCameraResource right ?

_loadCameraResource(entityId, camera) { var resource = { url: camera.attributes.entity_picture, name: entityId, extension: "jpg", caption: camera.attributes.friendly_name ?? entityId, isHass: true }

You may enter a fake date to have them sorted at the beginning or end maybe ?

De : TarheelGrad1998 @.> Envoyé : mercredi 5 janvier 2022 22:39 À : TarheelGrad1998/gallery-card @.> Cc : kalhimeo @.>; Author @.> Objet : Re: [TarheelGrad1998/gallery-card] Sort Media files by formatted name (PR #29)

Updates here, if you want to take a look: c0ebc51#diff-55be0a59b91aaf13302be0eeedc3463c77d461f000bf586cd03d99678e2df492https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTarheelGrad1998%2Fgallery-card%2Fcommit%2Fc0ebc518ed24b5ddb53b4130840941f0db9ef6e4%23diff-55be0a59b91aaf13302be0eeedc3463c77d461f000bf586cd03d99678e2df492&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=f6vISeKKF2qC7bMkcc83eIoDpbzXn0gA4FIqTvXFjio%3D&reserved=0

(Feels like I made that harder than it should be. My dev skills are slowly dying, lol)

— Reply to this email directly, view it on GitHubhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTarheelGrad1998%2Fgallery-card%2Fpull%2F29%23issuecomment-1006098031&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=jVOXugcTwdCXhhuXD0lh8MWSQEyntCUOprNC7%2BuLtn4%3D&reserved=0, or unsubscribehttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABQPTLIFVUA37DQKOIV7NBDUUS255ANCNFSM5K5EWPYA&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=AYl5ABOsDOQH3xUqpX3b2CUmqZa%2FGooiGQ8jB8Zvjnc%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=pGB3gRRzVhI92NJ%2Bf88gUl7SV82daizsV%2BtRKBOuzqc%3D&reserved=0 or Androidhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C5c093e0f8da64d27067508d9d093bcff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770155205430933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=HlzOCJQL7pIXK1vIsYJEzEisgag5G5P7JDKRNmLrmO4%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>

TarheelGrad1998 commented 2 years ago

I did test with the camera resource. You are right, it doesn't have a date, but the sorting left them where they were in the order. I'm guessing that sort method fails when there is no date and thus doesn't do sorting on it? In which case, that's probably good enough for this purpose.

You are right on maxfiles. Maybe I'll move it to after the sorting (though I hate that it has to gather all the files before doing that, but...not sure there's a good alternative there).

kalhimeo commented 2 years ago

Yeah it's sad to gather all the files before limiting the number, but I am afraid it's the only solution :/

For the cameras I think that it s better to add the date array element in order to avoid any potential error. I created a pull request for that and added a new config option to define if user wants cameras to be shown before or after the images. Didn't had time to test to be honest :p

De : TarheelGrad1998 @.> Envoyé : jeudi 6 janvier 2022 14:32 À : TarheelGrad1998/gallery-card @.> Cc : kalhimeo @.>; Author @.> Objet : Re: [TarheelGrad1998/gallery-card] Sort Media files by formatted name (PR #29)

I did test with the camera resource. You are right, it doesn't have a date, but the sorting left them where they were in the order. I'm guessing that sort method fails when there is no date and thus doesn't do sorting on it? In which case, that's probably good enough for this purpose.

You are right on maxfiles. Maybe I'll move it to after the sorting (though I hate that it has to gather all the files before doing that, but...not sure there's a good alternative there).

- Reply to this email directly, view it on GitHubhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTarheelGrad1998%2Fgallery-card%2Fpull%2F29%23issuecomment-1006595205&data=04%7C01%7C%7Cd8b501166d1d444f0cbf08d9d118f4b8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770727376688488%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=nqsWHI3hk%2BTXf%2FbrnLcqBTOshrb1A80IAEs%2FwwVeM0c%3D&reserved=0, or unsubscribehttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABQPTLLMCTAANX5CFBSYCT3UUWKV5ANCNFSM5K5EWPYA&data=04%7C01%7C%7Cd8b501166d1d444f0cbf08d9d118f4b8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770727376688488%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=U66plpF4N3luZKs%2BVgJQzpFqPQc6B7kNIBh5gTHK%2BUM%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7Cd8b501166d1d444f0cbf08d9d118f4b8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770727376688488%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=8MPU1S6fh0302ayS92p4GLrDBGuwDkqY9PDS5V9lSQY%3D&reserved=0 or Androidhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7Cd8b501166d1d444f0cbf08d9d118f4b8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770727376688488%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=P5949reb1%2BPCYyRjtUvUqhU%2FPkMagueqEsk8K%2F33OdY%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>