How to close the MauiCommunityToolkit Popup from Viewmodel - popup

I want close a CommunityToolkit Popup in my Viewmodel.
I tried using a WeakReferenceMessenger to receive a message like this:
public mypopup()
{
InitializeComponent();
WeakReferenceMessenger.Default.Register<string, string>(this, "popup", (r, d) =>
{
Debug.WriteLine(message: "received message");
if (d == "close")
{
WeakReferenceMessenger.Default.Unregister<string>(this);
MainThread.BeginInvokeOnMainThread(() => { this.Close(); });
}
});
}
And somewhere else I use this to send a message
WeakReferenceMessenger.Default.Send<string, string>("close", "popup");
The 1st call works. And the SECOND time it will raise a System.NullReferenceException in MauiPopup.windows.cs Function void CleanUp() Target.ContextFlyout = null;
I also tried like this in message receive:
MainThread.BeginInvokeOnMainThread(() => { this.Close(); });
the same happens.
I wonder if there is a solution or a better way to close popup from somewhere else without transferring the handle of popup.

I did this in my code
PopupPage p = new PopupPage();
Application.Current.MainPage.ShowPopup(p);
await new TaskFactory().StartNew(() => { Thread.Sleep(5000); });
p.Close();

Related

Page.OnAppearing() Firing Twice

When I push a new instance of my page onto the navigation stack, OnAppearing() fires twice and consequently two DeliveryNotePicker pages are created. There's nothing in the call stack that gives me any clues. Why might this be happening?
protected override async void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<ScannerMessages, Barcode>(this, "ScannerData", (sender, arg) =>
{
Device.BeginInvokeOnMainThread(() => { ItemScanned(arg.Value); });
});
try
{
if (picklist == null)
{
// Attempt to retrieve an existing picklist:
picklist = (List<Pick>)Application.Current.Properties[PicklistProperty];
branchName.Text = (string)Application.Current.Properties[BranchNameProperty];
NextPick();
}
}
catch (KeyNotFoundException)
{
// Create a new picklist:
await Navigation.PushModalAsync(new DeliveryNotePicker());
}
}
I've also encountered this bug, think it's caused when pushing a new modal within the OnAppearing function
Workaround is to either check the current page isn't the same as in Saamer's last comment or using a private counter to check number of opens

Android Room with RXJava2; onNext() of emitter is not properly triggered

I am switching from async tasks to rxjava2 and have some issues with my code tests.
I have a room table of elements that have a certain monetary amount. On a usercontrol that is called DisplayCurrentBudget, a sum of all amounts should be displayed. This number must refresh everytime a new element is inserted. I tackled the requirement in two ways, but both produce the same result: My code does not care if the database is updated, it only updates when the fragment is recreated (onCreateView).
My first attempt was this:
//RxJava2 Test
Observable<ItemS> ItemObservable = Observable.create( emitter -> {
try {
List<ItemS> movies = oStandardModel.getItemsVanilla();
for (ItemS movie : movies) {
emitter.onNext(movie);
}
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});
DisposableObserver<ItemS> disposable = ItemObservable.
subscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread()).
subscribeWith(new DisposableObserver<ItemS>() {
public List<ItemS> BadFeelingAboutThis = new ArrayList<ItemS>();
#Override
public void onNext(ItemS movie) {
// Access your Movie object here
BadFeelingAboutThis.add(movie);
}
#Override
public void onError(Throwable e) {
// Show the user that an error has occurred
}
#Override
public void onComplete() {
// Show the user that the operation is complete
oBinding.DisplayCurrentBudget.setText(Manager.GetBigSum(BadFeelingAboutThis).toString());
}
});
I already was uncomfortable with that code. My second attempt produces the exact same result:
Observable<BigDecimal> ItemObservable2 = Observable.create( emitter -> {
try {
BigDecimal mySum = oStandardModel.getWholeBudget();
emitter.onNext(mySum);
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});
DisposableObserver<BigDecimal> disposable = ItemObservable2.
subscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread()).
subscribeWith(new DisposableObserver<BigDecimal>() {
#Override
public void onNext(BigDecimal sum) {
// Access your Movie object here
oBinding.DisplayCurrentBudget.setText(sum.toString());
}
#Override
public void onError(Throwable e) {
// Show the user that an error has occurred
}
#Override
public void onComplete() {
// Show the user that the operation is complete
}
});
Any obvious issues with my code?
Thanks for reading, much appreciate it!
Edit:
I was asked what Manager.GetBigSum does, it actually does not do much. It only adds BigDecimal-Values of an Item list.
public static BigDecimal GetBigSum(List<ItemS> ListP){
List<BigDecimal> bigDList = ListP.stream().map(ItemS::get_dAmount).collect(Collectors.toList());
return bigDList.stream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
Further, I simplified the query. But it still does not care about DB updates, only about fragment recreation:
Single.fromCallable(() -> oStandardModel.getItemsVanilla())
.map(Manager::GetBigSum)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
e -> oBinding.DisplayCurrentBudget.setText(e.toString())
);
Your rx logic has no error. That should be internal error in your getWholeBudget.
But why you write rx so complex?
For your case, you can just write:
Single.fromCallable(() -> oStandardModel.getItemsVanilla())
.map(Manager::GetBigSum)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
e -> oBinding.DisplayCurrentBudget.setText(sum.toString()),
e -> log.error(e));
I solved it this way:
oStandardModel.getItemJointCatLive().observe(this, new Observer<List<ItemJointCat>>() {
#Override
public void onChanged(#Nullable final List<ItemJointCat> oItemSP) {
Single.fromCallable(() -> oStandardModel.getWholeBudget())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
e -> oBinding.DisplayCurrentBudget.setText(e.toString())
);
}
});
My mistake was that I assumed RXjava2 does not need an onchanged event...now i just use onchanged event of livedata observer to trigger a simple rxjava2 query.
Do you think there is anything wrong with that approach?

