Can you return a result back to Flutter from Native Kotlin Bridge - flutter

at the moment I have a Flutter App that when I press a button it opens a Native Kotlin activity(mainActivity), once this executes I automatically open a SecondActivity that extends AppCompatActivity, the issue becomes is that I want to return a bool from the SecondActivity back to Flutter.
At the moment when the process finishes i use "finish()" to close my Native code and return to flutter successfully but I'd like to pass a result to Flutter.
I know how to do it from the mainActivity as it uses the FlutterActivity directly and can pass "result.success()", but from the different activity I can't find a solution.
I was wondering if somebody can tell me how to pass the bool from the secondActivity to Flutter.
Thank you very much everyone in advance!

It works exactly the same. Store Result in class property and call one of its methods from onActivityResult when the second activity finishes. Something like this:
class YourPlugin ... {
...
private var pendingResult: Result? = null
...
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
...
pendingResult = result
activity?.startActivityForResult(...)
...
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean {
...
pendingResult?.success(<result>)
...
return false
}

Related

Navigate to page on start in .NET Maui app

Seems like a simple question, but I haven't been able to find a simple answer. Essentially I want to choose which page in the app to start on based on some stored state. I added a GoToAsync call in the AppShell constructor, but this didn't work--which makes sense because the AppShell hasn't been fully constructed yet.
I found this answer, but it feels like it kind of skirts around the issue:
Maui AppShell - Navigate on Open
Where is the best place to inject some code that will run once on startup and can successfully navigate a .NET Maui app to a chosen page?
After playing around with overrides, it seems like overriding Application.OnStart works! Shell.Current is set at this point and navigation works.
Here's additional code that allows for asynchronous initialization and uses a Loading Page until the initialization is complete:
using MyApp.Services;
using MyApp.UI;
namespace MyApp;
public partial class App : Application
{
ConfigurationProviderService m_configProvider;
public App(ConfigurationProviderService configProvider)
{
m_configProvider = configProvider;
InitializeComponent();
MainPage = new LoadingPage();
}
protected override void OnStart()
{
var task = InitAsync();
task.ContinueWith((task) =>
{
MainThread.BeginInvokeOnMainThread(() =>
{
MainPage = new AppShell();
// Choose navigation depending on init
Shell.Current.GoToAsync(...);
});
});
base.OnStart();
}
private async Task InitAsync()
{
await m_configProvider.InitAsync();
}
}

Is there anyway in Kotlin something like Dart's Completer behavior?

I'm creating plugin for flutter. And face a issue from Android native.
I'm using Kotlin for Android and want to use value from platform channel from Flutter.
It's hard to explain in English. Here's a code.
fun Foo(): Any? {
methodChannel.invokeMethod(
"method",
null,
object : ErrorLogResult("tag") {
override fun success(result: Any?) {
super.success(result)
// Get result.
}
})
// return result from result callback after callback is done.
}
It can be like below in dart code.
Future<dynamic> Foo() async {
final completer = Completer();
someFunctionWithCallback((result) {
completer.complete(result);
});
return completer.future;
}
Old question, but maybe someone will come across this issue, as I had.
Yes, there is something similar: CompletableDeferred
Example pseudocode:
#Volatile
private var completableMeetingService = CompletableDeferred<MeetingService>()
// we want to join meeting, but have to wait for initialization
completableMeetingService.await().joinMeetingWithParams
// completing deffered in some listener
completableMeetingService.complete(zoomSDK.meetingService)

How to correctly scroll after item inserted into PagedListAdapter

I am using the Android Arch PagedListAdapter class. The issue I have run into is that since my app is a chat style app, I need to scroll to position 0 when an item is inserted. But the PagedListAdapter finds the diffs and calls necessary methods on the background thread so it seems that there is no dependable way to call layoutManager.scrollToPosition(0)
If anyone has any ideas on how I could scroll at the right time, it would be greatly appreciated. Thanks!
Here is my Room Dao:
#Dao
abstract class MessagesDao {
#Query("SELECT ...")
abstract fun getConversation(threadId: String): DataSource.Factory<Int, Conversation>
}
and then the LivePagedListBuilder:
LivePagedListBuilder(database.messagesDao.getConversation(threadId), PagedList.Config.Builder()
.setInitialLoadSizeHint(20)
.setPageSize(12)
.setEnablePlaceholders(true)
.build()).build()
It turns out that there is this beautiful thing called AdapterDataObsever which lets you observe the PagedListAdapter item calls. Here is how I was able to use it to solve the problem.
conversationAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
manager.scrollToPosition(0)
}
}
})

