How to convert Mapbox's Expression.get()'s result into a color suitable for fillColor()? - mapbox-android

The following code doesn't seem to apply the value of fillColorProp from my JSON file
JSON content
"fillColorProp": "#FF0000"
Java
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
// ...
FillLayer fillLayer = new FillLayer("abc123", source.getId()).withProperties(
fillColor(get("fillColorProp"))
);
I tried to get convert the type of the property a few different ways but couldn't find a syntax that would compile:
fillColor(color(literal(get("fillColorProp"))));
fillColor(Color.parseColor(literal(get("fillColorProp"))));
How do I properly convert Mapbox's Android Expression.get()'s result into a color suitable for passing as an argument to PropertyFactory.fillColor(), PropertyFactory.lineColor(), etc.?

Expression.toColor() seems to work:
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.toColor;
// ...
fillColor(toColor(get("fillColor")))

Related

Re-export all imports with changed type definition

We have a git submodule of React components written in poorly typed TS. It has a single index file, which re-exports everything like this:
export * from './blocks'
Then, in the main app, we use import * as allBlocks from '../packages/blocks' or import {SomeBlock} ....
But now, I have added some type declarations to the mentioned index file, which, however, need to be imported separately to use.
Is there a way to tell TS to re-export as the new type?
for example something like this:
export * from './blocks' as TheNewType
This is how the index file looks currently.
export * from './blocks' // I want to export all as different type
// all this is just for the types
import * as allBlocks from './blocks'
type AllBlocks = typeof allBlocks
type BlockNames = keyof AllBlocks
export type BlockComponents = {
[key in BlockNames]: BlockComponent<ExtractComponentGenerics<AllBlocks[key]>>
}
type BlockComponent<P = {}> = React.ComponentType<P> & BlockComponentStatics<P>
/** This interface types some static properties used by our library, that are not normally present on React.ComponentType */
interface BlockComponentStatics<P = {}> {
getInitialProps?(): P | Promise<P>;
// ...
}
function assertIsBlocks(val: any): asserts val is BlockComponents {/*...*/}
assertIsBlocks(allBlocks)
export allBlocks // This unfortunatelly produces named export, which I don't want.
export default allBlocks // I don't want this neither. I want the * export.
Since this package doesn't come as a module, I am not sure if and how I can use Ambient module declaration or Module augmentation.

Java 8 Stream: How to get a new list from one list property in a list

I'm new to java 8 and meet a problem trouble me a lot:
I've a List like below:
List<objMain>
class objMain{
Long rid;
List<objUser> list;
String rname;
}
class objUser{
String userid;
}
now,I want get a new List like below:
List<objUserMain>
class objUserMain{
Long rid;
String rname;
String userid;
}
How can I do this by java 8 stream? Thanks anyone answer me.
It seems as though you want to map each objMain instance into an objUserMain type.
In order to accomplish the task at hand, you'll need to utilise flatMap along with map then collect to a list implementation.
Assuming you have getters and setter where necessary then you can perform the following logic to get the required result.
List<objUserMain> result =
objMainsList.stream()
.flatMap(obj -> obj.getList().stream().map(e -> {
objUserMain user = new objUserMain();
user.setRid(obj.getRid());
user.setRname(obj.getRname());
user.setUserid(e.getUserid());
return user;
})).collect(Collectors.toList());
You can do this using streams the following way
public static List convert(List existing, Function func) {
return existing.stream().map(func).collect(Collectors.toList());
}
The above method will help you to convert your list from one object type to another. The parameters to this is the initial object you want to convert and the method you want to use for conversion. Import the following in you main class
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.function.*;
Now call the method for conversion the following way in the main class that you are trying to convert
List result=convert(newList,
l->{
objUserMain r=new objUserMain();
r.rid=l.rid;
r.rname=l.rname;
r.userid=l.list.get(0).userid;
return r;});
System.out.println(result.get(0).rid);
System.out.println(result.get(0).rname);
System.out.println(result.get(0).userid);
The above is a mixture of lambda functions and streams to allow you to convert object from one type to another. Let me know if you have any queries then I am happy to help. Happy coding.

Load image directly from library without class in Actionscript 3

