Is it possible to convert Windows::Foundation::IAsyncOperation<T> to Concurrency::task<T>? - c++-winrt

As in the question, is it possible to convert between Windows::Foundation::IAsyncOperation and Concurrency::task?
If so, how to do it?
Update 1.
I'm migrating from C++/CX to C++/winrt and I have the following constructor in C++/CX code:
XamlComponentConstructor:XamlComponentConstructor()
{
InitializeComponent();
Uri^ uri = ref new Uri(L"URI");
create_task(StorageFile::GetFileFromApplicationUriAsync(uri))
.then([this](StorageFile^ file)
{
return Windows::Storage::FileIO::ReadTextAsync(file);
})
.then([this](Platform::String^ text)
{
// Text processing
x_firstTextBox->Text = var1;
x_secondTextBox->Text = var2;
})
}
The main problem is that there is no possibility to use corutines in the class constructor.
So the following code doesn't work:
XamlComponentConstructor:XamlComponentConstructor()
{
InitializeComponent();
const auto uri = winrt::Uri(L"URI");
const auto file { co_await StorageFile::GetFileFromApplicationUriAsync(uri) };
const auto text { co_await winrt::FileIO::ReadTextAsync(file) };
//Text processing
x_firstTextBox->Text = var1;
x_secondTextBox->Text = var2;
}
Do you have any idea how to change this code to work?

you can call do like following code.
XamlComponentConstructor:XamlComponentConstructor()
{
InitializeComponent();
const auto uri = winrt::Uri(L"URI");
StorageFile::GetFileFromApplicationUriAsync(uri).Completed(
[this](auto const& res, AsyncStatus const status)
{
StorageFile file { res.GetResults() };
winrt::FileIO::ReadTextAsync(file).Completed[this](
auto const& res, AsyncStatus const status)
{
auto text = res.GetResults();
//your code
}
}
);
}

Related

Unhandled Exception: type 'String' is not a subtype of type 'Future<String>' in type cast

I am gettting this error ,can anyone help me to sort out this error
static Future<String> get_video_lecture_subject(int schoolId,int classroom) async {
var body;
body = jsonEncode({
"school_id": schoolId,
"classroom": classroom,
});
final response = await http.post(
'https://b5c4tdo0hd.execute-api.ap-south-1.amazonaws.com/testing/get-video-lecture-subjects',
headers: {"Content-Type": "application/json"},
body: body,
);
print(response.body.toString());
return response.body.toString();
}
i have used above function in getpref() function
getpref() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int classNo = int.parse(prefs.getString("class")[0]);
int schoolId = prefs.getInt("school_id");
print("hello");
response=(await ApiService.get_video_lecture_subject(schoolId, classNo)) as Future<String> ;
print(response);
}
The expression:
(await ApiService.get_video_lecture_subject(schoolId, classNo)) as Future<String>
calls your method get_video_lecture_subject. That returns a Future<String>.
You then await that future, which results in a String.
Then you try to cast that String to Future<String>. That fails because a string is not a future.
Try removing the as Future<String>.
Check out this sample example and let me know if it works
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
getPrefs();
runApp(MyApp());
}
void getPrefs() async {
String value = await yourfunction();
print(value);
}
Future<String> yourfunction() {
final c = new Completer<String>();
// Below is the sample example you can do your api calling here
Timer(Duration(seconds: 1), () {
c.complete("you should see me second");
});
// Process your thigs above and send the string via the complete method.
// in you case it will be c.complete(response.body);
return c.future;
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child:
/* FutureBuilder<String>(
future: yourfunction(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data);
}
return CircularProgressIndicator();
}), */
Text('')),
));
}
}
Replace the line with this
final res = await ApiService.get_video_lecture_subject(schoolId, classNo);
Update: Change the variable name (maybe it's type was initialized as Future)

`TypeError: cannot write property 'implementation' of undefined` error in frida

I am trying to inject the below script using frida
setImmediate(function() { //prevent timeout
console.log("[*] Starting script");
Java.perform(function() {
var bClass = Java.use("sg.vantagepoint.uncrackable1.a");
bClass.onClick.implementation = function(v) {
console.log("[*] onClick called");
// do nothing
}
console.log("[*] onClick handler modified")
})
})
Throws the below error
Attaching...
[*] Starting script
TypeError: cannot write property 'implementation' of undefined
at [anon] (../../../frida-gum/bindings/gumjs/duktape.c:57636)
at /inject.js:10
at frida/node_modules/frida-java-bridge/lib/vm.js:11
at E (frida/node_modules/frida-java-bridge/index.js:346)
at frida/node_modules/frida-java-bridge/index.js:298
at frida/node_modules/frida-java-bridge/lib/vm.js:11
at frida/node_modules/frida-java-bridge/index.js:278
at /inject.js:13
at frida/runtime/core.js:55
command i am using frida -U -l inject.js owasp.mstg.uncrackable1
Below one is the decompiled code of the apk file.
package sg.vantagepoint.uncrackable1;
public class MainActivity extends Activity {
private void a(String str) {
AlertDialog create = new AlertDialog.Builder(this).create();
create.setTitle(str);
create.setMessage("This is unacceptable. The app is now going to exit.");
create.setButton(-3, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
System.exit(0);
}
});
create.setCancelable(false);
create.show();
}
Try to inject this script
setImmediate(function () {
console.log("[*] Starting script");
Java.perform(function () {
var bClass = Java.use("sg.vantagepoint.uncrackable1.MainActivity$1");
bClass.onClick.implementation = function (v) {
console.log("[*] onClick called.");
}
console.log("[*] onClick handler modified")
var aaClass = Java.use("sg.vantagepoint.a.a");
aaClass.a.implementation = function (arg1, arg2) {
var retval = this.a(arg1, arg2);
var password = ''
for (var i = 0; i < retval.length; i++) {
password += String.fromCharCode(retval[i]);
}
console.log("[*] Decrypted: " + password);
return retval;
}
console.log("[*] sg.vantagepoint.a.a.a modified");
});
});
This is happening because you're trying to override the onClick function of the main activity: sg.vantagepoint.uncrackable1.MainActivity$1 doesn't have an onClick() function. You can either cancel the function call entirely by overriding the a function to do nothing, or if you want to retain the logic but instead just cancel the display, should use Java.choose(...) API to choose the alert dialogue once it is instantiated and change it to fit your needs - for example, you can make the dialogue window cancelable, like this:
setImmediate(function() { //prevent timeout
console.log("[*] Starting script");
Java.perform(function() {
var bClass = Java.use("sg.vantagepoint.uncrackable1.a");
var oldImpl = bClass.a.implementation;
bClass.a.implementation = function(v){
console.log("Initial activity logic is launching");
oldImpl(v);
Java.choose("android.app.AlertDialog", {
onMatch: function(instance){
console.log("Found an alert dialog, making it cancelable")
instance.setCancelable(true);
}
onComplete: function(){
console.log("Done")
}
})
}
})
})