GWT TestCase: Simulating clicking a button on my page

I'm using GWT 2.4 with JUnit 4.8.1. When writing my class that extends GWTTestCase, I want to simulate clicking on a button on the page. Currently, in my onModuleLoad method, this button is only a local field ...
public void onModuleLoad() {
final Button submitButton = Button.wrap(Document.get().getElementById(SUBMIT_BUTTON_ID));
...
// Add a handler to send the name to the server
GetHtmlHandler handler = new GetHtmlHandler();
submitButton.addClickHandler(handler);
How do I simulate clicking on this button from the GWTTestCase? Do I have to expose this button as a public member accessor is there a more elegant way to access it? Here is what I have in my test case so far ...
public class GetHtmlTest extends GWTTestCase {
// Entry point class of the GWT application being tested.
private Productplus_gwt productPlusModule;
#Override
public String getModuleName() {
return "com.myco.clearing.productplus.Productplus_gwt";
}
#Before
public void prepareTests() {
productPlusModule = new Productplus_gwt();
productPlusModule.onModuleLoad();
} // setUp
#Test
public void testSuccessEvent() {
// TODO: Simulate clicking on button
} // testSuccessEvent
}
Thanks, - Dave
It can be as easy as buttonElement.click() (or ButtonElement.as(buttonWidget.getElement()).click(), or ButtonElement.as(Document.get().getElementById(SUBMIT_BUTTON_ID)).click())
But remember that a GWTTestCase doesn't run in your own HTML host page, but an empty one, so you'll first have to insert your button within the page before simulating your module's load.
gwt-test-utils seems to be the perfect framework to answer your need. Instead of inheriting from GWTTestCase, extend the gwt-test-utils GwtTest class and implement your click test with the Browser class, like shown in the getting starting guide :
#Test
public void checkClickOnSendMoreThan4chars() {
// Arrange
Browser.fillText(app.nameField, "World");
// Act
Browser.click(app.sendButton);
// Assert
assertTrue(app.dialogBox.isShowing());
assertEquals("", app.errorLabel.getText());
assertEquals("Hello, World!", app.serverResponseLabel.getHTML());
assertEquals("Remote Procedure Call", app.dialogBox.getText());
}
If you want to keep your button private, you'd be able to retrieve it by introspection. But my advice is to make you view's widgets package protected and to write your unit test in the same package so it could access them. It's more convinent and refactoring-friendly.
gwt-test-utils provide introspection convinence. For example, to retrieve the "dialogBox" field which could have been private, you could have do this :
DialogBox dialogBox = GwtReflectionUtils.getPrivateFieldValue(app, "dialogBox");
But note that using GwtReflectionUtils is not mandatory. gwt-test-utils allows you to use ANY java classes in GWT client side tests, without restriction :)
You can do it like this:
YourComposite view = new YourComposite();
RootPanel.get().add(view);
view.getSubmitButton.getElement().<ButtonElement>cast().click();

Add a DoubleClickHandler to a FixedWidthGrid

so I got a FixedWidthGrid table which is made from a pagingtable
FixedWidthGrid dataTable = x.getDataTable();
I could add alot of handlers to the dataTables rows like selected or sort policies.
but I cant add a double click handler ... any idea's ?!
thank you
I do have a class which I made to try to add a double click handler but it didn't work.
class:
public class DoubleClickTable extends FixedWidthGrid implements HasDoubleClickHandlers {
public DoubleClickTable() {
super();
}
public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
return addDomHandler(handler, DoubleClickEvent.getType());
}
}
Thank you so much for your help.
Here is the way I'd implement a double click handler :
Create a time attribute to the class
In the ClickHandler method, fill a variable with the current time.
Compare the variable with the time attribute of the class (var - attr) and if the result is equal or smaller than one second, execute the method that handle double clicks.
This should do the trick. If you want more precision, don't hesitate to ask.