I have an image in my Flash library (called sun.png) and I want to load this directly into an instance of a Bitmap (or equivalent) without using Actionscript linkage and creating its own class.
I have tried:
var sun:Bitmap = flash.utils.getDefinitionByName("sun.png") as Bitmap;
and:
var sun:Bitmap = this.loaderInfo.applicationDomain.getDefinition("sun.png") as Bitmap;
But neither of these work (I believe these functions are used for loading classes)
How can I do this without creating a class or loading from the filesystem?
Or is this even possible?
No, it is impossible. getDefinitionByName is just like getDefinition in current ApplicationDomain, except one of them through error instead of returning null.
Both of they are use finding only linked symbols. If you don't link the bitmap it will not even be compiled into output flash movie.
You can create some aliases in a static object like:
You define them once then use the objects as many times as you want. In the case of Bitmaps, after you get the bitmap, use the bitmapdata in a new Bitmap() because you need more different references with that same bitmapData.
class Refs
{
public static const PNG_0 : Bitmap= flash.utils.getDefinitionByName("com.example.MyImage") as Bitmap;
public static const PNG_1 : Bitmap= flash.utils.getDefinitionByName("com.example.OtherImage") as Bitmap;
}
what i think you want to do: first, make sure the image file is in the same folder as your FLA file (no need to have it in the library). then you can get the bitmap by writing this:
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.net.URLRequest;
bitmap:Bitmap;
ldr = new Loader ();
ldr.load( new URLRequest ( "sun.png" ));
ldr.contentLoaderInfo.addEventListener ( Event.COMPLETE, onloaded );
function onloaded ( e:Event ):void {
e.target.removeEventListener ( Event.COMPLETE, onloaded );
bmd = new BitmapData ( ldr.width, ldr.height );
bmd.draw ( ldr.content );
bitmap = new Bitmap (bmd);
}

Key Input using GtkD

I'm a beginner of both gtk and GtkD.
Now, I'm trying to get input from keyboard with reference to this .
But, It seems that three years have made some changes in Toolkits.
I wrote code below. However, I got strange values of ev in callback function.
I could not see any prospect of resolution with going alone.
So, could you show me where to modify?
I appreciate you in advance, and also your patient with my poor English.
I'm using gtkD-2.1.1 and gtk+3.2.3.
this is the full code:
import std.stdio;
import gtkc.gdktypes;
import gtk.MainWindow;
import gtk.Widget;
import gdk.Event;
import gtk.Main;
class Window : MainWindow{
immutable width = 200;
immutable height = 200;
this(){
super("input test");
setDefaultSize(width,height);
setEvents(EventMask.KEY_PRESS_MASK); // Actually I don't know how this works
auto callback_func = cast(bool delegate(Event,Widget))&get_key; // I doubt this cast
this.addOnKeyPress(callback_func);
showAll();
}
bool get_key(GdkEventKey* ev, Widget widget){
writefln("sender %s", widget);
writefln("type %x",ev.type);
writefln("window* %x",ev.window);
writefln("send_event %x",ev.sendEvent);
writefln("time %x",ev.time);
writefln("state %x",ev.state);
writefln("keyval %x",ev.keyval);
writefln("length %x",ev.length);
writefln("gchar* %x",ev.string);
writefln("hardware_keycode %x",ev.hardwareKeycode);
writefln("group %x",ev.group);
writefln("is_modifier %x\n",ev.bitfield0);
return true;
}
}
void main(string[] args){
Main.init(args);
auto win = new Window();
Main.run();
}
Yes, that cast is wrong. I guess that Signature with GdkEventKey* is outdated. Change your get_key to take an Event and you should get proper results:
...
auto call = &get_key;
...
bool get_key(Event e, Widget widget){
GdkEventKey* ev = e.key();
...
I have never done anything with GtkD, and this is just the result of some glances over the docs. So, it's probably not best practice, but it should get you back on the road.

Call a method of a web component and respond to events

I'm working on a Dart project where I have created a custom element with the Web_ui package that has some animation. What I was hoping to do is to have within the dart code for the element something like this....
class MyElement extends WebComponent {
...
void StartAnimation() { ... }
...
}
and then in the main() function of the dart app itself I have something like this...
void main() {
MyElement elm = new MyElement();
elm.StartAnimation(); // Kicks off the animation
}
The Dart editor tells me that Directly constructing a web component is not currently supported. It then says to use WebComponent.forElement -- but I'm not clear on how to use that to achieve my goal.
While you can't yet import web components into a Dart file, you can access them via query() and .xtag. xtag gives you a reference the web component instance that the element is associated with. You do have to be careful that you allow the Web UI setup to complete so that xtag is given a value.
Here's an example:
import 'dart:async';
import 'dart:html';
import 'package:web_ui/web_ui.dart';
main() {
Timer.run(() {
var myElement = query('#my-element').xtag;
myElement.startAnimation();
});
}
This will get better with the ability to import components, directly subclass Element and maybe some lifecycle events that guarantee that you get the correct class back from a query(). This is what the exemple should look like in the future:
import 'dart:async';
import 'dart:html';
import 'package:web_ui/web_ui.dart';
import 'package:my_app/my_element.dart';
main() {
MyElement myElement = query('#my-element');
myElement.startAnimation();
}

Resources