checking paragraph property loses the selection

in my vsto addin i have some simple code on a timer
private void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!dialogopen & Application.Documents.Count > 0)
{
var doc = Application.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>().ToList();
foreach (var obj in pars2)
{
if (obj.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText )//PROBLEM HERE
{
};
}
}
}
as soon as it reaches the line that checks the outlinelevel, even if i dont do a thing, the selection in the activedocument gets lost
of course the user cant use a plugin which keeps on canceling his selection...
googling didnt give me a thing
thanks
EDIT1
I tried making a static function for checking the styles, but it did not help. Here's the code
static public class Helpers
{
static public Word.Paragraph checkPars(List<Word.Paragraph> pars)
{
return pars.FirstOrDefault();//(x) => x.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText);
}
}
As you can see, I had to remove the actual check, since it was causing the cursor to blink and lose selection
please advise
We use the Application.ScreenUpdating = true; and this keep the selection for all properties in Paragraph except for the Range property.
Then, we tried to access the range via Reflection and this was the solution.
Range rng = (Range)typeof(Paragraph).GetProperty("Range").GetValue(comObj);
We tried to eliminate querying ActiveDocument thinking that that might have had side-effects that were causing the problem but that was not the case.
Then, we confirmed that the selection was not "lost" and screen-updating is the only problem, so we tried restoring the UI with Application.ScreenRefresh and while it did work, it causes the screen to flicker every time the timer fires and this is not good enough.
Finally, knowing that it's only a UI problem, I tried simply switching off Application.ScreenUpdating...
in ThisAddin
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Timer timer = new Timer(2000);
timer.Elapsed += (s, t) =>
{
var scrnUpdating = Application.ScreenUpdating;
Application.ScreenUpdating = false;
MainTimer.onElapsed(Application, t);
if (scrnUpdating)
Application.ScreenUpdating = true;
};
timer.Start();
}
In another class library (note that it's static, I still think this is the best way)...
public static class MainTimer
{
public static void onElapsed (Word.Application state, System.Timers.ElapsedEventArgs e)
{
if (state.Documents.Count > 0)
{
var doc = state.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>()
.Where(p => p.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText)
.Select(p => p) // do stuff with the selected parragraphs...
.ToList();
}
}
}
And this works for me.
The selection remains and is displayed in the UI without flicker.
I also eliminated some premature enumeration from your code: you don't meed the .ToList() before the foreach.

Show success message and then redirect to another page after a timeout using PageFlow

How can I show a success message and then redirect the user to another page after a timeout of e.g. 5 seconds?
I need this for the login page after a successful login. I tried the following and I can see the warning message on login failure, but not the success message on login success. It shows immediately the target page.
public String check(){
if (username.equals("test") && password.equals("test")) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));
return "Success";
}else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));
return "Failure";
}
}
I'm using Seam's PageFlow for navigation.
I have a
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
on the login page.
It is one of the utilities of Flash. Instead of
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));:
simply use this code
FacesContext facesContext = FacesContext.getCurrentInstance();
Flash flash = facesContext.getExternalContext().getFlash();
flash.setKeepMessages(true);
flash.setRedirect(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));
First of all, with the code you posted you won't see the FacesMessage before the redirect, you'll see it after the redirect. But also, in order to make that happen you'll need to add a filter, because messages are lost when you redirect. This is the code for the filter you need (don't forget to declare it in web.xml):
public class MultiPageMessagesSupport implements PhaseListener {
private static final long serialVersionUID = 1250469273857785274L;
private static final String sessionToken = "MULTI_PAGE_MESSAGES_SUPPORT";
#Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
/*
* Check to see if we are "naturally" in the RENDER_RESPONSE phase. If we
* have arrived here and the response is already complete, then the page is
* not going to show up: don't display messages yet.
*/
#Override
public void beforePhase(final PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
int msg = this.saveMessages(facesContext);
if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
if (!facesContext.getResponseComplete()) {
this.restoreMessages(facesContext);
}
}
}
/*
* Save messages into the session after every phase.
*/
#Override
public void afterPhase(final PhaseEvent event) {
if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES ||
event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS ||
event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
FacesContext facesContext = event.getFacesContext();
int msg = this.saveMessages(facesContext);
}
}
#SuppressWarnings("unchecked")
private int saveMessages(final FacesContext facesContext) {
List<FacesMessage> messages = new ArrayList<FacesMessage>();
for (Iterator<FacesMessage> iter = facesContext.getMessages(null); iter.hasNext();) {
messages.add(iter.next());
iter.remove();
}
if (messages.isEmpty()) {
return 0;
}
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
if (existingMessages != null) {
existingMessages.addAll(messages);
} else {
sessionMap.put(sessionToken, messages);
}
return messages.size();
}
#SuppressWarnings("unchecked")
private int restoreMessages(final FacesContext facesContext) {
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> messages = (List<FacesMessage>) sessionMap.remove(sessionToken);
if (messages == null) {
return 0;
}
int restoredCount = messages.size();
for (Object element : messages) {
facesContext.addMessage(null, (FacesMessage) element);
}
return restoredCount;
}
}
If this doesn't work for you, and you need to show the message before, then you'll have to something like the following: make the method return void, invoke it through ajax, and after adding the success message invoke some javascript method that will wait a couple of seconds and then make the redirect (maybe by programmatically clicking a hidden button that redirects to next page).
In my opinion this is not worth the trouble, you will just delay the login process. Anyway user will know tha tlogin succeeded because he will be redirect to home page (or whatever page you send him to)
EDIT:
the messages are displayed in the page when the method finishes, so waiting in the managed bean method won't work. after adding the FacesMessage, use
RequestContext.getCurrentInstance().execute("waitAndRedirect()");
And in your xhtml, you'll need to have a javascript function similar to this:
function waitAndRedirect() {
setTimeout(function() {
hiddenButtonId.click();
}, 2000);
}
where hiddenButtonId is the ID of a p:button which redirects to home page and is hidden (display:none)
But again, this a nasty approach, in my opinion there's no need to do this, you will just delay the login process.
you can not declare MultiPageMessagesSupport in the web.xml you must declare MultiPageMessagesSupport in the faces-config.xml. por example:
enter code here
<lifecycle>
<phase-listener>your.package.MultiPageMessagesSupport</phase-listener>
</lifecycle>

