Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Commit for PlaformChannel Sample App #453

Merged
merged 13 commits into from
Jun 4, 2020
Merged

Initial Commit for PlaformChannel Sample App #453

merged 13 commits into from
Jun 4, 2020

Conversation

AyushBherwani1998
Copy link
Member

@AyushBherwani1998 AyushBherwani1998 commented Jun 1, 2020

Description

  • Adds a MethodChannel Demo with tests.

GIF

@RedBrogdon @domesticmouse I have few doubts regarding the current commit. I'll state them below

  • Currently, for testing the functionality of the counter, I need to write an integration test. Should I write an integration test or just unit test which Mocks the increment and decrement methods using setMockMethodCallHandler? I can also use the provider package since my logic is already separated out and in the test, I can create a Mock.

  • Currently, I have separated the logic for the MethodChannel and method calls in the class named Counter. Is that how we are looking to implement the demos? If yes, then is there any project structure I should follow?

Copy link
Contributor

@RedBrogdon RedBrogdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AyushBherwani1998, this is a great start. I've added a lot of comments (and, I should warn you, there's probably a few more to come 😄 ), but that's mostly just because this is the first PR and an especially big one. Once you get going on this app, we'll know each other better and the process will speed up.

Let me know what you think and when you're ready for another look!

@@ -0,0 +1,3 @@
# Platform Channel Samples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs to get bigger and more detailed. Take a look at the README in jsonexample for a model:

https://github.com/flutter/samples/blob/master/jsonexample/README.md

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to add a detailed README after the project. As of now, I have improved it taking the base from other samples in the repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new stuff looks great. The only additional suggestion I have is to add a line just before "Goals" that says "This sample is currently being built. Not all platforms and functionality are in place."

- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_new
- unnecessary_statements
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Throw a newline on this.

@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.platform_channels">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally use "dev.flutter" or "dev.flutter.example" as the org. You should be able to run:

flutter create --org="dev.flutter" .

in the project directory to change it.

}

// List of all the demos with their titles and routes.
final demoList = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we'd make a simple data class for this rather than using maps, so you'd have List<DemoData> rather than List<Map<String, String>>.

appBar: AppBar(
title: Text('Platform Channel Sample'),
),
body: SingleChildScrollView(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using a ListView here? While it's unlikely to matter in this case, ListView is significantly more performant than a scrolling Column.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider using a ListView, but I think we won't have many DemoTile's on the screen. I'm using SingleChildScrollView just to be on safer side in case if user is testing in small device.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, and we decided to go with a ListView.

@override
void initState() {
super.initState();
count = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also just initialize count to 0 right in its declaration and get ride of this override.

title: const Text('MethodChannel Demo'),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.start,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start is the default, so you can actually leave this out.

appBar: AppBar(
title: const Text('MethodChannel Demo'),
),
floatingActionButton: Row(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that if one is going to have two FABs, they should be a in a Column. At least that's the example they show on the Material site:

https://material.io/components/buttons-floating-action-button

platform_channels/lib/src/method_channel_demo.dart Outdated Show resolved Hide resolved
platform_channels/lib/src/method_channel_demo.dart Outdated Show resolved Hide resolved
@AyushBherwani1998
Copy link
Member Author

@AyushBherwani1998, this is a great start. I've added a lot of comments (and, I should warn you, there's probably a few more to come 😄 ), but that's mostly just because this is the first PR and an especially big one. Once you get going on this app, we'll know each other better and the process will speed up.

Let me know what you think and when you're ready for another look!

I was expecting comments for the feedback and reviews to help me better understand how the samples are maintained 😄 . I'll push the changes soon.

Copy link
Contributor

@RedBrogdon RedBrogdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really close!

@@ -0,0 +1,3 @@
# Platform Channel Samples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new stuff looks great. The only additional suggestion I have is to add a line just before "Goals" that says "This sample is currently being built. Not all platforms and functionality are in place."

## Goals

* Demonstrate how to use `MethodChannel` to invoke platform methods.
* Demonstrate how to use `EventChannel` to listen continous value changes across the platforms.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "to listen to continuous value changes from the platform."


if (call.argument<Int>("count") != null) {
count = call.argument<Int>("count")!!
val count: Int? = call.argument<Int>("count")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Much tighter code.

appBar: AppBar(
title: Text('Platform Channel Sample'),
),
body: SingleChildScrollView(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, and we decided to go with a ListView.

/// It has two [FloatingActionButton]s and a [Text] widget to display the value of
/// [count].
/// It has two [FloatingActionButton]s to increment and decrement the value of
/// [count], and a [Text] widget to display the value it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "...to display its value."

FloatingActionButton(
heroTag: 'addition',
child: Icon(Icons.add),
floatingActionButton: Builder(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor. Builder has saved me a lot of keys. :)

FloatingActionButton(
heroTag: 'subtract',
child: Icon(Icons.remove),
// Whenever users presses the FloatingActionButton, it invokes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "...users press the..."

import 'package:flutter_test/flutter_test.dart';
import 'package:platform_channels/src/method_channel_demo.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary. Try taking it out and see if everything still works. :)

Comment on lines 36 to 45
onPressed: () {
Counter.increment(counterValue: count).then((value) {
setState(() => count = value);
}).catchError((dynamic error) {
showErrorMessage(
context,
error.message as String,
);
});
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I had a comment in this section earlier, but just in case: typically I would expect to see an async method here. Generally we recommend using async/await over the Futures API whenever possible.

The logic can all stay the same; it's just a syntactical change:

onPressed: () async {
  try {
    final value = await Counter.increment(counterValue: count);
    setState(() => count = value);
  } catch (error) {
    showErrorMessage(
      context,
      error.message as String,
    );
  }
},

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed it in the previous commit after doing the changes.

@AyushBherwani1998
Copy link
Member Author

The recent commit updates the UI of MethodChannel Demo since having two FloatingActionButtons is not always encouraged.

New UI

Copy link
Contributor

@RedBrogdon RedBrogdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the one suggestion about shuffling some test code around. Let me know what you want to do there, and then we can merge this.

@RedBrogdon RedBrogdon merged commit acf700c into flutter:master Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants