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

Implement camera switching #51

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

ikbendewilliam
Copy link

Fixed #4

@ikbendewilliam ikbendewilliam changed the title Implement camera switching for Android Implement camera switching Dec 9, 2021
@@ -15,6 +15,20 @@ class ScannerScreen extends StatefulWidget {

class _ScannerScreenState extends State<ScannerScreen> {
final _torchIconState = ValueNotifier(false);
var _canChangeCamera = false;
Copy link

Choose a reason for hiding this comment

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

Good practice sake should declare type bool

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Xazin , actually the Dart style guide encourages the use of var/final for local variables instead of declaring types::
https://dart.dev/guides/language/effective-dart/design#dont-redundantly-type-annotate-initialized-local-variables

Copy link

@Xazin Xazin Jan 8, 2022

Choose a reason for hiding this comment

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

This cannot be final, and I cannot find the place in the effective dart guidelines where it says to prefer var over declaring it the known type bool. And it depends highly on how narrow the scope the variable is used in is. In my opinion this is not a narrow scope.

Copy link
Author

Choose a reason for hiding this comment

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

Internally we always use var, but as this is not our package, and I can't find any best practise references I've changed it to bool

Copy link
Collaborator

Choose a reason for hiding this comment

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

This cannot be final, and I cannot find the place in the effective dart guidelines where it says to prefer var over declaring it the known type bool. And it depends highly on how narrow the scope the variable is used in is. In my opinion this is not a narrow scope.

@Xazin sorry that my comment wasn't clear. I mentioned both var and final. Yes, this can't be final since you need to mutate the value, but it should be var. The part of the dart style guide that mentions this best practice is here:

https://dart.dev/guides/language/effective-dart/design#do-type-annotate-fields-and-top-level-variables-if-the-type-isnt-obvious

In this case the initializer makes the type obvious; it's initialized to false so it's obviously a bool and typing it out doesn't add any clarity.

So I believe the dart guide here recommends that this be var rather than bool.

Copy link

Choose a reason for hiding this comment

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

I agree, but they also recommend doing this mostly in smaller functions. When in doubt always declare type, they even mention this themselves.

Although the subject is a bit contradictory even from their own documentation, I do agree if we're talking about functions of 10 lines or less, it makes no sense to declare type, but this is a local variable with a scope of 50+ lines.

Whether it adds clarity to type the variable, I can also agree with you that it's not because it has super much value, because the naming of the variable now correctly reflects what type it is as well. But with a larger scope and 1 character difference, I don't see a good enough reason to omit the type in this scenario.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Xazin I hope you don't mind a continued discussion on this. It seems like such a small thing; but since we're talking about enforcing a convention I'm more interested in making sure we get the convention right than about this particular line of code. As as a contributor to this package I want to make sure my own contributions meet the package's standards.

Ultimately, it's neither right nor wrong to add the type annotation in this location. I think it should be fine if we do either; though I personally I take a minimalistic approach and prefer to leave them off when the annotation doesn't add value.

From the style guide section I shared it specifically says this:

In some cases, though, the type is so obvious that writing it is pointless:

And then as you pointed out it also says it's ok to add them even if it's obvious:

When in doubt, add a type annotation.

And so the use of the annotations requires some judgement. In addition to considering the size of the function I think it is also useful to consider the complexity of the expression the produces the type. Complex expressions can make the type more difficult for the reader to infer. consider these examples:

complex expression

final firstList = <int>[1, 2, 3, 4];
final secondList = firstList
    .map((e) => e.toString())
    .map((e) => e.length)
    .map((e) => e % 2 == 0 ? "even" : "odd")
    .toList();

In this first example you would have to mentally evaluate the expression before you know what type secondList is. This would probably be fine in a small function where the logical flow of the program is easy to evaluate and follow. But in a bigger scope a type annotation would be useful in this spot to save the mental energy. Furthermore, a type annotation is helpful to avoid an accidental List<dynamic> inference which is easy to do when mutating lists and the tools wouldn't necessarily help you see the issue.

This would be much better with generic types and type annotations:

final firstList = <int>[1, 2, 3, 4];
final List<String> secondList = firstList
    .map<String>((int e) => e.toString())
    .map<int>((String e) => e.length)
    .map<String>((int e) => e % 2 == 0 ? "even" : "odd")
    .toList();

simple expression

var _canChangeCamera = false;

In this second example, taken from the code in question, false is a reserved primitive type that is completely obvious. Adding a type annotation to a simple and unambiguous expressions like this can be avoided.

In the end I think our opinion here is actually very close. You don't find a good enough reason to omit the type and I don't find a good enough reason to add the type. And so ultimately I think we should support either style; and in this case, leave this line how it is. A rule to always add or always omit isn't workable.

_checkCamera();
}

Future<void> _checkCamera() async {
Copy link

Choose a reason for hiding this comment

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

Naming sense here could be better, you're not checking the camera, you're checking if it's possible to change camera.

Copy link
Collaborator

Choose a reason for hiding this comment

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

suggest: _validateCameraSwitching

Future<void> _checkCamera() async {
final canChangeCamera = await CameraController.instance.canChangeCamera();
setState(() {
_canChangeCamera = canChangeCamera;
Copy link

@Xazin Xazin Dec 23, 2021

Choose a reason for hiding this comment

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

You can change this to

setState(() async {
  _canChangeCamera = await CameraController.instance.canChangeCamera();
});

No need for variable declaration.

Comment on lines 122 to 127
do {
try setupCaptureDevice(arguments)
} catch {
throw error
}
}
Copy link

Choose a reason for hiding this comment

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

Formatting in this file seems very off.

@ikbendewilliam
Copy link
Author

@Xazin Can you review again? The comments have been fixed

@Xazin
Copy link

Xazin commented Dec 29, 2021

@Xazin Can you review again? The comments have been fixed

@jhoogstraat Is the only one who can merge it, and he also needs to review it.

Also I think you should change the branch you're merging into to develop !

@jhoogstraat
Copy link
Owner

Yeah, sorry for being to quiet. I am finalizing my master thesis, so after that I will be back working on this plugin.
Does this fix issues which are present in the develop branch?

@Xazin
Copy link

Xazin commented Dec 29, 2021

This is the missing functionality to switch between front and back camera, I am not sure how the develop branch looks, but this indeed does fix this part of the issue on main.

@ikbendewilliam
Copy link
Author

No worries, that has priority of course!
This adds functionality in the current release v1.1.4 (main branch). I haven't tested it on the v2 (develop branch)

@ikbendewilliam
Copy link
Author

Hi @jhoogstraat, I hope you're doing well.
In the near future we'd like to use this implementation in a new package, to publish that package this first needs to be merged. For now we use git dependencies 🙂.
This isn't priority as you're working on your master thesis, but can you give us an indication of when you would be able to check this? Or, if you prefer, you could give @Xazin merge rights (or someone else ofc). But that's entirely up to you.

@jhoogstraat
Copy link
Owner

Hi, actually I am back right now. I am currently looking through all the comments and changes!

@ikbendewilliam
Copy link
Author

Any update on this?

@jhoogstraat
Copy link
Owner

I would very much like to merge your pr, but I am still hestitent as I am unsure how to incorporate these changes into v2 (develop branch).

Maybe with a bit of refactoring this approach can be used.

@ikbendewilliam

This comment was marked as outdated.

rwillemsandroid and others added 2 commits April 21, 2022 14:11
@vanlooverenkoen
Copy link

@jhoogstraat any idea when we can get this merged or when v2 will be done?

@jhoogstraat
Copy link
Owner

I think there is not much left for the release of v2. I'll do some tests and check with other issues/prs.

@vanlooverenkoen
Copy link

Thanks, we are looking forward to that so we can update our package and release them to pub.dev as well

@vanlooverenkoen
Copy link

@jhoogstraat any update on this?

@ikbendewilliam
Copy link
Author

@jhoogstraat any more updates? 😁

@jhoogstraat
Copy link
Owner

Hey, I'll take at look at the code.
Is this ready to merge?

@vanlooverenkoen
Copy link

We are using this in production for 1,5 years already

@ikbendewilliam
Copy link
Author

Yes it's ready to merge and indeed, we've been using it in different projects that are in production

@vanlooverenkoen
Copy link

@jhoogstraat any idea when this can be merged?

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.

Unable to use front camera
7 participants