Why is Finally not called on my DownloadStringAsync Observable?

I'm using FromEventPattern and I want to be able to do some cleanup in the finally block of my observable. Right now the finally block isn't being called. It's my understanding I have to call OnCompleted...somewhere but not sure how to implement that. Some code from my Silverlight program:
public IObservable<string> StartDownload ( string uri )
{
WebClient wc = new WebClient();
var o = Observable.FromEventPattern<DownloadStringCompletedEventArgs>( wc, "DownloadStringCompleted" )
.Select( s => s.EventArgs.Result );
wc.DownloadStringAsync( new Uri( uri ) );
return o;
}
public void TestRx ()
{
var anobs = StartDownload( "http://www.google.com" );
anobs
.Subscribe( stuff =>
{
// do stuff
} );
anobs
.Finally( () =>
{
// not called?
} );
}
UPDATE:
Apparently my assumption that OnCompleted() would fix my problem was wrong. I tried changing StartDownload to the following and Finally is still not called. What is going on here?
public IObservable<string> StartDownload ( string uri )
{
WebClient wc = new WebClient();
var subject = new AsyncSubject<string>();
wc.DownloadStringCompleted += ( sender, e ) =>
{
if ( e.Error != null )
subject.OnError( e.Error );
subject.OnNext( e.Result );
subject.OnCompleted();
};
wc.DownloadStringAsync( new Uri( uri ) );
return subject;
}
You need to write your subscription code like this:
anobs
.Finally(() =>
{
// will now be called. ;-)
})
.Subscribe(stuff =>
{
// do stuff
});
Your Finally call returns a new observable that is "appended" to the anobs observable - it does nothing to actually modify the anobs observable at all. If you subscribe directly to anobs it doesn't know anything about the Finally call so it can't be called.
Remember, that Rx operators are exactly like LINQ operators, in that they don't actually do anything until you do something to "ForEach" it (i.e. evaluate it). In Rx, Subscribe is like ForEach. Enigmativity is correct up here (but I wanted to explain a bit further as to why he's correct!)