Implementing iOS Barometer Plugin - The Boring Flutter Development Show, Ep. 7.2

Implementing iOS Barometer Plugin - The Boring Flutter Development Show, Ep. 7.2

Show Video

Hey. Everybody welcome back to the boring show I'm Andrew Brogdon from the flutter team and with me is and Willie Fortuna also, with the flutter team yes it's the all redhead dream team that you've been waiting for more, ginger than grandma's chana masala. Here. We go all. Right so. Today we're going to continue our barometer plug-in. Development. So last time where, did we wrap it up we had a dart. Interface right. You. Could get one reading, or. A single reading when you press the button for. To. Get your pressure, okay, and we had it implemented, on Java but not on iOS yes okay. Well. Cool so, next. Step let's get it implemented for iOS and show. How that works. Yes. I think we'll, get an implement iOS then we can publish. That just so that, you all can see the process for, through-line. Now we recognize, that the current API is not ideal, and, probably don't just want to get one pressure reading and then be like I'm good my F my, yeah. So. Then. We'd, like to do, update, our API to have streams, on. The Android iOS what, is all about the reactive programming and, streams very, native to flutter okay and, it, will throw in some questions. Or, answers to questions maybe. Some questions maybe some questions and not answer them all. Right so that's. A good review let's let's review the code as well so we had an app here that was using, let's, start with the plugin so we had our barometer plugin which is, make. This a little bit bigger Oh too big so. It's very simple it. Was just pulling a still, had that leftover platform, version yes I get her in there which we should probably take it that'll be her API cleanup yeah we'll take a but. It had get barometer and initialized barometer, and so. It was initializing, the barometer and then in Java land. It. Was just kind of, kicking. Off when. You initialized, it would go get the sensor register. A listener that would update a little a little variable and then any time you called in to get the value would just be like oh here's the way. I have. One right here and. All this is tied together with your, the. Methods. Method. Channels yes so. Message. Passing mm, so we have on method call right here what. We're checking the method the dart is trying to call and then, returning a result based on which method it is and calling the right Java function in response so I basically need to go implement this, in iOS. Land. If, the cake yeah no problem. I have, literally. Days of iOS experience this would be this would be fun so. Much, like the plugin has an android folder it also has an iOS folder and. Where, the nice things you can do is right-click on that or. Double-click. And. Open in. Xcode and, before, you dive into this too much I just want to reiterate that. Plug-in.

Development, Is a probably. More advanced topic then you would need for your average flutter. App, in. Ideal world and probably. In most times in practice you're just going to be able to write, your app and then say oh I, need to plug in to do X and then you search and someone, will have already implement it for you this isn't the rare off case that you need, to. Emergency, measure, air, pressure. No. One's done that yet you're in a hot air balloon and you need to calculate how high you are something, like shooting off a rocket ship, yes. I. Mean this is we're interacting with a platform, API and, for, platform api's you kind of have to you have to talk to the platform for that there's no getting around it but I imagine, as flutter matures you'll start seeing more and more dedicated, written, in dart, SDKs. For things, like network services, and things like that so, that's, something I look forward to seeing in the future but this isn't that rare case that yeah you're taking a trip to space you need your air pressure we, can help you see, how to do a plugin for that all, right let me see about. Setting. Up the font, size for this there, we go. Hello. Code okay. So this is the iOS. Version of the plug-in that we were just looking at in, Android, land in, Objective C we should mention you, can program your plugins, in. Swift and cotton, we're doing Objective, C and Java for this particular case mm-hmm all right, so, what. Are we doing here we've got the barometer plug-in class and, that's. Being we, have a register, with register our class method that's getting called and, that's, making a method Channel so, here's that barometer, name if you remember back in our dart code we. Had. I'm. At the channel we called barometer, so, here we're spinning up the sort of other end of that channel to listen for messages and. We make an instance of the barometer plug-in class to handle, the actual work and add. A method called delegate so. We got this that kind of sets things up and then here's an instance method that's. Handling the method calls, very. Similar to what we saw in Java, looks. Like so. Let's. Get. Right into it I gotta add some stuff to this right so I need to add just. Like before I, need, to add some, things so yeah. What. Are our methods called that would be a good thing for me if you remember I think initialize, and. Then, think. It's just, reading. Oh. Got. An extra bracket in there. See. Called a strain called up. And so, that's going to do some stuff and then I need another one of these. Constructions. For. The. Next one yeah. Yeah. It's just Oh we were calling it get barometer, okay, so, I've initialized barometer, and. Get barometer. And. So these are checking the name of the method that art is attempting to call and then we need to do stuff. Stuff. So. Yeah. Piece of cake. So. Let's see we're gonna need how.

