Add a pigeon sample that demonstrates a "realistic" integration scenario with middleware and business logic (#465)
1
add_to_app/android_books/app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
49
add_to_app/android_books/app/build.gradle
Normal file
@@ -0,0 +1,49 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion "29.0.3"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "dev.flutter.example.books"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation "com.squareup.okhttp3:okhttp:4.7.2"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'androidx.core:core-ktx:1.3.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation "androidx.activity:activity-ktx:1.1.0"
|
||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||
implementation 'com.google.android.material:material:1.1.0'
|
||||
implementation 'com.google.code.gson:gson:2.8.6'
|
||||
implementation project(path: ':flutter')
|
||||
testImplementation 'junit:junit:4.13'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
|
||||
}
|
||||
21
add_to_app/android_books/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.flutter.example.books
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class MainActivityTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("dev.flutter.example.books", appContext.packageName)
|
||||
}
|
||||
|
||||
// The app should be hermetic (with offline books JSON) before adding
|
||||
// more tests.
|
||||
}
|
||||
28
add_to_app/android_books/app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="dev.flutter.example.books">
|
||||
|
||||
<application
|
||||
android:name=".BookApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".FlutterBookActivity"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
|
||||
android:hardwareAccelerated="true"
|
||||
android:windowSoftInputMode="adjustResize" />
|
||||
</application>
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,143 @@
|
||||
// Autogenerated from Pigeon (v0.1.0), 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;
|
||||
|
||||
/** Generated class from Pigeon. */
|
||||
public class Api {
|
||||
|
||||
/** Generated class from Pigeon that represents data sent in messages. */
|
||||
public static class Book {
|
||||
private String title;
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String setterArg) { this.title = setterArg; }
|
||||
|
||||
private String subtitle;
|
||||
public String getSubtitle() { return subtitle; }
|
||||
public void setSubtitle(String setterArg) { this.subtitle = setterArg; }
|
||||
|
||||
private String author;
|
||||
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 publishDate;
|
||||
public String getPublishDate() { return publishDate; }
|
||||
public void setPublishDate(String setterArg) { this.publishDate = setterArg; }
|
||||
|
||||
private Long pageCount;
|
||||
public Long getPageCount() { return pageCount; }
|
||||
public void setPageCount(Long setterArg) { this.pageCount = setterArg; }
|
||||
|
||||
HashMap toMap() {
|
||||
HashMap<String, Object> toMapResult = new HashMap<String, Object>();
|
||||
toMapResult.put("title", title);
|
||||
toMapResult.put("subtitle", subtitle);
|
||||
toMapResult.put("author", author);
|
||||
toMapResult.put("description", description);
|
||||
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");
|
||||
return fromMapResult;
|
||||
}
|
||||
}
|
||||
|
||||
/** Generated class from Pigeon that represents Flutter messages that can be called from Java.*/
|
||||
public static class FlutterBookApi {
|
||||
private BinaryMessenger binaryMessenger;
|
||||
public FlutterBookApi(BinaryMessenger argBinaryMessenger){
|
||||
this.binaryMessenger = argBinaryMessenger;
|
||||
}
|
||||
public interface Reply<T> {
|
||||
void reply(T reply);
|
||||
}
|
||||
public void displayBookDetails(Book argInput, Reply<Void> callback) {
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<Object>(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Generated interface from Pigeon that represents a handler of messages from Flutter.*/
|
||||
public interface HostBookApi {
|
||||
void cancel();
|
||||
void finishEditingBook(Book arg);
|
||||
|
||||
/** Sets up an instance of `HostBookApi` to handle messages through the `binaryMessenger` */
|
||||
static void setup(BinaryMessenger binaryMessenger, HostBookApi api) {
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<Object>(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);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<Object>(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) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static HashMap wrapError(Exception exception) {
|
||||
HashMap<String, Object> errorMap = new HashMap<String, Object>();
|
||||
errorMap.put("message", exception.toString());
|
||||
errorMap.put("code", null);
|
||||
errorMap.put("details", null);
|
||||
return errorMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package dev.flutter.example.books
|
||||
|
||||
import android.app.Application
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.embedding.engine.FlutterEngineCache
|
||||
import io.flutter.embedding.engine.dart.DartExecutor
|
||||
|
||||
class BookApplication: Application() {
|
||||
companion object {
|
||||
const val ENGINE_ID = "book_engine"
|
||||
}
|
||||
|
||||
private lateinit var flutterEngine: FlutterEngine
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
// This application reuses a single FlutterEngine instance throughout.
|
||||
// Create the FlutterEngine on application start.
|
||||
flutterEngine = FlutterEngine(this).apply{
|
||||
dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
|
||||
}
|
||||
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package dev.flutter.example.books
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import dev.flutter.example.books.BookApplication.Companion.ENGINE_ID
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import java.util.HashMap
|
||||
|
||||
/**
|
||||
* This {@link FlutterActivity} class repackages Kotlin-Dart interop using the Pigeon IPC mechanism.
|
||||
* It repackages Flutter/Dart-side functionalities in standard Android API style, passing
|
||||
* arguments in and out of the activity using 'startActivityForResult' intents and
|
||||
* 'onActivityResult' intents.
|
||||
*/
|
||||
class FlutterBookActivity: FlutterActivity() {
|
||||
companion object {
|
||||
const val EXTRA_BOOK = "book"
|
||||
|
||||
/**
|
||||
* Static intent factory to start {@link FlutterBookActivity} with the singleton
|
||||
* {@link FlutterEngine} the application started.
|
||||
*
|
||||
* The activity launched from this intent shows the details of the {@link Api.Book}
|
||||
* supplied.
|
||||
*/
|
||||
fun withBook(context: Context, book: Api.Book): Intent {
|
||||
// In a more realistic app, there should be some dependency injection mechanism to
|
||||
// determine which engine to use.
|
||||
return CachedEngineBookIntentBuilder(ENGINE_ID)
|
||||
.build(context)
|
||||
.putExtra(
|
||||
// The Pigeon data class is useful not only between Kotlin/Java and Dart
|
||||
// but also within Kotlin/Java where activities must communicate with
|
||||
// each other via serializable data. The Pigeon data class is a
|
||||
// serializable class by definition.
|
||||
EXTRA_BOOK,
|
||||
// TODO(gaaclarke): the Pigeon generated data class should just implement
|
||||
// Serializable so we won't need 'toMap()' here
|
||||
// https://github.com/flutter/flutter/issues/58909
|
||||
book.toMap()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A static helper method to parse a result intent from this activity into a {@link Book}.
|
||||
*
|
||||
* @param resultIntent an {@link Intent} that must be the data intent returned by this
|
||||
* activity's {@code onActivityResult}.
|
||||
*/
|
||||
fun getBookFromResultIntent(resultIntent: Intent): Api.Book {
|
||||
return Api.Book.fromMap(resultIntent.getSerializableExtra(FlutterBookActivity.EXTRA_BOOK) as HashMap<*, *>);
|
||||
}
|
||||
}
|
||||
|
||||
// Intent builder class to build a FlutterBookActivity instance instead of the default FlutterActivity.
|
||||
class CachedEngineBookIntentBuilder(engineId: String): CachedEngineIntentBuilder(FlutterBookActivity::class.java, engineId) { }
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
// Called shortly after the activity is created, when the activity is bound to a
|
||||
// FlutterEngine responsible for rendering the Flutter activity's content.
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
|
||||
// The book to give to Flutter is passed in from the MainActivity via this activity's
|
||||
// source intent getter. The intent contains the book serialized as on extra.
|
||||
val bookToShow = Api.Book.fromMap(intent.getSerializableExtra(EXTRA_BOOK) as HashMap<*, *>)
|
||||
|
||||
// Register the HostBookApiHandler callback class to get results from Flutter.
|
||||
Api.HostBookApi.setup(flutterEngine.dartExecutor, HostBookApiHandler())
|
||||
|
||||
// Send in the book instance to Flutter.
|
||||
Api.FlutterBookApi(flutterEngine.dartExecutor).displayBookDetails(bookToShow) {
|
||||
// We don't care about the callback
|
||||
}
|
||||
}
|
||||
|
||||
// This {@link Api.HostBookApi} subclass will be called by Pigeon when the corresponding
|
||||
// APIs are invoked on the Dart side.
|
||||
inner class HostBookApiHandler: Api.HostBookApi {
|
||||
override fun cancel() {
|
||||
// Flutter called cancel. Finish the activity with a cancel result.
|
||||
setResult(Activity.RESULT_CANCELED)
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun finishEditingBook(book: Api.Book?) {
|
||||
if (book == null) {
|
||||
throw IllegalArgumentException("finishedEditingBook cannot be called with a null argument")
|
||||
}
|
||||
// Flutter returned an edited book instance. Return it to the MainActivity via the
|
||||
// standard Android Activity set result mechanism.
|
||||
setResult(Activity.RESULT_OK, Intent().putExtra(EXTRA_BOOK, book.toMap()))
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package dev.flutter.example.books
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonParser
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
import java.lang.RuntimeException
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
const val BOOKS_QUERY = "https://www.googleapis.com/books/v1/volumes?q=greenwood+tulsa&maxResults=15"
|
||||
}
|
||||
|
||||
private lateinit var books: MutableList<Api.Book>
|
||||
private lateinit var list: LinearLayout
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
list = findViewById<LinearLayout>(R.id.list)
|
||||
|
||||
// OkHttp is arbitrarily chosen here to represent an existing middleware constraint that's
|
||||
// already present in an existing application's infrastructure.
|
||||
val httpClient = OkHttpClient()
|
||||
val bookRequest = Request.Builder()
|
||||
// Retrieve data from Google Books API (arbitrarily chosen). This represents existing
|
||||
// data sources that an existing application is already interfacing with.
|
||||
.url(BOOKS_QUERY)
|
||||
.build()
|
||||
|
||||
httpClient.newCall(bookRequest).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
throw e
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
response.use {
|
||||
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||
books = parseGoogleBooksJsonToBooks(response.body!!.string())
|
||||
|
||||
val spinner = findViewById<ProgressBar>(R.id.spinner)
|
||||
|
||||
runOnUiThread {
|
||||
// Showed a spinner while network call is in progress. Remove it when
|
||||
// response is received.
|
||||
spinner.visibility = View.GONE
|
||||
populateBookCards()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Take a top level Google Books query's response JSON and create a list of Book Pigeon data
|
||||
// classes that will be used both as a model here on the Kotlin side and also in the IPCs
|
||||
// to Dart.
|
||||
private fun parseGoogleBooksJsonToBooks(jsonBody: String): MutableList<Api.Book> {
|
||||
// Here we're arbitrarily using GSON to represent another existing middleware constraint
|
||||
// that already exists in your existing application's infrastructure.
|
||||
val jsonBooks = JsonParser.parseString(jsonBody).asJsonObject.getAsJsonArray("items")
|
||||
val books = mutableListOf<Api.Book>()
|
||||
for (jsonBook in jsonBooks.map { it.asJsonObject }) {
|
||||
try {
|
||||
// Here we're using GSON to populate a Pigeon data class directly. The Pigeon data
|
||||
// class can be used not just as part of your IPC API's signature but also as a
|
||||
// normal data class in your existing application.
|
||||
//
|
||||
// We could either push the Pigeon data class usage higher into the existing GSON
|
||||
// "middleware" or lower, closer to the IPC.
|
||||
val book = Api.Book()
|
||||
val volumeInfoJson = jsonBook.getAsJsonObject("volumeInfo")
|
||||
book.title = volumeInfoJson.get("title").asString
|
||||
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.publishDate = volumeInfoJson.get("publishedDate").asString
|
||||
book.pageCount = volumeInfoJson.get("pageCount").asLong
|
||||
books.add(book)
|
||||
} catch (e: Exception) {
|
||||
println("Failed to parse book:")
|
||||
println(GsonBuilder().setPrettyPrinting().create().toJson(jsonBook))
|
||||
println("Parsing error:")
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
return books
|
||||
}
|
||||
|
||||
// Given a populated books list, create a Material Design card in a scroll view for each book.
|
||||
private fun populateBookCards() {
|
||||
for ((index, book) in books.withIndex()) {
|
||||
val card = layoutInflater.inflate(R.layout.book_card, null)
|
||||
updateCardWithBook(card, book)
|
||||
card.findViewById<MaterialButton>(R.id.edit).setOnClickListener {
|
||||
// When the edit button is clicked in a book's card, launch a Flutter activity
|
||||
// showing the details of the book.
|
||||
startActivityForResult(
|
||||
// We're using our own 'FlutterActivity' subclass which wraps Pigeon API usages
|
||||
// into an idiomatic Android activity interface with intent extras as input and
|
||||
// with activity 'setResult' as output.
|
||||
//
|
||||
// This lets activity-level feature developers abstract their Flutter usage
|
||||
// and present a standard Android API to their upstream application developers.
|
||||
//
|
||||
// No Flutter-specific concepts are leaked outside the Flutter activity itself
|
||||
// into the consuming class.
|
||||
FlutterBookActivity
|
||||
// Re-read from the 'books' list rather than just capturing the iterated
|
||||
// 'book' instance since we change it when Dart updates it in onActivityResult.
|
||||
.withBook(this, books[index]),
|
||||
// The index lets us know which book we're returning the result for when we
|
||||
// return from the Flutter activity.
|
||||
index)
|
||||
}
|
||||
list.addView(card)
|
||||
}
|
||||
}
|
||||
|
||||
// Given a Material Design card and a book, update the card content to reflect the book model.
|
||||
private fun updateCardWithBook(card: View, book: Api.Book) {
|
||||
card.findViewById<TextView>(R.id.title).text = book.title
|
||||
card.findViewById<TextView>(R.id.subtitle).text = book.subtitle
|
||||
card.findViewById<TextView>(R.id.author).text = resources.getString(R.string.author_prefix, book.author)
|
||||
}
|
||||
|
||||
// Callback when the Flutter activity started with 'startActivityForResult' above returns.
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
// The Flutter activity may cancel the edit. If so, don't update anything.
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (data == null) {
|
||||
throw RuntimeException("The FlutterBookActivity returning RESULT_OK should always have a return data intent")
|
||||
}
|
||||
|
||||
// If the book was edited in Flutter, the Flutter activity finishes and returns an
|
||||
// activity result in an intent (the 'data' argument). The intent has an extra which is
|
||||
// the edited book in serialized form.
|
||||
val returnedBook = FlutterBookActivity.getBookFromResultIntent(data)
|
||||
// Update our book model list.
|
||||
books[requestCode] = returnedBook
|
||||
|
||||
// Refresh the UI here on the Kotlin side.
|
||||
updateCardWithBook(list.getChildAt(requestCode), returnedBook)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
@@ -0,0 +1,170 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
</vector>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:id="@+id/appbar"
|
||||
app:liftOnScroll="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:id="@+id/nestedScrollView"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/list"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:context=".MainActivity" />
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/spinner"
|
||||
style="?android:attr/progressBarStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
app:layout_anchor="@+id/nestedScrollView"
|
||||
app:layout_anchorGravity="center" />
|
||||
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="21dp"
|
||||
android:layout_marginStart="21dp"
|
||||
android:layout_marginEnd="21dp"
|
||||
android:layout_marginBottom="2dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?attr/textAppearanceHeadline5"
|
||||
tools:text="Title" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/subtitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:textAppearance="?attr/textAppearanceSubtitle1"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
tools:text="subtitle" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/author"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:textAppearance="?attr/textAppearanceBody1"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
tools:text="Author" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:orientation="horizontal"
|
||||
android:layout_gravity="end">
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/edit"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/edit"
|
||||
style="?attr/borderlessButtonStyle"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 7.3 KiB |
|
After Width: | Height: | Size: 7.7 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 16 KiB |
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#6200EE</color>
|
||||
<color name="colorPrimaryDark">#3700B3</color>
|
||||
<color name="colorAccent">#03DAC5</color>
|
||||
</resources>
|
||||
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<string name="app_name">Books</string>
|
||||
<string name="edit">Edit</string>
|
||||
<string name="author_prefix">By: %1$s</string>
|
||||
</resources>
|
||||
10
add_to_app/android_books/app/src/main/res/values/styles.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<resources>
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||