The mobile stack for Angular || Angular Virtual Conference 2021
7K views
Nov 9, 2023
Conference Website: https://globaltechconferences.com/event/angular-virtual-conference-2021/ C# Corner - Community of Software and Data Developers: https://www.c-sharpcorner.com C# Live - Dev Streaming Destination: https://csharp.live #angular #webdevelopment #Ionic #mobilestack
View Video Transcript
0:00
Well, hello, everyone. Thank you for being here. This is going to be a lot of fun. I hope that you leave here learning some cool new tools
0:12
So before we dive into this, I want to just share something that I have basically with me at all times. And that's this device. This little phone over here is basically with me
0:27
24 7 365 increasingly it's like the core thing for you know getting stuff to my house getting stuff
0:39
to other people get ordering food it is like a key part of my of my day-to-day and you know
0:49
this powerful device and powerful all these things that are that you're capable that it's capable of
0:55
doing came from humble beginnings. And that's where we got this, you know, lovely little device
1:00
called the original iPhone. Now, the original iPhone was a groundbreaking piece of technology
1:07
that promised to bring, you know, the power of the web to the masses. If you don't know the story
1:18
originally, when the iPhone was announced, they were saying, you're going to create apps for this
1:24
using the web, which was great. You'd be able to take all the amazing features
1:30
and now let's state this a little bit of HTML5 and build out complex apps
1:39
complex apps that, you know, take advantage of all the latest features
1:43
These are things like Ajax, HTML forms and client-side form validation. You know, it is a little bit of a
1:53
a little bit of a dated reference, but it truly was going to be a groundbreaking time
2:00
We were going to have all the power of the web on our mobile devices
2:05
But, you know, as time grew on, went on, you know, I think we all know where this ended up
2:11
We did not get HTML as the main development language for iPhone
2:18
We ended up getting things like Swift or Objective-C and Swift nowadays
2:23
We get these complex native tool chains so that way developers would have to learn a whole new language, a whole new stack if they want to write for this one device
2:34
Granted, now we have iPhone, iPad, Mac that we can all develop using these languages, but we're still in these one ecosystem versus being able to develop for everywhere
2:45
Android is in a similar situation where they have all of their tools and technologies available through Java traditionally and now Kotlin
2:56
So these kind of ecosystems kind of, you know, got rid of the idea of let's just have this one language or this one approach to developing for all these platforms
3:08
Not necessarily something I'm happy about, but it is what it is
3:13
So how can we, you know, change this and do something better
3:18
This is where the idea of cross-platform development comes from. So like Simon said, my name is Mike Hardington
3:27
I'm going to be talking about Mobile Stack, the Mobile Stack for Angular
3:31
If you want to find me on Twitter, handles mhardington, basically everywhere
3:38
Feel free to reach out if you want to ask any questions after the presentation
3:46
So cross-platform in this mobile stack for Angular kind of solidifies around these few ideas
3:54
Write once, run anywhere. I should be able to take one code base and deploy to all these different platforms
4:01
and still have access to native device features and the same HTML, CSS, JavaScript development, and debugging experience that I'm used to
4:13
If I want to access native features or go down to, say, the low-level APIs like Bluetooth, connections, file system access
4:25
I should have abstractions around that so I'm not dealing with it directly, but I can still make sure that wherever I am deployed to, I can either call the right API or fall back to something else
4:41
And again, one code base is really the goal, but I should be able to have these multiple targets
4:46
So all the platforms should exist at their own, you know, they should be all the platforms should be able to exist on their own merit
4:57
But I should be able to take this core code base that I have and drop it into all these platforms and have access to the same code bases
5:06
So with that in mind, the technology that we're going to be looking at today are, you know, one, Angular is at the center of all this
5:14
So I don't feel like I need to teach everyone Angular. I don't know if I can, you know, teach everyone Angular because, you know, we're here at an Angular event
5:27
So let's try to teach some of the other stuff. But we're going to look at first, Zionic
5:33
So for those who do not know, what is Ionic? Well, Ionic is a UI library for native and web, meaning that I have a collection of components that work really well for not only my web apps, but also deploying to native devices
5:52
And I feel right at home on those platforms. I can deploy the same components to both desktop, native, web, and be able to have the theming and styles for these components adapt the platform so they feel right at home, not only with the CSS that's being used, but the animations and transitions and all the little nuances in each of these platforms
6:21
It should feel right at home to those platforms. For Angular developers, Ionic integrates with the Angular CLI and integrates with the Angular router
6:32
So you're not having to learn a flavor or a slight deviation from Angular
6:41
You are just using your Angular knowledge. Everything that we've learned here today can still apply to your Ionic apps
6:48
It's less of Ionic and more of Angular with Ionic as its dependencies
6:57
Same way that you can look at it as Angular with Angular Material or Angular with whatever the UI system you're using
7:03
And for the people who have access to a designer or have some existing brand designs, you can
7:12
customize the entire look and feel of your app to feel very close to your own branding
7:19
by using CSS variables which can give you runtime computation the flexibility to change themes based on your operating system environment
7:32
all of these great things without ever having to deal with SAS
7:38
which I know that seems like it's a, you know, like, oh, that doesn't seem that great of a feature
7:44
But trust me, not having to deal with SAS and Node SAS and complex build tools is increasingly becoming a good thing
7:55
So for Ionic itself, you know, we can boil it down to some of the UI pieces that make up your app
8:04
So here on one end, we'll see. Let's go. Yeah, we'll do this
8:10
Here on one end for all the way towards this side, we have a collection of badges, list items, a tab bar component
8:21
Next from the left, we have a collection of buttons from different colors, different styles
8:28
to whether or not they should be an inset block button that has a line break or a full width button that goes all the way to the edge
8:37
collection of cards and then also list items and cards in particular are one of my favorite
8:44
components uh which seems like you know that's a pretty simple component mike why would you think
8:49
cards are that great uh because cards represent the building blocks that uh every app can use
8:57
cards themselves are just generally a a give with a drop shadow but in here our cards can be
9:06
composed by all these subcomponents. So that way we can have a card header, a card content
9:14
an image, a card subtitle. So we can start to build up all of these components together
9:21
and create what would have been a pretty boring, simple text into this massively complex card with
9:30
so much information, so much visual detail by just putting components in it and having it
9:36
automatically update to adapt to that. So something I think is really cool
9:44
So let's kind of look at some HTML. We're going to look at like a little walkthrough sample at
9:50
the moment, and then we can go to my editor, but let's kind of describe how some of these
9:56
components can be brought together. So I have a demo app. But before we go to the demo app
10:01
we have nothing, right? We have this blank canvas, this empty component or this empty text file
10:09
Let's add something in here to build out a page or a navigate or a route that you can navigate to
10:18
Well, start off with this, we're probably going to want to have like a header component and maybe
10:23
some content area. So let's add this ion header and ion content component. These are two core
10:32
building blocks that are based on Flexbox that allow you to have all these, all this dynamic
10:39
layout. So header is we can assume it's going to be at the top content is going to I'm assuming
10:45
be below the content of the header. Okay, pretty self-explanatory. Well, inside of header
10:52
we could also have a toolbar and a title. So all of these are prefixed with ions
10:58
that way they are in the same namespace, but toolbar, I'm going to assume
11:03
bar is in the name, I'm going to assume it's like an action bar or some kind of full width element with a title
11:09
I'm assuming it's just going to show the text, but style it a very different way. So that's pretty nice
11:15
Ion header, ion toolbar, makes perfect sense. Up next from toolbar and title
11:22
why don't we add some buttons into this, into this header or to this toolbar
11:29
So if we have one button and then we have an ion buttons around that
11:36
we can assume that ion buttons is just going to be a way for us to have
11:39
multiple buttons, but group them in a way that keeps them all right next to each other
11:45
We can then use things like component slots to say, all right
11:50
Ion buttons, I want you to go to the end slot of Ion toolbar and Ion icon. I want you to be in the
11:59
icon only slot for Ion buttons. That way you take up the full space of this button
12:06
Pretty nice, pretty easy to, or pretty simple and straightforward to see what each of these
12:12
components and what all of these attributes are doing. Further down from the content
12:18
And we can assume that we're going to have a list of something
12:24
And we think to vanilla HTML, we typically have something like an unordered list
12:29
or an ordered list. And inside of those, well, we have list items
12:35
In this case, we're calling them ion items. So they're slightly closer to
12:39
they're not exactly what you would expect from a UL and an LI, but it's close enough
12:44
We can get the same kind of metaphor in here. So we have ion list
12:50
Inside of that, we have an ion item. Let's throw some Angular in there and G4 and just iterate over some notes
12:57
And then we're going to render out each note inside of the item. Fairly straightforward
13:02
Looking at this all together, we have basically built out an entire component
13:07
or an entire page using Ionix components and using Angular as our framework
13:15
So this is cool and all, and fun text overview. Let's actually look at an example of this
13:25
So I'm here in my browser. You can see we have our list items, our header
13:36
Here's our button from our code sample. we can say click on this note, take a look at some notes that I have written here in this app
13:47
I can save it, check out another note. And this is all just being used, all being done using IAMS Core Components
13:55
If we go over to my editor, I use Vim. So let's see if I can't get screen refresh to work
14:08
What we have inside of the editor is just, oh no, my computer is going so slow
14:27
Let's try this again. Let's go to source app
14:38
Yeah, let's just look at the home page. So here in our component we have the core building blocks that we already gone through with some additional properties or attributes applied to the component
15:00
So here on our Ion header, we have this translucent property binding, and we're setting it to true, meaning that as we go to iOS or to platforms that support this, as the user scrolls, the content should kind of have this underglow effect on the header
15:20
So as you scroll up, you should be able to see some content beneath the header
15:23
This Ion content here on line 12 has full screen, meaning that it should take up the full width of the viewport so that way we can have the scroll effect working
15:37
Everything else in here is still part of the core Angular and Ionic experience
15:43
We're just enhancing these components using Ionics properties and all of their capabilities
15:53
So if we go to now, let's see if my computer wants to behave
16:04
okay so we're going to we're going to go and i know i'm not not that i dislike it but
16:20
it's just not what i am 100 familiar with we're going to use vs code um most people are familiar
16:28
with VS Code. I'm just the odd one who is not as familiar
16:35
with VS Code as everybody else. So if there is something that is going to go
16:38
completely wrong, here is where it's going to happen. So we're going to open up, let's see
16:45
we looked at the homepage HTML. Let's take a look at the actual component logic here
16:51
inside of the TypeScript file. So nothing in here is, like I said
16:58
overly different from a default Angular app, we are just adding some additional features
17:09
like some type information, some additional injectables that we can use, as well as components that we can just
17:15
import right into the main page. Here we have this access to
17:22
some features that we're going to look at in a bit. But the component itself is making use of things like NG on it
17:28
we're using at component decorators. So this is still an Angular app
17:33
just again, enhanced with ionic components. So let's go back to the sides
17:40
and take that very basic app structure that we have, and enhance it with some native features
17:47
So when I say enhance it with native features, I'm going to talk about capacitor because
17:52
capacitor is really what's going to drive the native functionality here. So what is capacitor? We like to call it CAP for short because capacitor is very, very, so it's kind of a long word
18:07
CAP is a two-part JavaScript library and native runtime. So you can take a, you can have some APIs that you can have access to
18:23
you can import them into your project. And then when you want to deploy that to a native device
18:29
you can have access to them in the same way that you would expect
18:32
across iOS, Android, and the web. Now, the way we do this is that we actually have a high-level abstraction
18:42
or an abstract kind of class that you're calling, and then we can swap out the implementations based on what platform you're on
18:50
So iOS is going to get the, if we were to say call the geolocation API, we're going to call the native geolocation services on an iPhone and the native geolocation activity on Android
19:04
And then for the web, we're just going to use Navigator geolocation. So the tooling and the infrastructure on the native side is actually making use of all the native features that native SDKs have had for years now
19:21
So things like CocoaPods and Android libraries, if you're unfamiliar with those, they're essentially our equivalent of node modules
19:32
They're basically native packages that you can install and have a specific version to
19:37
So that way, when you clone a project and then your co-worker clones a project, you can have the same dependencies and ensure that they stay in sync
19:45
So great stuff there. If you're a web dev or if you have some native devs on your team, they should feel right at home
19:52
So architecturally, this doesn't really add anything, but I think it's nice to know how the whole thing works
20:03
At its core, you have your web app here represented in blue that gets loaded into a web view
20:09
This is essentially just a browser without any forward or back buttons or URL bar
20:15
As you use your app, you are making calls to say the capacitors core APIs that get proxied out from the WebView to this native bridge
20:27
Now, the state of bridge is saying, OK, you are asking me for camera access
20:32
Here are the options. Here's the camera. I'll take your options. Let's pass them down to, say, the hardware APIs, get the results, serialize them back in that bridge and then pass it back into the app
20:44
So that way you have this really nice flow that makes sense
20:51
Really all that matters for us Angular developers is that when we use Capacitor, we can target not only a progressive web app, but an Android app and an iOS app, literally with the same code base
21:05
Capacitor itself, like I said, is a separate project from Ionic. so it doesn't have any opinions on components or even framework
21:16
You could be using Angular, like we all are here, or you could be using Vue, React, or Node framework at all
21:23
Capacitor adapts to those needs. You don't also have to be using Ionic, though you totally should
21:31
You could also use things like Angular Material or any of the variant bootstrap libraries that exist out there
21:38
As I said earlier, the API can swap out implementation. So if you are familiar with, say, the camera API or the media devices API on the web, you're going to see that the APIs are not exactly the same as the web variant
21:55
And that is so we can swap out implementations. So let's kind of look at how this works
22:02
Like I said, we can target iOS, Android, and web. We also have a community edition for electron If that is something that people are interested in I have links to it afterwards But let actually see Capacitor in action and how to call native features
22:22
So back in VS Code, we are going to bring in this Capacitor file system package
22:31
Now, Capacitor has all their core APIs split out into separate packages
22:36
So if you need the camera, if you need file system, Bluetooth, you just install the package for it and then it's automatically available
22:44
And here we have access to the file system API. And then we have this directory enum so we can have a consistent way of looking up directories
22:57
What we're going to do is inside of this NG on init, we're going to request the permission for file system access
23:10
And then we're going to do this thing called init file system. Now, init file system is just this very simple way of saying, hey, make a directory and then read that directory
23:24
So this really doesn't do anything. It's the make directory and redirectory that are going to be the main drivers here
23:33
Now, make directory only calls filesystem.makedir. We give it a path and then a directory where we want to store this
23:44
So in this case, we're saying in the directory.documents, make a folder called notes
23:50
So this is going to be our directory. This is how it's all going to get stored
23:55
we can use directory as the directory enum here to pick where we want to create our files
24:02
So you could use directory.documents, downloads, different places for different platforms. When this directory has been made, we're going to read that directory and get all of the files that are available
24:18
So in this case, I'm just going to get all these files and then do a quick little sort to render them out, which is how we're doing all this here
24:27
We're just reading files from the directory, from the documents directory. So for you to go into here, if we're going here, this is the actual file that exists
24:40
We can save. We can come here to this note one. We can actually delete it and delete the file altogether and have this just work
24:48
When we're inside of, say, our edit page, which is doing all the file saving and all of the deletion, we can come down to this save and delete methods
25:05
And we're just saying, hey, file system, write file, give it the path, give it some content or some data to save, give it the directory and the encoding
25:15
And then the rest of this, you know, is just handling some Ionic stuff
25:20
So the big point that I want to get across is that this file system API is fairly approachable
25:28
You have make directory, write file, delete file, pretty clear and concise method names that do exactly what they are supposed to do
25:38
It's much better than some of the file system APIs that have existed on the browser for a while now
25:46
Delete file is pretty similar. We just call delete file, pass it the path
25:51
and the directory, and we're done. They're all async. So we are going to do promises or async await
25:58
depending on what your preference is, but this all works. Now, this is being run in the browser
26:06
but what's the point of running something in the browser when you're supposed to be doing stuff cross-platform
26:13
We can also run this in, say, an iOS device. Where we have this note in here, we could open up, say, hello, world
26:27
No, we're going to say, ooh, spelling error. hello C sharp if nope I can't even get my shift key to work demo gods why must you fail me we'll
26:43
just say C sharp and call it then we can hit save have that note just work we could add a new note
26:54
a new note, have that work well. And this is all running and saving this
27:01
into the iOS directory folders where that directory enum is saving everything
27:11
If we were to say open up an Android emulator, the same thing works here
27:16
We have access to the notes. We could save something new. We could delete a directory
27:21
it all works the same exact way. So I'm actually going to delete and close this emulator
27:32
because I'm 100% sure that this is what's causing some of my computer issues
27:42
And I'm also going to go back to the slides. So kind of towards the end and wrapping up here
27:51
If you want to try Capacitor out right now, obviously commit whatever you're working on and create a new branch
27:59
Be a mindful developer and use version control here and install these dependencies, Capacitor Core and Capacitor CLI, and then run MPX Cap Init
28:12
And this is going to give you a little guided walkthrough of how to set up your project, and then you'll have access to Capacitor
28:18
Add your plugins as you need them, and you're good. If you want to actually learn more about Capacitor or Ionic altogether, go to capacitorjs.com
28:28
We have a full guidance section on how all the plugins will work and how to get access to all these APIs
28:34
And then ionicframework.com is going to give you a guided access to all of the components and interactions that you can use when building out the visuals of your app
28:44
Some parting thoughts, Ionic, I will say that it's an amazing UI library for web and native
28:51
I've been working on it and with the company for like seven years now
28:56
And to this day, Ionic is probably one of my favorite UI libraries. Capacitor
29:03
Capacitor is really awesome right now. It is the solution that I wish I had when I first started
29:10
And Angular. Who doesn't love Angular? But when you add them all together, you get amazing stuff
29:16
So I'm happy to take any questions if I have some time
29:20
And if anyone has any questions, if you can't reach out on Twitter, happy to take them there
29:26
Thank you
#Mobile Apps & Add-Ons
#Mobile Phones