Does. The. Barometer, work this is our old code which I was checking out, its. Cm altimeter. I. Believe. From, core motion is what we're looking for and. So. An object that initiates a delivery of altitude related changes. That. Sounds fancy and so I think this is the equivalent of the sensor and that would be accessing, on. So. We have a class we'll need to instantiate one, of these and we'll, need to. Start. Relative, altitude. Updates. Seems. Like, what we're interested in hmm. Okay, so. Let's just okay. Let's declare some, you. Put instance variables in right live ours. Yeah. So. We'll want and. What. Does this. Return. In. So. If you're a call from last time there's this short. For. Flutter that tells you what the types are. For. Yeah. Tell. Me let, me see so, I want a double on the dart side so what do I need to give it on, the iOS side. And. Everything because. I was showing this last time so. If I want a double, I need, an NS number number. With double okay so. Let's make an in its number. Pointer. To and. Call it pressure. So. That can be the, thing that we just keep updating, and then I need. An altimeter. I'm. Scared, because it's not recognizing. That we. Import, something that, we do. I'm. Guessing. Core motion oh this, is parkour motion okay so I can come up here and it's, a framework. For. Motion. Like. That we. Good we're, good, okay so I'm important. To class. Okay, so that's good uh Nuvi questions so someone - that's not really programmed much. Objective-c. Is, this is this like a class is, this. I guess. My, my like oh no global variables, is, traveling but I don't maybe. Then. It's. Probably tingling for a reason I believe. That. These are variables, that will be one, per class I don't. Know that for a fact okay, well we, can let's. We'll do it we'll start this way we will get the code out and then we can I, don't, think it's possible for them to spin up two instances, of the. Plug-in which, means even if we haven't properly done. This and we're polluting the global namespace on the iOS side there's no chance that we can pull it it twice. You know what I bet we have commenters, it, will tell us exactly what's, going on, just. Like we had someone that. Explained. What I had not seen on the pressure. Sensor, do. We have is. That on our list of. So. For. Wad cond there. We go very. Well put a graphic so. Pointed, out that, Andrew. Was missing do the additional, sub shoes and we, will when we're doing Android we can simulate, the different pressures yeah I missed, this conveniently. Named additional sensors tab right, here that was doing the panel so. We learned from you as, well. Okay, so. Someone. Can tell us if instance, variables are not a good idea there in. The meantime I'm gonna use one. You're gonna press on because we, progress. Yeah, it's, very, you, knows we're coding without a net not a lot of preparation, here. Operating. Yeah okay, so we need to make an ultimate err stay. On stay, on task yes, all of my teachers used to tell me. Told. Me to. Allocates. And. Initialize. That. And, then we'll need to subscribe. To, updates right, yes, and that function, was. Cold. Start. Relative, altitude updates, to, and it takes a cue and a handler, which. Are words I've heard before. Operation. Cute mm-hmm, okay so it's gonna take an operation cue I'm. Okay. So. Start. Okay. So. I'm guessing, I can do. And. There's a main there's always a main there's. Always a main everything mm-hmm so got that. You. Make that block. There, we go okay, let. Me clean that up, and. So that, made a block for me which is very helpful yes. This is our sort of callback, handler right so giving it a block. And. That's gonna get an altitude data as well as an error. We'll. Just assume everything's, fine and just. Use the altitude data so that's coding practice yes no, yeah. It's piece of cake um so, altitude, huh. Whoa. Drag. Source descriptor is probably not what we're looking for. Let's. Do okay, can, I hurt you, and what. Are you. Okay. Can. I jump the definition or something yeah.

