I'm using Deeplinking on Xamarin.iOS.
I can't find a pattern to handle the link if the user has disconnected from my app.
In this case I need to redirect to the login page while waiting for the login to complete before leaving SceneDelegate.ContinueUserActivity, right?
What I've tried so far is to set a Nito.AsyncEx.AsyncAutoResetEvent in my login ViewController, then wait for it with the help of AsyncHelpers. But the login UI is frozen.
[Export("scene:continueUserActivity:")]
public void ContinueUserActivity(UIScene scene, NSUserActivity userActivity)
{
....
if (notAuthenticated)
{
// The user is not authenticated
// Redirect to the Login Page
LoginViewController lvc = new LoginViewController(true);
if (Window.RootViewController == null)
{
var navController = new UINavigationController(lvc);
Window.RootViewController = navController;
}
else
{
UINavigationController rootViewController = ((UINavigationController)Window.RootViewController);
rootViewController.PopToRootViewController(true);
rootViewController.PushViewController(lvc, true);
}
Window.MakeKeyAndVisible();
AsyncHelpers.RunSync(async() => await lvc.LoginFinishedOrCancelled.WaitAsync());
...
}
}
As far as I know there is no way to make ContinueUserActivity async aware.
You could invoke the line AsyncHelpers.RunSync(async() => await lvc.LoginFinishedOrCancelled.WaitAsync()); in your login page instead of in SceneDelegate .
public bool isNeedLoad {get;set;}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if(isNeedLoad)
{
// ...AsyncHelpers.RunSync(async() => await lvc.LoginFinishedOrCancelled.WaitAsync());
isNeedLoad = false ;
}
}
LoginViewController lvc = new LoginViewController(true){isNeedLoad = true};
if (Window.RootViewController == null)
{
var navController = new UINavigationController(lvc);
Window.RootViewController = navController;
Window.MakeKeyAndVisible();
}
else
{
UINavigationController rootViewController = ((UINavigationController)Window.RootViewController);
rootViewController.PopToRootViewController(true);
rootViewController.PushViewController(lvc, true);
}
Related
I am displaying 2 second fading animation in view class, after 2-secs, I want to go to viewmodel class to run which load login page or signup page logic
right now its skipping 2 sec animation and going directly to viewmodel. idk what the issue is here
if i delete line BindingContext = new SplashscreenViewModel(); than animation shows up but it doesnt go to viewmodel class
SplashscreenPage.xaml.cs - view class
public SplashscreenPage ()
{
InitializeComponent ();
Animation();
BindingContext = new SplashscreenViewModel();
}
async void Animation()
{
ssImage.Opacity = 0;
await Task.WhenAll(
ssImage.FadeTo(1, 2000),
ssImage.ScaleTo(1.1, 2000)
);
}//end of method
Splashscreen - viewmodel class
public SplashscreenViewModel()
{
WhichPageToLoad();
}
async void WhichPageToLoad()
{
var getToken = await SecureStorage.GetAsync("Save_Pin_1");
if(getToken == null)
{
var route = $"{ nameof(SignupPage)}";
await Shell.Current.GoToAsync(route);
}
else
{
var route = $"{ nameof(LoginPage)}";
await Shell.Current.GoToAsync(route);
}
}
there is really no point in having a VM class or assigning the BindingContext since you are not doing any databinding. You can just call the navigation code after the animation completes. You cannot make async calls from the constructor, so moving the animation and navigation login into OnAppearing will allow you to make an async call
public SplashscreenPage ()
{
InitializeComponent ();
}
async override void OnAppearing()
{
ssImage.Opacity = 0;
await Task.WhenAll(
ssImage.FadeTo(1, 2000),
ssImage.ScaleTo(1.1, 2000)
);
var getToken = await SecureStorage.GetAsync("Save_Pin_1");
if(getToken == null)
{
var route = $"{ nameof(SignupPage)}";
await Shell.Current.GoToAsync(route);
}
else
{
var route = $"{ nameof(LoginPage)}";
await Shell.Current.GoToAsync(route);
}
}
Periodically, the application begins to update itself. There is a constant call in the logs:
[ForceDarkHelper] updateByCheckExcludeList: pkg: com.companyname.manimobile activity: crc64d14753dcc52b83b4.MainActivity#a894c70
[ForceDarkHelper] updateByCheckExcludeList: pkg: com.companyname.manimobile activity: crc64d14753dcc52b83b4.MainActivity#a894c70
[ForceDarkHelper] updateByCheckExcludeList: pkg: com.companyname.manimobile activity: crc64d14753dcc52b83b4.MainActivity#a894c70
[ForceDarkHelper] updateByCheckExcludeList: pkg: com.companyname.manimobile activity: crc64d14753dcc52b83b4.MainActivity#a894c70
When this happens, if, for example, you open the menu , it closes itself, if something is filled in, it is cleared, the page is updated. There are no timers in the code. I'm testing the app on Xiaomi Redmi. I repeat sometimes it happens sometimes it doesn't. What is it?
I do not know what the problem is, but occasionally, it happens that the application throws the fingerprint to the page. It is intermittent. Sometimes everything works fine. That is, I go through the fingerprint, the next page opens, everything is normal and a second after 5 I am again thrown to the page where you need to enter the fingerprint.
Code for the authorization page:
public authentification()
{
try
{
InitializeComponent();
bool auth = CrossSettings.Current.GetValueOrDefault("authorized", false);
if (auth == false) { CheckAuth(); }
else
{
Application.Current.MainPage = new MasterLk();
}
}
catch { }
}
async void CheckAuth()
{
try
{
var avail = await CrossFingerprint.Current.IsAvailableAsync();
if (!avail)
{
CrossSettings.Current.GetValueOrDefault("authorized", true);
Application.Current.MainPage = new MasterLk();
}
else
{
var request = new AuthenticationRequestConfiguration("NeedAuth", "-");
var result = await CrossFingerprint.Current.AuthenticateAsync(request);
if (result.Authenticated)
{
CrossSettings.Current.GetValueOrDefault("authorized", true);
Application.Current.MainPage = new MasterLk();
}
else
{
CheckAuth();
}
}
}
catch { }
}
On the page where it throws it there is a ListView with a binding:
public class OrdersViewModel : BaseViewModel
{
private Table oldLoan;
private bool isRefreshing;
private readonly string clientId;
public bool IsRefreshing
{
get
{
return isRefreshing;
}
set
{
isRefreshing = value;
OnPropertyChanged("IsRefreshing");
}
}
public ICommand RefreshCommand { get; set; }
public ObservableCollection<Table> Loans { get; set; }
public void ShowOrHideLoan(Table loan)
{
if (oldLoan == loan)
{
loan.IsExpanded = !loan.IsExpanded;
Reload(loan);
}
else
{
if (oldLoan != null)
{
oldLoan.IsExpanded = false;
Reload(oldLoan);
}
loan.IsExpanded = true;
Reload(loan);
}
oldLoan = loan;
}
private void Reload(Table loan)
{
var index = Loans.IndexOf(loan);
Loans.Remove(loan);
Loans.Insert(index, loan);
}
public async Task LoadDataAsync()
{
IsRefreshing = true;
Loans.Clear();
try
{
var loans = await ConnectAPI.GetOrdersAsync(clientId);
await Task.Delay(1000);
foreach (var item in loans)
{
Loans.Add(item);
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
finally
{
oldLoan = null;
IsRefreshing = false;
}
}
public OrdersViewModel(string clientId)
{
IsRefreshing = false;
this.clientId = clientId;
Loans = new ObservableCollection<Table>();
RefreshCommand = new Command(async () =>
{
await LoadDataAsync();
});
Task.Run(async () => await LoadDataAsync());
}
}
That is, whenever the [ForceDarkHelper] updateByCheckExcludeList: pkg: com.companyname.manimobile activity: crc64d14753dcc52b83b4 event appears.MainActivity#a894c70
Throws it to the print page...
and if you stay on this page, it is updated after a while.
MIUI 12 has made an intelligent dark theme. The system itself repaints the applications if they do not support the dark theme. Apparently this service is ForceDarkHelper. And ExcludeList is in the settings a list of applications that cannot be repainted
I am trying to write a xamarin app that will display a login page before a master detail page but I am running into issues.
Right now I have my app.xaml calling an appbootstrapper as follows:
public App()
{
this.InitializeComponent();
RxApp.SuspensionHost.CreateNewAppState = () => new AppBootstrapper();
RxApp.SuspensionHost.SetupDefaultSuspendResume();
this.MainPage = RxApp.SuspensionHost
.GetAppState<AppBootstrapper>()
.CreateMainPage();
}
/// <summary>Gets the Router associated with this Screen.</summary>
public RoutingState Router { get; } = new RoutingState();
With the app bootstrapper as follows:
public class AppBootstrapper : ReactiveObject, IScreen
{
public AppBootstrapper(IMutableDependencyResolver dependencyResolver = null)
{
SetupLogging();
this.RegisterParts(dependencyResolver ?? Locator.CurrentMutable);
this.Router.Navigate.Execute(new LoginPageViewModel(this));
}
/// <summary>Gets the Router associated with this Screen.</summary>
public RoutingState Router { get; } = new RoutingState();
public Page CreateMainPage()
{
return new RoutedViewHost();
}
private static void SetupLogging()
{
var logger = new Logger { Level = LogLevel.Debug };
Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger));
}
private void RegisterParts(IMutableDependencyResolver dependencyResolver)
{
dependencyResolver.RegisterConstant(this, typeof(IScreen));
dependencyResolver.Register(() => new LoginPage(), typeof(IViewFor<LoginPageViewModel>));
dependencyResolver.RegisterConstant(new LoginService(), typeof(ILoginService));
}
}
This gets me to my login screen no problem, and I can perform my login operation. Then, once login is successful, I try to navigate to the master detail page, but this is where I run into issues.
public LoginPageViewModel(IScreen screen)
{
this.loginService = Locator.Current.GetService<ILoginService>();
this.HostScreen = screen ?? Locator.Current.GetService<IScreen>();
this.PrepareObservables();
}
........................................................
private void PrepareObservables()
{
...
this.LoginCommand = ReactiveCommand.CreateFromTask(
async execute =>
{
var loginSuccessful = await this.loginService.Login(this.Username, this.Password);
if (loginSuccessful)
{
this.HostScreen.Router.NavigateBack.Execute().Subscribe();
}
}, canExecuteLogin);
...
You can see that my login command is trying to perform a navigate and reset to go to the Main Page (which is my master detail page). This is not working and is resulting in an unhandled exception stating:
An object implementing IHandleObservableErrors has errored, thereby breaking its observable pipeline. To prevent this, ...>
Does anyone know what to do here? I need a good pattern for handling the use case of Login -> Master Detail Page in Xamarin Forms using ReactiveUI. Thanks.
this.LoginCommand = ReactiveCommand.CreateFromTask(
async execute =>
{
var loginSuccessful = await this.loginService.Login(this.Username, this.Password);
if (loginSuccessful)
{
this.HostScreen.Router.NavigateBack.Execute().Subscribe();
}
}, canExecuteLogin);
The above code is navigating back on successful login. I think you mean to use Router.NavigateAndReset.Execute(new MainPageViewModel()).Subscribe();
I have implement a bot using Microsoft Bot builder SDK v-4 (pre-release). To manage the conversation flow I have used two simple dialogs-
GreetingDialog - DialogBegin: To greet the user first time
public Task DialogBegin(DialogContext dc, IDictionary<string, object> dialogArgs = null)
{
var state = dc.Context.GetConversationState<EchoState>();
string greetMessage = string.Format("Hi, I am {0}.", _botName);
dc.Context.SendActivity(greetMessage);
IList<CardAction> suggestedActions = new List<CardAction>
{
//some card action suggestions
};
var activity = MessageFactory.SuggestedActions(suggestedActions, text: "Please select the area of conversation.");
dc.Context.SendActivity(activity);
dc.End();
return Task.CompletedTask;
}
ConversationDialog - DialogBegin: To continue the subsequent conversation after the user has been greeted
public Task DialogBegin(DialogContext dc, IDictionary<string, object> dialogArgs = null)
{
string activity = "test";
dc.Context.SendActivity(activity);
dc.Continue();
return Task.CompletedTask;
}
I am calling the GreetingDialog in the ConversationUpdate event and the ConversationDialog in the subsequent message received event, within the OnTurn method in my Bot class.
OnTurn event in my Bot class:
public async Task OnTurn(ITurnContext context)
{
var state = context.GetConversationState<EchoState>();
var dialogCtx = _dialogs.CreateContext(context, state);
if (context.Activity.Type == ActivityTypes.ConversationUpdate)
{
//Greet user first time
if (context.Activity.MembersAdded[0].Id == "default-user")
{
return;
}
if (!context.Responded)
{
var args = new Dictionary<string, object>
{
["greetingArgs"] = context.Activity.Text
};
await dialogCtx.Begin("greetingDialog", args);
}
}
else if (context.Activity.Type == ActivityTypes.Message)
{
await dialogCtx.Continue(); //this line is supposed to execute Begin the active dialog again??
//if (!context.Responded)
if(dialogCtx.ActiveDialog == null || !dialogCtx.Context.Responded)
{
var args = new Dictionary<string, object>
{
["conversationArgs"] = context.Activity.Text
};
await dialogCtx.Begin("conversationDialog", args);
}
}
}
Using the above code, I get redirected to ConversationDialog but it only happens through await dialogCtx.Begin("conversationDialog", args);. Isn't it supposed to redirect to DialogBegin of the Active dialog when I do await dialogCtx.Continue();? I can see the Active dialog is 'conversationDialog' and the debugger steps over through await dialogCtx.Continue();. Any help with this please?
I think I figured it out. We can implement the IDialogContinue interface for our Dialog class like this-
public class QnADialog : IDialog, IDialogContinue
{
public Task DialogBegin(DialogContext dc, IDictionary<string, object> dialogArgs = null)
{
string activity = "test";
dc.Context.SendActivity(activity);
//dc.Continue();
return Task.CompletedTask;
}
public Task DialogContinue(DialogContext dc)
{
dc.Context.SendActivity("dc continue");
dc.Context.Responded = true;
return Task.CompletedTask;
}
}
Then we can use the DialogContinue method to handle the DialogContext.Continue() from the calling code.
We are trying to use Touch Id with iOS using our Xamarin Forms application.
In our Xamarin Forms Application, in the App.Xaml.cs constructor we are using an interface to reference the iOS native touch id implementation:
bool _authenticatedWithTouchID = DependencyService.Get<ITouchID>().AuthenticateUserIDWithTouchID();
if (_authenticatedWithTouchID)
{
MainPage = new NavigationPage(new MainPage());
}
else
{
MainPage = new NavigationPage(new LoginPage());
}
This is the interface signature within Forms Application:
public interface ITouchID
{
bool AuthenticateUserIDWithTouchID();
}
This is the implementation of the interface within the iOS project:
[assembly: Dependency(typeof(TouchID))]
namespace GetIn.iOS
{
public class TouchID : ITouchID
{
public bool AuthenticateUserIDWithTouchID()
{
bool outcome = false;
var context = new LAContext();
if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out NSError AuthError))
{
var replyHandler = new LAContextReplyHandler((success, error) => {
Device.BeginInvokeOnMainThread(() => {
if (success)
{
outcome = true;
}
else
{
outcome = false;
}
});
});
context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Logging with touch ID", replyHandler);
};
return outcome;
}
}
}
We get a response from the outcome variable (which is true if user is authenticated) but that is not being passed back to the forms application.
We have also tried using async tasks with no luck.
Is there a recommended way we do this? We are trying to keep this as simple as possible.
You need to change your code to handle asynchronous behavior.
public class TouchID : ITouchID
{
public Task<bool> AuthenticateUserIDWithTouchID()
{
var taskSource = new TaskCompletionSource<bool>();
var context = new LAContext();
if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out NSError AuthError))
{
var replyHandler = new LAContextReplyHandler((success, error) => {
taskSource.SetResult(success);
});
context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Logging with touch ID", replyHandler);
};
return taskSource.Task;
}
}
Remember add the using on top
using System.Threading.Tasks;
And change your interface declaration
public interface ITouchID
{
Task<bool> AuthenticateUserIDWithTouchID();
}
And finally your Xamarin.Forms code...
var touchId = DependencyService.Get<ITouchID>();
var _authenticatedWithTouchID = await touchId.AuthenticateUserIDWithTouchID();
if (_authenticatedWithTouchID)
{
MainPage = new NavigationPage(new MainPage());
}
else
{
MainPage = new NavigationPage(new LoginPage());
}
Managed to get this working by using the async changes above (although you can do this without using the async method), and then doing the following:
By moving and adapting the following code from the app.xaml.cs to our MainPage.xaml.cs (our tabbed page) constructor.
var touchId = DependencyService.Get<ITouchID>();
var _authenticatedWithTouchID = await
touchId.AuthenticateUserIDWithTouchID();
if (_authenticatedWithTouchID)
{
//Do Nothing as on this page
}
else
{
//Go back to login page
Navigation.InsertPageBefore(new LoginPage(), this);
await Navigation.PopAsync();
}