Flutter: how to mock a stream

I´m using the bloc pattern and now I came to test the UI. My question is: how to mock streams?
This is the code that I have:
I give to the RootPage class an optional mockBloc value and I will set it to the actual _bloc if this mockBloc is not null
class RootPage extends StatefulWidget {
final loggedOut;
final mockBlock;
RootPage(this.loggedOut, {this.mockBlock});
#override
_RootPageState createState() => _RootPageState();
}
class _RootPageState extends State<RootPage> {
LoginBloc _bloc;
#override
void initState() {
super.initState();
if (widget.mockBlock != null)
_bloc = widget.mockBlock;
else
_bloc = new LoginBloc();
if (widget.loggedOut == false)
_bloc.startLoad.add(null);
}
...
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _bloc.load,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: <Widget>
...
This is what I´ve tried:
testWidgets('MyWidget', (WidgetTester tester) async {
MockBloc mockBloc = new MockBloc();
MockTokenApi mockTokenApi = new MockTokenApi();
await tester.pumpWidget(new MaterialApp(
home: RootPage(false, mockBlock: mockBloc)));
when(mockBloc.startLoad.add(null)).thenReturn(mockBloc.insertLoadStatus.add(SettingsStatus.set)); //this will give in output the stream that the StreamBuilder is listening to (_bloc.load)
});
await tester.pump();
expect(find.text("Root"), findsOneWidget);
});
The result that I achieve is always to get:
The method 'add' was called on null
when _bloc.startLoad.add(null) is called
You can create a mock Stream by creating a mock class using mockito. Here's a sample on how one can be implemented.
import 'package:mockito/mockito.dart';
class MockStream extends Mock implements Stream<int> {}
void main() async {
var stream = MockStream();
when(stream.first).thenAnswer((_) => Future.value(7));
print(await stream.first);
when(stream.listen(any)).thenAnswer((Invocation invocation) {
var callback = invocation.positionalArguments.single;
callback(1);
callback(2);
callback(3);
});
stream.listen((e) async => print(await e));
}
Since the advent of null safety, you can do this as follows:
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'test_test.mocks.dart';
#GenerateMocks([Stream])
void main() {
test('foo', () async {
var stream = MockStream();
Stream<int> streamFunc() async* {
yield 1;
yield 2;
yield 3;
}
when(stream.listen(
any,
onError: anyNamed('onError'),
onDone: anyNamed('onDone'),
cancelOnError: anyNamed('cancelOnError'),
)).thenAnswer((inv) {
var onData = inv.positionalArguments.single;
var onError = inv.namedArguments[#onError];
var onDone = inv.namedArguments[#onDone];
var cancelOnError = inv.namedArguments[#cancelOnError];
return streamFunc().listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);
});
stream.listen((e) async => print(await e));
});
}
source

Razor pages custom routes

I am trying to get following routes so, i have the GlobalTemplatePageRouteModelConvention.
I used int? constraint to distinguish /1 from /ListJson handler but I am not happy with it, it is needlessly checking integer for each route.
Is it the ideal solution? How could i get it better?
Pages;
/Index.cshtml
/MainTestPage.cshtml
/TestDir/Index.cshtml
/TestDir/TestPage.cshtml
Handlers;
public void OnGet(int? id)
public void OnGetListJson(int? id)
Routes;
"/",
"/1",
"/ListJson",
"/ListJson/1",
"/MainTestPage",
"/MainTestPage/1",
"/MainTestPage/ListJson",
"/MainTestPage/ListJson/1",
"/TestDir",
"/TestDir/1",
"/TestDir/ListJson",
"/TestDir/ListJson/1",
"/TestDir/TestPage",
"/TestDir/TestPage/1",
"/TestDir/TestPage/ListJson",
"/TestDir/TestPage/ListJson/1",
Code;
public class GlobalTemplatePageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var selectorCount = model.Selectors.Count;
var list = new List<SelectorModel>();
var isIndexPage = model.ViewEnginePath.EndsWith("/Index", StringComparison.OrdinalIgnoreCase);
for (var i = 0; i < selectorCount; i++)
{
var selector = model.Selectors[i];
var template = selector.AttributeRouteModel.Template;
var isIndexRoute = template.EndsWith("Index", StringComparison.OrdinalIgnoreCase);
if (isIndexPage)
{
if (isIndexRoute)
{
list.Add(selector);
}
else
{
model.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Order = -2,
Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{id:int?}")
}
});
selector.AttributeRouteModel.Order = -1;
selector.AttributeRouteModel.Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{handler?}/{id?}");
}
}
else
{
model.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Order = -4,
Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{id:int?}")
}
});
selector.AttributeRouteModel.Order = -3;
selector.AttributeRouteModel.Template = AttributeRouteModel.CombineTemplates(selector.AttributeRouteModel.Template, "{handler?}/{id?}");
}
}
foreach (var selector in list)
model.Selectors.Remove(selector);
}
}
builder.AddRazorPagesOptions(o => {
o.Conventions.Add(new GlobalTemplatePageRouteModelConvention());
}
Finally, i prefer following route template convention so int constraints are eliminated and performance is not lost because of them.
Templates are specified globally, not specified within #page directive of each page.
Index routes(/Index/1) are intended for unnamed handlers, OnGet etc.
It works for following routes;
"/",
"/Index/1",
"/ListJson",
"/ListJson/1",
"/MainTestPage",
"/MainTestPage/Index/1",
"/MainTestPage/ListJson",
"/MainTestPage/ListJson/1",
"/TestDir",
"/TestDir/Index/1",
"/TestDir/ListJson",
"/TestDir/ListJson/1",
"/TestDir/TestPage",
"/TestDir/TestPage/Index/1",
"/TestDir/TestPage/ListJson",
"/TestDir/TestPage/ListJson/1",
GlobalTemplatePageRouteModelConvention;
Index route causes mismatches so it is removed.
builder.AddRazorPagesOptions(o =>
{
o.Conventions.Add(new GlobalTemplatePageRouteModelConvention());
});
public class GlobalTemplatePageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var isIndexPage = model.ViewEnginePath.EndsWith("/Index", StringComparison.OrdinalIgnoreCase);
foreach (var selector in model.Selectors.ToList())
{
var template = selector.AttributeRouteModel.Template;
if (isIndexPage)
{
var isIndexRoute = template.EndsWith("Index", StringComparison.OrdinalIgnoreCase);
if (isIndexRoute)
{
model.Selectors.Remove(selector);
continue;
}
}
selector.AttributeRouteModel.Template =
AttributeRouteModel.CombineTemplates(template,
"{handler?}/{id?}");
}
}
}

