1
0
mirror of https://github.com/XFox111/SimpleOTP.git synced 2026-04-22 08:00:45 +03:00

Created OTPFactory overview (markdown)

2021-06-01 18:10:23 +03:00
parent 9de0323ff1
commit c182d1ce13
+61
@@ -0,0 +1,61 @@
In case you are working on an authenticator app, you can use `OTPFactory` to handle code re-generation and timer updates
## Configure factory
To configure factory just simply pass an `OTPConfiguration` object to constructor:
```csharp
OTPFactory factory = new (config);
```
You can update configuration any time by updating `Configuration` property
## Subscribe to events
To keep code up to date on the UI side, subscribe to `CodeUpdated` event:
```csharp
factory.CodeUpdated += (newCode) =>
{
// Update your data here
this.codeLabel.Text = newCode.GetCode();
};
```
Also it could be useful to subscribe to `PropertyChanged` event, since it occurs every second to keep code countdown up to date (countdown update frequency is set on instance constructor by parameter `timeUpdateInterval` in milliseconds)
```csharp
OTPFactory factory = new (config); // use 'new (config, 100)' to update countdown every 100 milliseconds
factory.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(factory.TimeLeft))
this.countdownLabel.Text = factory.TimeLeft.ToString();
};
```
## Use MVVM pattern with OTPFactory
`OTPFactory` object is MVVM ready, so you can use it as is in your ViewModel
```csharp
public class MyViewModel : ViewModelBase
{
private OTPFactory _factory = new (OTPConfiguration.GenerateConfiguration("FoxDev Studio", "eugene@xfox111.net")); // Example value
public OTPFactory Factory
{
get => _factory;
set => SetProperty(ref _factory, value);
}
}
```
```xaml
<ContentPage
...
xmlns:vm="Your.ViewModel.Namespace">
<ContentPage.BindingContext>
<vm:MyViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding Factory.Configuration.Issuer}" FontSize="28"/>
<Label Text="{Binding Factory.Configuration.AccountName}" FontAttributes="Italic"/>
<Label Text="{Binding Factory.CurrentCode.Code}"/>
<Label Text="{Binding Factory.TimeLeft}"/>
</StackLayout>
...
</ContentPage>
```
## Dispose OTPFactory
When you don't need `OTPFactory` anymore it is important to dispose it to prevent memory leaks. To do so just simply call `OTPFactory.Dispose()` or use it with `using keyword`