Move application logic from view to Launcher

This commit is contained in:
Robert McRackan 2019-12-10 10:33:51 -05:00
parent 41620352e8
commit 123a32ff9b
10 changed files with 68 additions and 93 deletions

View file

@ -31,12 +31,10 @@ namespace FileManager
*/
#endregion
private const string configFilename = "LibationSettings.json";
private PersistentDictionary persistentDictionary { get; }
[Description("Location of the configuration file where these settings are saved. Please do not edit this file directly while Libation is running.")]
public string Filepath { get; }
public string Filepath => Path.Combine(Path.GetDirectoryName(Exe.FileLocationOnDisk), "Settings.json");
[Description("[Advanced. Leave alone in most cases.] Your user-specific key used to decrypt your audible files (*.aax) into audio files you can use anywhere (*.m4b)")]
public string DecryptKey
@ -90,13 +88,10 @@ namespace FileManager
public static Configuration Instance { get; } = new Configuration();
private Configuration()
{
Filepath = getPath();
// load json values into memory
persistentDictionary = new PersistentDictionary(Filepath);
ensureDictionaryEntries();
// setUserFilesDirectoryDefault
// don't create dir. dir creation is the responsibility of places that use the dir
if (string.IsNullOrWhiteSpace(LibationFiles))
LibationFiles = Path.Combine(Path.GetDirectoryName(Exe.FileLocationOnDisk), "Libation");
@ -113,67 +108,6 @@ namespace FileManager
return attribute?.Description;
}
private static string getPath()
{
// search folders for config file. accept the first match
var defaultdir = Path.GetDirectoryName(Exe.FileLocationOnDisk);
var baseDirs = new HashSet<string>
{
defaultdir,
getNonDevelopmentDir(defaultdir),
Environment.GetFolderPath(Environment.SpecialFolder.Personal)
};
var subDirs = baseDirs.Select(dir => Path.Combine(dir, "Libation"));
var dirs = baseDirs.Concat(subDirs).ToList();
foreach (var dir in dirs)
{
var f = Path.Combine(dir, configFilename);
if (File.Exists(f))
return f;
}
return Path.Combine(defaultdir, configFilename);
}
private static string getNonDevelopmentDir(string path)
{
// examples:
// \Libation\Core2_0\bin\Debug\netcoreapp3.1
// \Libation\StndLib\bin\Debug\netstandard2.1
// \Libation\MyWnfrm\bin\Debug
// \Libation\Core2_0\bin\Release\netcoreapp3.1
// \Libation\StndLib\bin\Release\netstandard2.1
// \Libation\MyWnfrm\bin\Release
var curr = new DirectoryInfo(path);
if (!curr.Name.EqualsInsensitive("debug") && !curr.Name.EqualsInsensitive("release") && !curr.Name.StartsWithInsensitive("netcoreapp") && !curr.Name.StartsWithInsensitive("netstandard"))
return path;
// get out of netcore/standard dir => debug
if (curr.Name.StartsWithInsensitive("netcoreapp") || curr.Name.StartsWithInsensitive("netstandard"))
curr = curr.Parent;
if (!curr.Name.EqualsInsensitive("debug") && !curr.Name.EqualsInsensitive("release"))
return path;
// get out of debug => bin
curr = curr.Parent;
if (!curr.Name.EqualsInsensitive("bin"))
return path;
// get out of bin
curr = curr.Parent;
// get out of csproj-level dir
curr = curr.Parent;
// curr should now be sln-level dir
return curr.FullName;
}
private void ensureDictionaryEntries()
{
var stringProperties = getDictionaryProperties().Select(p => p.Name).ToList();