DineshSolanki / FoliCon

Creates Folder icons for Movies, Serials, Music,Anime and Games Folders
https://dineshsolanki.github.io/FoliCon/
GNU General Public License v3.0
57 stars 6 forks source link

Issue #145 Rating shows decimal points more than 2, in list and on rating shield #158

Closed sarangsmk closed 8 months ago

sarangsmk commented 8 months ago

Changes

Updated BuildFolderIco helper to Format the rating string, Added helper FormatRatingString in DataUtils.cs

Issue #145 Rating shows decimal points more than 2, in list and on rating shield

Checklist before requesting a review

DineshSolanki commented 8 months ago

The same ratings are also shown in the listbox, of poster mode search result.

Try to generify this at the source instead, also if possible, avoid using regex here, see if the code can be be made a bit more simpler.

Please do not add files in commit, that are not related to the issue,

DineshSolanki commented 8 months ago

@sarangsmk see if this helps

Subject: [PATCH] Add rating formatting in ListItem and DataUtils
---
Index: FoliCon/Models/Data/ListItem.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/FoliCon/Models/Data/ListItem.cs b/FoliCon/Models/Data/ListItem.cs
--- a/FoliCon/Models/Data/ListItem.cs   (revision 9b33c85e9a932dcf8f27eddf3aa1895c2a5be1ad)
+++ b/FoliCon/Models/Data/ListItem.cs   (revision 85f2f38b16ec8646fe92ff0175600f53800a4d60)
@@ -1,3 +1,5 @@
+using FoliCon.Modules.utils;
+
 namespace FoliCon.Models.Data;

 public class ListItem : BindableBase
@@ -28,7 +30,17 @@
     public string Rating
     {
         get => _rating;
-        set => SetProperty(ref _rating, value);
+        set
+        {
+            if (double.TryParse(value, out var ratingValue))
+            {
+                SetProperty(ref _rating, DataUtils.FormatRating(ratingValue)); 
+            }
+            else
+            {
+                throw new ArgumentException("Invalid rating value.");
+            }
+        }
     }

     public string Folder
Index: FoliCon/Modules/utils/DataUtils.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/FoliCon/Modules/utils/DataUtils.cs b/FoliCon/Modules/utils/DataUtils.cs
--- a/FoliCon/Modules/utils/DataUtils.cs    (revision 9b33c85e9a932dcf8f27eddf3aa1895c2a5be1ad)
+++ b/FoliCon/Modules/utils/DataUtils.cs    (revision 85f2f38b16ec8646fe92ff0175600f53800a4d60)
@@ -6,4 +6,20 @@
     {
         return isPickedById ? result != null ? 1 : 0 : searchMode == "Game" ? result.Length : result.TotalResults;
     }
+    
+    /// <summary>
+    /// Formats a rating value by removing unnecessary trailing zeroes. 
+    /// If the rating has a decimal part, it is formatted with up to two decimal places.
+    /// Else it is formatted as an integer.
+    /// </summary>
+    /// <param name="ratingValue">The rating value to format.</param>
+    /// <returns>The formatted rating as a string.</returns>
+    public static string FormatRating(double ratingValue)
+    {
+        var decimalPart = ratingValue % 1;
+
+        return decimalPart > 0
+            ? ratingValue.ToString("0.##")
+            : ratingValue.ToString("0");
+    }
 }
\ No newline at end of file
DineshSolanki commented 8 months ago

/// <summary>
    /// Formats a rating value by removing unnecessary trailing zeroes. 
    /// If the rating has a decimal part, it is formatted with up to two decimal places.
    /// Else it is formatted as an integer.
    /// </summary>
    /// <param name="ratingValue">The rating value to format.</param>
    /// <returns>The formatted rating as a string.</returns>
    public static string FormatRating(double ratingValue)
    {
        var decimalPart = ratingValue % 1;

        return decimalPart > 0
            ? ratingValue.ToString("0.##")
            : ratingValue.ToString("0");
    }
sarangsmk commented 8 months ago

Fixed review comments,Please check again