Browse Source

Code refactor

Ruben 4 years ago
parent
commit
6f05a4deb4

+ 1 - 1
PicView/ChangeImage/History.cs

@@ -162,7 +162,7 @@ namespace PicView.ChangeImage
 
         internal static async Task PrevAsync()
         {
-            if (Navigation.Pics.Count == 0)
+            if (Navigation.Pics.Count <= 0)
             {
                 await OpenLastFileAsync().ConfigureAwait(false);
                 return;

+ 3 - 6
PicView/ChangeImage/Preloader.cs

@@ -35,7 +35,7 @@ namespace PicView.ChangeImage
             string, PreloadValue> Sources = new ConcurrentDictionary<string, PreloadValue>();
 
         /// <summary>
-        /// Add file to preloader from index
+        /// Add file to preloader from index. Returns true if new value added.
         /// </summary>
         /// <param name="i">Index of Pics</param>
         internal static async Task<bool> AddAsync(int i, FileInfo? fileInfo = null, BitmapSource? bitmapSource = null)
@@ -130,11 +130,8 @@ namespace PicView.ChangeImage
             {
                 return false;
             }
-            if (Sources.TryAdd(name, preloadValue))
-            {
-                return true;
-            }
-            return false;
+
+            return Sources.TryAdd(name, preloadValue);
         }
 
         /// <summary>

+ 10 - 3
PicView/FileHandling/FileFunctions.cs

@@ -65,10 +65,17 @@ namespace PicView.FileHandling
         /// </summary>
         /// <param name="path"></param>
         /// <returns></returns>
-        internal static bool CheckIfDirectoryOrFile(string path)
+        internal static bool? CheckIfDirectoryOrFile(string path)
         {
-            var getAttributes = File.GetAttributes(path);
-            return getAttributes.HasFlag(FileAttributes.Directory);
+            try
+            {
+                var getAttributes = File.GetAttributes(path);
+                return getAttributes.HasFlag(FileAttributes.Directory);
+            }
+            catch (Exception)
+            {
+                return null;
+            }
         }
 
         internal static bool RenameFile(string path, string newPath)

+ 5 - 3
PicView/FileHandling/FileLists.cs

@@ -43,6 +43,11 @@ namespace PicView.FileHandling
         {
             if (fileInfo is null) { return null; }
 
+            var checkIfDir = FileFunctions.CheckIfDirectoryOrFile(fileInfo.FullName);
+            if (checkIfDir is null) { return null; }
+
+            var directory = checkIfDir.Value ? fileInfo.FullName : fileInfo.DirectoryName;
+
             SearchOption searchOption;
 
             if (Properties.Settings.Default.IncludeSubDirectories && string.IsNullOrWhiteSpace(TempZipFile)) // Don't search subdirectories when zipped
@@ -54,9 +59,6 @@ namespace PicView.FileHandling
                 searchOption = SearchOption.TopDirectoryOnly;
             }
 
-            string? directory = FileFunctions.CheckIfDirectoryOrFile(fileInfo.FullName) ? fileInfo.FullName : fileInfo.DirectoryName;
-            if (directory == null) { return null; }
-
             var items = Directory.EnumerateFiles(directory, "*.*", searchOption)
                 .AsParallel()
                 .Where(file => SupportedFiles.IsSupportedExt(file)

+ 1 - 1
PicView/UILogic/TransformImage/Zoom.cs

@@ -68,7 +68,7 @@ namespace PicView.UILogic.TransformImage
                 return ") ";
             }
 
-            return $", {x} : {y})";
+            return $", {x} : {y}) ";
         }
 
         /// <summary>