1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00

Added iOS example for add-to-app Pigeon (#722)

This commit is contained in:
gaaclarke
2021-03-03 21:04:28 -08:00
committed by GitHub
parent eab215f1b7
commit 5adf66ba65
31 changed files with 1440 additions and 188 deletions

View File

@@ -1,15 +1,16 @@
// Autogenerated from Pigeon (v0.1.0), do not edit directly.
// Autogenerated from Pigeon (v0.1.17), do not edit directly.
// See also: https://pub.dev/packages/pigeon
package dev.flutter.example.books;
import java.util.HashMap;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
import java.util.ArrayList;
import java.util.HashMap;
/** Generated class from Pigeon. */
@SuppressWarnings("unused")
public class Api {
/** Generated class from Pigeon that represents data sent in messages. */
@@ -26,9 +27,9 @@ public class Api {
public String getAuthor() { return author; }
public void setAuthor(String setterArg) { this.author = setterArg; }
private String description;
public String getDescription() { return description; }
public void setDescription(String setterArg) { this.description = setterArg; }
private String summary;
public String getSummary() { return summary; }
public void setSummary(String setterArg) { this.summary = setterArg; }
private String publishDate;
public String getPublishDate() { return publishDate; }
@@ -39,30 +40,36 @@ public class Api {
public void setPageCount(Long setterArg) { this.pageCount = setterArg; }
HashMap toMap() {
HashMap<String, Object> toMapResult = new HashMap<String, Object>();
HashMap<String, Object> toMapResult = new HashMap<>();
toMapResult.put("title", title);
toMapResult.put("subtitle", subtitle);
toMapResult.put("author", author);
toMapResult.put("description", description);
toMapResult.put("summary", summary);
toMapResult.put("publishDate", publishDate);
toMapResult.put("pageCount", pageCount);
return toMapResult;
}
static Book fromMap(HashMap map) {
Book fromMapResult = new Book();
fromMapResult.title = (String)map.get("title");
fromMapResult.subtitle = (String)map.get("subtitle");
fromMapResult.author = (String)map.get("author");
fromMapResult.description = (String)map.get("description");
fromMapResult.publishDate = (String)map.get("publishDate");
fromMapResult.pageCount = (map.get("pageCount") instanceof Integer) ? (Integer)map.get("pageCount") : (Long)map.get("pageCount");
Object title = map.get("title");
fromMapResult.title = (String)title;
Object subtitle = map.get("subtitle");
fromMapResult.subtitle = (String)subtitle;
Object author = map.get("author");
fromMapResult.author = (String)author;
Object summary = map.get("summary");
fromMapResult.summary = (String)summary;
Object publishDate = map.get("publishDate");
fromMapResult.publishDate = (String)publishDate;
Object pageCount = map.get("pageCount");
fromMapResult.pageCount = (pageCount == null) ? null : ((pageCount instanceof Integer) ? (Integer)pageCount : (Long)pageCount);
return fromMapResult;
}
}
/** Generated class from Pigeon that represents Flutter messages that can be called from Java.*/
public static class FlutterBookApi {
private BinaryMessenger binaryMessenger;
private final BinaryMessenger binaryMessenger;
public FlutterBookApi(BinaryMessenger argBinaryMessenger){
this.binaryMessenger = argBinaryMessenger;
}
@@ -71,12 +78,10 @@ public class Api {
}
public void displayBookDetails(Book argInput, Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<Object>(binaryMessenger, "dev.flutter.pigeon.FlutterBookApi.displayBookDetails", new StandardMessageCodec());
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.FlutterBookApi.displayBookDetails", new StandardMessageCodec());
HashMap inputMap = argInput.toMap();
channel.send(inputMap, new BasicMessageChannel.Reply<Object>() {
public void reply(Object channelReply) {
callback.reply(null);
}
channel.send(inputMap, channelReply -> {
callback.reply(null);
});
}
}
@@ -90,20 +95,18 @@ public class Api {
static void setup(BinaryMessenger binaryMessenger, HostBookApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<Object>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.cancel", new StandardMessageCodec());
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.cancel", new StandardMessageCodec());
if (api != null) {
channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() {
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();
try {
api.cancel();
wrapped.put("result", null);
}
catch (Exception exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
channel.setMessageHandler((message, reply) -> {
HashMap<String, HashMap> wrapped = new HashMap<>();
try {
api.cancel();
wrapped.put("result", null);
}
catch (Exception exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
@@ -111,21 +114,20 @@ public class Api {
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<Object>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.finishEditingBook", new StandardMessageCodec());
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.finishEditingBook", new StandardMessageCodec());
if (api != null) {
channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() {
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
channel.setMessageHandler((message, reply) -> {
HashMap<String, HashMap> wrapped = new HashMap<>();
try {
@SuppressWarnings("ConstantConditions")
Book input = Book.fromMap((HashMap)message);
HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();
try {
api.finishEditingBook(input);
wrapped.put("result", null);
}
catch (Exception exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
api.finishEditingBook(input);
wrapped.put("result", null);
}
catch (Exception exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
@@ -134,9 +136,9 @@ public class Api {
}
}
private static HashMap wrapError(Exception exception) {
HashMap<String, Object> errorMap = new HashMap<String, Object>();
HashMap<String, Object> errorMap = new HashMap<>();
errorMap.put("message", exception.toString());
errorMap.put("code", null);
errorMap.put("code", exception.getClass().getSimpleName());
errorMap.put("details", null);
return errorMap;
}

View File

@@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity() {
book.subtitle = volumeInfoJson.get("subtitle")?.asString
// Sorry co-authors, we're trying to keep this simple.
book.author = volumeInfoJson.getAsJsonArray("authors")[0].asString
book.description = volumeInfoJson.get("description").asString
book.summary = volumeInfoJson.get("description").asString
book.publishDate = volumeInfoJson.get("publishedDate").asString
book.pageCount = volumeInfoJson.get("pageCount").asLong
books.add(book)