composing html file in aurelia

I'd like to achieve something similar as "include" in android but in aurelia:
How to inject a plain html file content into my view, with binding evaluated within the parent View, and without using a custom element?
Binding innerhtml is not enough as, according to the doc, the bindings expressions are bypassed.
As already said by Ashley, using <compose view="./your-view.html"></compose> element will work with an existing HTML file and it will inherit the parent context.
If you want to compose HTML dynamically (from a file, database, or built-up programmatically) then using the ViewCompiler will give you the best performance and flexibility, as this is one layer less than compose compared to how aurelia builds custom elements internally.
I gave a similar answer to a different (but related) question here:
Aurelia dynamic binding
You'd use the text plugin to load your HTML file as text into a variable, and then pass that to the ViewCompiler. I have a custom element for this which, in terms of performance, is probably not better than compose but it does allow for more control when working with raw html as input and you could do your own performance optimizations specific to your situation as needed:
import * as markup from "text!./your-element.html";
export class SomeViewModel {
constructor() {
this.markup = markup;
}
}
And the view:
<template>
<dynamic-html html.bind="markup"></dynamic-html>
</template>
For completeness sake, here is the custom element I encapsulated the ViewCompiler in:
import {
customElement,
TaskQueue,
bindable,
ViewCompiler,
ViewSlot,
View,
ViewResources,
Container,
ViewFactory,
inlineView,
inject,
DOM
} from "aurelia-framework";
#customElement("dynamic-html")
#inlineView("<template><div></div></template>")
#inject(DOM.Element, TaskQueue, Container, ViewCompiler)
export class DynamicHtml {
#bindable()
public html: string;
public element: HTMLElement;
private tq: TaskQueue;
private container: Container;
private viewCompiler: ViewCompiler;
private runtimeView: View;
private runtimeViewSlot: ViewSlot;
private runtimeViewFactory: ViewFactory;
private runtimeViewAnchor: HTMLDivElement;
constructor(element, tq, container, viewCompiler) {
this.element = <HTMLElement>element;
this.tq = tq;
this.container = container;
this.viewCompiler = viewCompiler;
}
public bindingContext: any;
public overrideContext: any;
public bind(bindingContext: any, overrideContext: any): void {
this.bindingContext = bindingContext;
this.overrideContext = overrideContext;
if (this.html) {
this.htmlChanged(this.html, undefined);
}
}
public unbind(): void {
this.disposeView();
this.bindingContext = null;
this.overrideContext = null;
}
public needsApply: boolean = false;
public isAttached: boolean = false;
public attached(): void {
this.runtimeViewAnchor = <HTMLDivElement>this.element.firstElementChild;
this.isAttached = true;
if (this.needsApply) {
this.needsApply = false;
this.apply();
}
}
public detached(): void {
this.isAttached = false;
this.runtimeViewAnchor = null;
}
private htmlChanged(newValue: string, oldValue: void): void {
if (newValue) {
if (this.isAttached) {
this.tq.queueMicroTask(() => {
this.apply();
});
} else {
this.needsApply = true;
}
} else {
if (this.isApplied) {
this.disposeView();
}
}
}
private isApplied: boolean = false;
private apply(): void {
if (this.isApplied) {
this.disposeView();
}
this.compileView();
}
private disposeView(): void {
if (this.runtimeViewSlot) {
this.runtimeViewSlot.unbind();
this.runtimeViewSlot.detached();
this.runtimeViewSlot.removeAll();
this.runtimeViewSlot = null;
}
if (this.runtimeViewFactory) {
this.runtimeViewFactory = null;
}
if (this.runtimeView) {
this.runtimeView = null;
}
this.isApplied = false;
}
private compileView(): void {
this.runtimeViewFactory = createViewFactory(this.viewCompiler, this.container, this.html);
this.runtimeView = createView(this.runtimeViewFactory, this.container);
this.runtimeViewSlot = createViewSlot(this.runtimeViewAnchor);
this.runtimeViewSlot.add(this.runtimeView);
this.runtimeViewSlot.bind(this.bindingContext, this.overrideContext);
this.runtimeViewSlot.attached();
this.isApplied = true;
}
}
function createViewFactory(viewCompiler: ViewCompiler, container: Container, html: string): ViewFactory {
if (!html.startsWith("<template>")) {
html = `<template>${html}</template>`;
}
let viewResources: ViewResources = container.get(ViewResources);
let viewFactory = viewCompiler.compile(html, viewResources);
return viewFactory;
}
function createView(viewFactory: ViewFactory, container: Container): View {
let childContainer = container.createChild();
let view = viewFactory.create(childContainer);
return view;
}
function createViewSlot(containerElement: Element): ViewSlot {
let viewSlot = new ViewSlot(containerElement, true);
return viewSlot;
}