So. That's an NS number. Pressure. And kiloPascals, and. What. Was the. Measurement. For the pressure in Android, one do, you remember, I know. It was a thousand. Okay. Yeah, we'll just pretend like. It, oh look at it that's that's no it's that's an important thing to check so we could. Occasionally. When you're doing platform stuff you're gonna have platforms it behaves slightly differently, so in, this, case if Android, uses one unit and iOS uses another and there you. Know there's a constant, that you have to multiply by. A so you, know centigrade, to Fahrenheit or, something, like that those, sorts of conversions, you definitely want to be aware of that. For. Right now I'll just press on this I mean this is yeah. So. That's gonna initialize the barometer, and then. Here, we would just oh wait we need to return something I remember that this richer this expects, a, boolean. So. Let's do result. What. Is a boolean in. This that's a. Boolean. Is an NS number with bull no, oh it's just NS number that's the that's, a constructor, Cameron no the initializer. Okay, I, bet this, is why I'm not a. This right now. Okay, this number which. Is a, term that I've heard. Some. Number, with. And. Then. Yes. That, looks. Like. Reasonable code I think. Okay. And, then, we still need to fill in this one so initialized. Barometer, is going to make the altimeter. Subscribe. To it for updates and, update. The pressure anytime it gets an update and then. Kept, rahmanir needs to return the, number that it's updating. And. I just do. No. I cannot. Hmm. So. Do we need to. I'm. Not returning something I'm passing. This resolving. Okay. Cuz. Not all six-letter, words that begin with are are, the same as, far as xcode is concerned so. I can save this now I could actually. There. Is a module. Project you can actually run from, within Xcode but I'm gonna go back to our our flutter, code here and run from here so let's, get. Got an iPhone X. Let's. See what I got. Now. Before. It. Would have, crashed. Because. It would have the, first thing happens is initialized barometer, gets called no yeah, me and the iOS code would have you know before we added this bit it would've been like I don't know what the heck you're talking about and, it would have immediately crashed, it. Might still crash we'll see. Alright. That's, cool Oh. Hooray. We're. In a vacuum I'm, surprised that didn't crash kudos, to whoever saved. Me and put. A two string method on null thank you for that. So. Okay here's the question though and I. Think, you were saying that there's no. You. Can't there's, no like simulator, for pressure with. The I oh, that's. Right there might not everyone is, it where, we getting no because, yeah. Or, is it because it's our fault that's the question okay so I did I was worried about that I know on on Android, because. Someone told me exactly where to go to mess, with the. Pressure. Stuff and, I. Did bring I actually have a hardware device this time just in case, so let's. And. We can know whether it's us or yeah. Okay. So let's let's, go back to Android. Studio ironically. Enough let's. Stop and, let's. Try, my. Hardware device. And. See what we get there. And. It's getting updates from new, sites that's nice do you have a. Mirroring. Thing, I do, not okay, I can. Take a picture of this with my pixel so. We could do that like, this is why everything, she's. Right there it's, the real device, okay. So, that's that's. 101. That's. Interesting and we had a thousand. It. Was a thousand, and thirteen, so, what are the units did you actually get a chance to look this up I, got. Distracted. Probably. Helping me uh let's, see her. So. We. Have this. We. Got the recording, going over here let me see what I could find - so, a sensor. It's. Just a pressure, Android's. Sensor. And. See what comes up, environment. Sensors, Oh. Millibar. Hectopascal. Sounds. Like a stock ticker symbol, okay. So. So. HPA is, a, hundred, Pascal's. Hectopascal. I think, I. Killed. One of those playing Dungeons & Dragons once. Hectopascal. Okay so that's 100 pascals, do. We know, back. Here. Okay. Got to be on your summer.

Core. Motion sounds like an exercise routine well. There's a CM altitude data. KiloPascals. There we go, okay. So, kiloPascals. Versus checked it myself so we're off by an order of magnitude okay, so. That. Would be why we've got 100 versus, a thousand, okay so, the. Question is what should a wheel yeah which yeah so we're in the iOS code right now so it would be easy to change that yeah and I also feel. Like, from. My. Because. I'm such a air, pressure expert. I. Feel, like a, thousand. Thirteen is that's like sea level that's like the more standard okay where did I normally, see it reported I will take your nautical knowledge. This. Will also be fun because people get to watch me try new math with an NS number. So. Hope first we want to get the double value of, it and. Then. Multiply that but. Then we have to make it another, NS number out of it I. Think. So, I know it's number. Number. With. With. Double. Does. That work. Let's, save. And. Go back to Android studio and, stop. And. Start. Real-time. Recording, happening. Now. I. Borrow. This device from the test lab it's, all just, flutter icons, from various sample absent tests it's. Like we've been working or nothing so, there we go. This. Is I'm sure the video production people in the room are very happy to see me doing this but. Our. Latest reading okay. So nothing. And. We're, around sea level and that kind of makes sense. All. Right. Yeah. Piece. Of cake end of episode now. All. Right so that's task one down. For today so we just finished up. Adding. Just the iOS code. Underneath, the, dart interface. That we'd written so it was just opening up some files. In Xcode adding a little bit of code here to handle those methods interact with the platform which is the purpose of the plug-in and. Then shipping. Those numbers back to. Marland. So. I think we are ready to share this with the world all, right so you wanna you wanna get started publishing it all. Right.

2018-09-06 12:39

Show Video

Comments:

second comment yay, please how do we do we save auth key on flutter.

Hey, skipmonday! auth key for what do you mean?

Do you have any code on github yet for this project?

Yes! it's here: https://github.com/RedBrogdon/barometer

Proud of you Fawad :)

Awesome content as always! I don't remember if this was commented in the previous episode, but why did you go with Java/Obj-C instead of Kotlin/Swift for this plugin? And also, Andrew, what's the editor theme for your Android Studio?

I think the short version on the Obj-C vs Swift is of the people who were available to record this video -- no one was comfortable enough to live code in Swift on camera yet! I have some studying to do. :-) I don't know why Kotlin vs Java... and Andrew can tell you about his editor settings...

i got noticed :D

First Comment yay!!!!!!!!!!!!

Oh, and the color scheme I use is called Obsidian. There are a zillion versions of it for various IDEs out there. :)

Like Emily said, it's mainly an issue of our personal experience. I have some Kotlin and Swift experience, but to be able to sit down and code something up while on camera, it helps to be working with something you're more familiar with. No reason not to use Kotlin and/or Swift in your own plugins, though.

in what episodes do you guy create the plugin from scratch?

16:25 No pressure guys! Take your time.

Other news