Skip to content

Conversation

@taooceros
Copy link
Member

@taooceros taooceros commented May 11, 2021

close #388

TODO:

  • Implement a auto call to Save all plugin's json storage that has registered
  • Implement a better (safer) store to replace Dictionary<Type, object>
  • Finish up some settings

public void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "") => Log.Exception(className, message, e, methodName);

private readonly Dictionary<Type, dynamic> PluginJsonStorages = new Dictionary<Type, dynamic>();
private readonly Dictionary<Type, object> PluginJsonStorages = new Dictionary<Type, object>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dynamic seems not working as intended, so I move back to direct cast

@jjw24
Copy link
Member

jjw24 commented May 12, 2021

Did you want me to review and merge, or you still working on the other two todos?

@taooceros
Copy link
Member Author

Did you want me to review and merge, or you still working on the other two todos?

still working on it. There are a few things that need to be cleaned up.
I am struggling with the second one, but it would be fine to not do it.

@taooceros
Copy link
Member Author

I didn't find a better way to replace the Dictionary<Type, object> and reflection for registration. So just review the current one.

@taooceros taooceros requested a review from jjw24 May 12, 2021 11:49
@jjw24
Copy link
Member

jjw24 commented May 12, 2021

Version bumps for plugins please :)

@taooceros
Copy link
Member Author

Version bumps for plugins please :)

Sure

@jjw24 jjw24 added the bug Something isn't working label May 12, 2021
Comment on lines -116 to -120
public void Save()
{
_storage.Save();
}

Copy link
Member

@jjw24 jjw24 May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed? settings still needs to be saved right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a auto save for all registered jsonstorage, so that we don't need to configure isavable for setting, unless they are going to save something else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private void SaveAllJsonStorage()
{
foreach (var value in _pluginJsonStorages.Values)
{
var method = value.GetType().GetMethod("Save");
method?.Invoke(value, null);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. would this not be a doubling up then, with PluginManager.Save() call?

  2. also you need to move this SaveAllJsonStorage() call to here right so when Flow's settings window closed you want to save all plugin settings:

    public void Save()
    {
    foreach (var vm in PluginViewModels)
    {
    var id = vm.PluginPair.Metadata.ID;
    Settings.PluginSettings.Plugins[id].Disabled = vm.PluginPair.Metadata.Disabled;
    Settings.PluginSettings.Plugins[id].Priority = vm.Priority;
    }
    PluginManager.Save();
    _storage.Save();
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. do we want to remove ISavable?
  2. if it's auto saved, might want to document it somewhere right? for those that develop plugins

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method is called in SaveAllJsonStorage, so it is impossible to directly call that method here, or else will become infinity recurse.
The call for

PluginManager.Save(); 
_storage.Save(); 

do save all plugin setting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i see. hmm how do save plugin settings when user closes settings window? PluginManager.Save() only saves plugin settings where the interface ISavable is applied and _storage.Save() saves Flow's own settings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of passing the api to the view, which is not even used, we can and should pass it to the viewmodel, this then allows you to call save plugin settings seperately from the view

public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginManager.Save() only saves plugin settings where the interface ISavable is applied and _storage.Save() saves Flow's own settings.

I have modified PluginManager.Save() to include saving plugins settings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public static void Save()
        {
            foreach (var plugin in AllPlugins)
            {
                var savable = plugin.Plugin as ISavable;
                savable?.Save();
            }
            API.SavePluginSettings();
        }

@jjw24
Copy link
Member

jjw24 commented May 12, 2021

Not related to this change but I think we should update the comment to give an example scenario where you will need to override the original class instance, i.e. use the override save method and when you just need to use save the original instance. Since you are in this space maybe you could just give a good explanation in the comment that would be awesome.

/// <summary>
/// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow.Launcher
/// This method will save the original instance loaded with LoadJsonStorage.
/// </summary>
/// <typeparam name="T">Type for Serialization</typeparam>
/// <returns></returns>
void SaveJsonStorage<T>() where T : new();
/// <summary>
/// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow.Launcher
/// This method will override the original class instance loaded from LoadJsonStorage
/// </summary>
/// <typeparam name="T">Type for Serialization</typeparam>
/// <returns></returns>
void SaveJsonStorage<T>(T settings) where T : new();

@taooceros
Copy link
Member Author

I have updated the comment. In fact, I don't believe we may actually need the one to overwrite it, because the supposed process is to load and use the loaded file (which will be a new when none exist), and save the original one. Should we remove it?

Also, do you think we should modify the name specifically to setting because the file will be stored inside the setting folder.

By the way, would you please give me the access for OAuth from Jetbrains IDE integration? Sometimes I may use rider instead of Visual Studio or vscode. Thank you!

@jjw24
Copy link
Member

jjw24 commented May 13, 2021

By the way, would you please give me the access for OAuth from Jetbrains IDE integration? Sometimes I may use rider instead of Visual Studio or vscode. Thank you!

Done

@jjw24
Copy link
Member

jjw24 commented May 13, 2021

I have updated the comment. In fact, I don't believe we may actually need the one to overwrite it, because the supposed process is to load and use the loaded file (which will be a new when none exist), and save the original one. Should we remove it?

i am unsure when there will be a circumstance where we needed to save a new instance to the original, so i would say yes

Also, do you think we should modify the name specifically to setting because the file will be stored inside the setting folder.

yes

@taooceros
Copy link
Member Author

By the way, would you please give me the access for OAuth from Jetbrains IDE integration? Sometimes I may use rider instead of Visual Studio or vscode. Thank you!

Done

Thank you!

@jjw24 jjw24 added the review in progress Indicates that a review is in progress for this PR label May 14, 2021
/// <summary>
/// Save Flow's plugins settings
/// </summary>
void SavePluginSettings();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between this method and SaveSettingJsonStorage()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SaveSettingJsonStorage is for a specific Type (or T as generic), but this is for all plugin.

/// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow.Launcher
/// Save JsonStorage for current plugin's setting. This is the method used to save settings to json in Flow.Launcher
/// This method will save the original instance loaded with LoadJsonStorage.
/// This API call is for manually Save. Flow will automatically save all setting that has registered.
Copy link
Member

@jjw24 jjw24 May 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it will be better to also explain how setting gets registered in the comment so plugin devs do not need to dig into code to find out.

Comment on lines 23 to 26
public void Save()
{
storage.Save();
Context.API.SaveSettingJsonStorage<Settings>();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more question, why does this have to call the save directly?

Copy link
Member Author

@taooceros taooceros May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explorer plugin contains a call for save every time a new item in its setting is edited. I didn't change it.
It is not ISavable, so I believe it won't be called twice. However, do you think we should remove that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, it's not ISavable, just calls infra's save directly. Nah don't worry about it for this PR, using the API is at least better. Let's get this merged in.

@jjw24 jjw24 enabled auto-merge May 18, 2021 22:04
@jjw24 jjw24 added enhancement New feature or request and removed bug Something isn't working review in progress Indicates that a review is in progress for this PR labels May 18, 2021
@jjw24 jjw24 merged commit be8bb8b into dev May 18, 2021
@jjw24 jjw24 deleted the MigrateDirectPluginJsonStorage branch May 18, 2021 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate Direct call to PluginJsonStorage to API.save()

3 participants