Skip to content

Commit

Permalink
Merge pull request #102 from jibedoubleve/issue_100-99-98
Browse files Browse the repository at this point in the history
Fix Issues
#100,
#99,
#98
  • Loading branch information
jibedoubleve authored Jan 28, 2023
2 parents 31344eb + e219a05 commit 36c63c3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/Lanceur.Core/Services/IDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public interface IDataService
/// <returns>Points of the chart</returns>
IEnumerable<DataPoint<DateTime, double>> GetUsage(Per per, long? idSession = null);

/// <summary>
/// Hydrate the macro with its <c>id</c> and <c>count</c>. This method will try to find the
/// macro by using its name that should be something like '@it_s_name@'
/// </summary>
/// <param name="alias">Macro to hydrate</param>
void HydrateMacro(QueryResult alias);

/// <summary>
/// Update the usage of the specified <see cref="QueryResult"/>
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Lanceur.Infra.SQLite/Models/Tuple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Lanceur.Infra.SQLite
{
internal record Tuple<T1, T2>
{
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
}
}
5 changes: 3 additions & 2 deletions src/Lanceur.Infra.SQLite/SQLiteAppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SQLiteAppSettingsService : SQLiteServiceBase, IAppSettingsService
"IdSession",
"ShowAtStartup",
"HotKey.Key",
"HotKey.ModifierKeys",
"HotKey.ModifierKey",
"Repository.ScoreLimit",
"Window.Position.Left",
"Window.Position.Top",
Expand Down Expand Up @@ -97,7 +97,8 @@ public void Save(AppSettings settings)

foreach (var item in Keys)
{
DB.Connection.Execute(sql, new { key = item, value = settings.GetPropValue(item) });
var value = settings.GetPropValue(item);
DB.Connection.Execute(sql, new { key = item, value });
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/Lanceur.Infra.SQLite/SQLiteDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Lanceur.Infra.SQLite
{
public class SQLiteDataService : SQLiteServiceBase, IDataService
public partial class SQLiteDataService : SQLiteServiceBase, IDataService
{
#region Fields

Expand Down Expand Up @@ -161,6 +161,30 @@ public IEnumerable<DataPoint<DateTime, double>> GetUsage(Per per, long? idSessio
};
}

public void HydrateMacro(QueryResult alias)
{
var sql = @"
select
a.id as Item1,
count(a.id) as Item2
from
alias a
inner join alias_usage au on a.id = au.id_alias
where
file_name like @name
group by
a.id ";

var results = DB.Connection.Query<Tuple<int, int>>(sql, new { name = alias.Name });

if (results.Count() == 1)
{
var item = results.ElementAt(0);
alias.Id = item.Item1;
alias.Count = item.Item2;
}
}

public IEnumerable<QueryResult> RefreshUsage(IEnumerable<QueryResult> result) => _aliasDbAction.RefreshUsage(result);

public void Remove(AliasQueryResult alias) => _aliasDbAction.Remove(alias);
Expand Down
8 changes: 6 additions & 2 deletions src/Lanceur.Infra/Managers/MacroManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ public class MacroManager : IMacroManager

private static Dictionary<string, ExecutableQueryResult> _macroInstances = null;
private readonly Assembly _asm;
private readonly IDataService _dataService;
private readonly IAppLogger _log;

#endregion Fields

#region Constructors

public MacroManager(Assembly asm, IAppLoggerFactory logFactory = null)
public MacroManager(Assembly asm, IAppLoggerFactory logFactory = null, IDataService dataService = null)
{
_asm = asm;
_log = Locator.Current.GetLogger<MacroManager>(logFactory);
_dataService = Locator.Current.GetService<IDataService>();
}

#endregion Constructors
Expand All @@ -50,6 +52,8 @@ where t.GetCustomAttributes<MacroAttribute>().Any()

var description = (type.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description;
alias.SetDescription(description);

_dataService.HydrateMacro(alias);

macroInstances.Add(name, alias);
_log.Info($"Found macro '{name}'");
Expand Down Expand Up @@ -95,7 +99,7 @@ public QueryResult Handle(QueryResult item)
}
else
{
/* Well, this a misconfigured macro, log it and forget it */
/* Well, this is a misconfigured macro, log it and forget it */
_log.Warning($"User has misconfigured a Macro with name '{src.FileName}'. Fix the name of the macro or remove the alias from the database.");
return null;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Lanceur.Infra/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public IEnumerable<QueryResult> Search(Cmdline query)
results.AddRange(res);
}

// Remember the query
foreach (var result in results) { result.Query = query; }

if (results.Any())
Expand All @@ -86,8 +87,14 @@ public IEnumerable<QueryResult> Search(Cmdline query)

// Updgrade alias to executable macro and return the result
var toReturn = _macroManager.Handle(results);

//Refresh the thumbnails
_thumbnailManager.RefreshThumbnails(toReturn);
return toReturn;

// Order the list and return the result
return toReturn
.OrderByDescending(e => e.Count)
.ThenBy(e => e.Name);
}
else { return DisplayQueryResult.SingleFromResult("No result found", iconKind: "AlertCircleOutline"); }
}
Expand Down
2 changes: 2 additions & 0 deletions src/Lanceur/Lanceur.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<None Remove="SQL\script-0.1.1.sql" />
<None Remove="SQL\script-0.1.sql" />
<None Remove="SQL\script-0.10.sql" />
<None Remove="SQL\script-0.2.sql" />
<None Remove="SQL\script-0.3.sql" />
<None Remove="SQL\script-0.4.1.sql" />
Expand All @@ -31,6 +32,7 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="SQL\script-0.10.sql" />
<EmbeddedResource Include="SQL\script-0.9.sql" />
<EmbeddedResource Include="SQL\script-0.8.sql" />
<EmbeddedResource Include="SQL\script-0.7.sql" />
Expand Down
12 changes: 12 additions & 0 deletions src/Lanceur/SQL/script-0.10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Add into the database the default values into the Settings table
*/

insert into settings (s_key, s_value)
select
'HotKey.ModifierKey',
s_value
from settings
where s_key = 'HotKey.ModifierKeys';

delete from settings where s_key = 'HotKey.ModifierKeys';
8 changes: 5 additions & 3 deletions src/Lanceur/Views/AppSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public AppSettingsViewModel(

private TimeSpan GetDelay()
{
_settings.Save(Context.AppSettings);
var delay = Context.AppSettings.RestartDelay;
var time = TimeSpan.FromMilliseconds(delay);
return time;
Expand All @@ -123,15 +122,18 @@ private ActivationContext OnActivate()

private async void OnSaveSettings()
{
//Save DB Path
//Save DB Path in property file
_stg[Setting.DbPath] = DbPath?.Replace("\"", "");
_stg.Save();

// Save hotkey & Session
// Save hotkey & Session in DB
Context.AppSettings.RestartDelay = RestartDelay;
Context.AppSettings.HotKey = HotKeySection;
if (CurrentSession is not null) { Context.AppSettings.IdSession = CurrentSession.Id; }

//Save settings
_settings.Save(Context.AppSettings);

TimeSpan time = GetDelay();
Toast.Information($"Application settings saved. Restart in {time.TotalMilliseconds} milliseconds");
await _delay.Of(time);
Expand Down

0 comments on commit 36c63c3

Please sign in to comment.