-
Notifications
You must be signed in to change notification settings - Fork 64
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
base: main
Are you sure you want to change the base?
Conversation
@@ -15,6 +15,20 @@ class ScannerScreen extends StatefulWidget { | |||
|
|||
class _ScannerScreenState extends State<ScannerScreen> { | |||
final _torchIconState = ValueNotifier(false); | |||
var _canChangeCamera = false; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 typebool
. 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:
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
do { | ||
try setupCaptureDevice(arguments) | ||
} catch { | ||
throw error | ||
} | ||
} |
There was a problem hiding this comment.
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.
@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 |
Yeah, sorry for being to quiet. I am finalizing my master thesis, so after that I will be back working on this plugin. |
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. |
No worries, that has priority of course! |
Hi @jhoogstraat, I hope you're doing well. |
Hi, actually I am back right now. I am currently looking through all the comments and changes! |
Any update on this? |
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. |
This comment was marked as outdated.
This comment was marked as outdated.
…ntinuous-detection-mode Enable pause & resume on continuous scanning mode in iOS
@jhoogstraat any idea when we can get this merged or when v2 will be done? |
I think there is not much left for the release of v2. I'll do some tests and check with other issues/prs. |
Thanks, we are looking forward to that so we can update our package and release them to pub.dev as well |
@jhoogstraat any update on this? |
@jhoogstraat any more updates? 😁 |
Hey, I'll take at look at the code. |
We are using this in production for 1,5 years already |
Yes it's ready to merge and indeed, we've been using it in different projects that are in production |
@jhoogstraat any idea when this can be merged? |
Fixed #4