From fb36cda9a13764f53793d4d1e5a8073cf55d6ccd Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 28 Oct 2023 13:47:21 +0200
Subject: [PATCH] AMADEUS: Format the date and the filesize in the kara modal

---
 Cargo.lock                                |  1 +
 amadeus/Cargo.toml                        |  1 +
 amadeus/src/components/mod.rs             | 13 ++++++++
 amadeus/src/components/modal/karainfos.rs | 37 +++++++++++++++--------
 4 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index b8d247de..ac601841 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -72,6 +72,7 @@ version = "3.0.1"
 dependencies = [
  "anyhow",
  "async-trait",
+ "chrono",
  "futures",
  "hashbrown 0.14.2",
  "iced",
diff --git a/amadeus/Cargo.toml b/amadeus/Cargo.toml
index 7465d597..de5b3c6a 100644
--- a/amadeus/Cargo.toml
+++ b/amadeus/Cargo.toml
@@ -11,6 +11,7 @@ description = "Amadeus-RS, graphical interface for lektord"
 serde.workspace = true
 serde_json.workspace = true
 
+chrono.workspace = true
 anyhow.workspace = true
 hashbrown.workspace = true
 
diff --git a/amadeus/src/components/mod.rs b/amadeus/src/components/mod.rs
index 7869487d..2f76ee1b 100644
--- a/amadeus/src/components/mod.rs
+++ b/amadeus/src/components/mod.rs
@@ -24,6 +24,19 @@ pub fn loading_centered_shrink<'a, T: 'a>() -> iced::Element<'a, T> {
         .into()
 }
 
+pub fn file_size<'a, T: 'a>(bytes: usize) -> iced::Element<'a, T> {
+    let kilos = bytes.div_euclid(1024);
+    let megas = kilos.div_euclid(1024);
+    iced::widget::text(if megas > 0 {
+        format!("File Size: {megas}MiB")
+    } else if kilos > 0 {
+        format!("File Size: {kilos}KiB")
+    } else {
+        format!("File Size: {bytes}B")
+    })
+    .into()
+}
+
 macro_rules! tip {
     ($inner: expr => $position: ident | $tip: expr) => {
         iced::widget::tooltip($inner, $tip, iced::widget::tooltip::Position::$position)
diff --git a/amadeus/src/components/modal/karainfos.rs b/amadeus/src/components/modal/karainfos.rs
index 3b5ddb13..889dae1e 100644
--- a/amadeus/src/components/modal/karainfos.rs
+++ b/amadeus/src/components/modal/karainfos.rs
@@ -1,4 +1,8 @@
-use crate::style_sheet::{sizes::*, Color, RoundBadgeStyleSheet};
+use crate::{
+    components::file_size,
+    style_sheet::{sizes::*, Color, RoundBadgeStyleSheet},
+};
+use chrono::TimeZone;
 use iced::{
     widget::{column, container, horizontal_rule, text},
     Element,
@@ -94,19 +98,26 @@ impl State {
             ..
         } = self.0.as_ref();
 
+        let mut column = column![text(format!(
+            "Remote Name: {}",
+            self.0.remote.remote_name()
+        ))];
+
+        if let Some(time) = chrono::Local.timestamp_opt(ts.created_at, 0).latest() {
+            let time = time.format("%Y-%m-%d %H:%M:%S");
+            column = column.push(text(format!("Created at: {time}")));
+        };
+
+        if let Some(time) = chrono::Local.timestamp_opt(ts.updated_at, 0).latest() {
+            let time = time.format("%Y-%m-%d %H:%M:%S");
+            column = column.push(text(format!("Last Modified at: {time}")));
+        };
+
         let aux_infos = match self.0.kara_status {
-            lektor_payloads::KaraStatus::Virtual => column![
-                text(format!("Remote Name: {}", self.0.remote.remote_name())),
-                text(format!("Created at: {}", ts.created_at)),
-                text(format!("Last Modified at: {}", ts.updated_at)),
-            ],
-            lektor_payloads::KaraStatus::Physical { filesize, hash } => column![
-                text(format!("Remote Name: {}", self.0.remote.remote_name())),
-                text(format!("Created at: {}", ts.created_at)),
-                text(format!("Last Modified at: {}", ts.updated_at)),
-                text(format!("File Size: {}B", filesize)),
-                text(format!("File Hash: {hash}")),
-            ],
+            lektor_payloads::KaraStatus::Virtual => column,
+            lektor_payloads::KaraStatus::Physical { filesize, hash } => column
+                .push(file_size(filesize as usize))
+                .push(text(format!("File Hash: {hash}"))),
         };
 
         container(
-- 
GitLab