Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile JavaScript Development

Mobile JavaScript Development

Stefan Cosma

May 17, 2019
Tweet

More Decks by Stefan Cosma

Other Decks in Programming

Transcript

  1. Stefan Cosma Senior Front-end Developer @ Ropardo Developer, builder of

    open source projects, comics enthusiast and amateur photographer. ➔ @stefanbc ➔ [email protected] ➔ stefancosma.xyz
  2. Timeline 1990 - 1995 : HTML, CSS and JavaScript are

    invented. 1996 - 1999 : Standardization efforts begin. Browser compliance is terrible. Browser wars ignite. 2000 - 2004 : CSS frameworks begin to emerge. jQuery is born. Frontend package management. 2005 - 2009 : W3C specification compliance is met. Chrome browser takes the lead. Responsive designs and frameworks are introduced.
  3. Timeline 2010 - 2015: JavaScript Frameworks are born - i.e.

    Backbone, Ember, AngularJS, React, Angular, Vue. HTML5 is announced. 2016 - 2019: GraphQL emerges. Native HTML, CSS & JavaScript become more powerful. New platforms built on top of existing JavaScript frameworks emerge: Motion UI, Gatsby, Next.js.
  4. Timeline Brendan Eich wrote Mocha in 10 days in May

    1995. Mocha becomes LiveScript later. In December 1995 the language was renamed again to it’s final name: JavaScript
  5. Native apps tl;dr: It depends quite a bit on the

    context and requirements of your app.
  6. Native apps Xamarin It lets you build one app that

    runs on many platforms using C#. Use Xamarin to write native iOS, Android, and Windows apps with native user interfaces and share code across multiple platforms.
  7. Native apps for end users hybrid = native Hybrid apps

    must feel native, or users are going to uninstall the app.
  8. Native apps Are built for specific platforms and are written

    in the languages the platform accepts (eg. Swift and Objective-C for iOS apps and Java or Kotlin for Android apps).
  9. Native apps Advantages of Native Apps: ➔ Fast and responsive

    since they are built for a specific platform. ➔ Have the best performance. ➔ More interactive, intuitive and much smoother in terms of user I/O. ➔ Allows developers to access the full feature set of the platform. ➔ Internet connection is not required, depending on the functionality. ➔ To the user, the flow is more natural as each platform has specific UI standards.
  10. Native apps Disadvantages of Native Apps: ➔ Difficult programming languages

    to learn which means you need experienced developers. ➔ More expensive. ➔ Not the best option for very simple apps.
  11. Native apps “Pure” native can still use the web: web

    views allow native apps to make use of web content.
  12. Progressive Web Apps Are not a technology, but a collection

    of features an application should support.
  13. Progressive Web Apps ➔ WebRTC ➔ Media Capture and Streams

    ➔ Geolocation ➔ Web Notifications ➔ Payment Request API ➔ Web Share API ➔ Generic Sensor API ➔ Canvas ➔ WebGL ➔ Gamepad API ➔ Web Bluetooth API ➔ Speech Synthesis ➔ Shape Detection API ➔ IndexedDB ➔ WebVR ➔ Web Cryptography API
  14. Progressive Web Apps How to distinguish websites from apps? Add

    a file with metadata for apps only so that apps can be identified by search engines, app store providers etc. Web App Manifest: <link rel="manifest" href="manifest.json">
  15. Progressive Web Apps manifest.json { "short_name": "PWA Demo", "name": "PWA

    Demo", "icons": [ { "src": "assets/icon-144x144.png", "sizes": "144x144", "type": "image/png" } ], "start_url": "/index.html", "display": "standalone" }
  16. Progressive Web Apps Service Worker ➔ Worker snippet is executed

    in it’s own thread. ➔ Worker can’t manipulate the parent document’s DOM. ➔ Communication via API (postMessage) + Acts as a controller / proxy / interceptor and performs background tasks.
  17. Progressive Web Apps Basic Implementation navigator.serviceWorker.register('sw.js') .then(subscription => console.log(subscription)) .catch(err

    => console.error('A wild error has appeared!', err)); self.addEventListener('install', event => {}); self.addEventListener('activate', () => {}); self.addEventListener('fetch', event => {});
  18. Progressive Web Apps Cache API ➔ Key-value storage ➔ Key:

    HTTP(S) request, value: HTTP(S) response ➔ Survives browser restarts (Safari: unused caches are cleared “after a ➔ few weeks”) ➔ Can be accessed from both service worker and website ➔ Isolated per origin (protocol + hostname + port) ➔ Multiple named caches per origin ➔ Cache operations are asynchronous (Promises)
  19. Progressive Web Apps Adding Requests to the cache self.addEventListener('install', event

    => { event.waitUntil( caches.open('pwa-demo') .then(cache => cache.addAll(['/', '/index.html'])) .then(() => self.skipWaiting()) ); });
  20. Progressive Web Apps Delivering from the cache self.addEventListener('fetch', event =>

    { event.respondWith( caches.match(event.request) .then(response => response || fetch(event.request)) ); });
  21. Progressive Web Apps IndexedDB Is a database in the browser

    capable of storing structured data in tables with keys and value. Data is stored permanently (survives browser restarts). Service Worker and website share access to the same IndexedDB database (e.g. for synchronization purposes).
  22. Progressive Web Apps Summary ➔ Write once, run anywhere (mobile,

    desktop, browser, …) ➔ One development team, one code base ➔ Limited to what the web can offer (API support is huge & growing) ➔ Can also be submitted to Google Play and Microsoft Store ➔ Can be monetized (Payment Request API or traditional checkout forms) ➔ Also run on legacy browsers (IE)
  23. React Native It uses the same design as React, letting

    you compose a rich mobile UI from declarative components.
  24. React Native Efficient Re-rendering through Virtual DOM. Abstracts the DOM

    and gives a simpler programming model and better performance. Minimizes the repaints focusing only on what has changed.
  25. React Native Express how the app should look at any

    given point in time. React will manage all the UI updates when your data changes.
  26. React Native Native components ➔ standard platform components such as

    UITabBar on iOS or Navigation Drawer on Android ➔ consistent look and feel with the rest of the platform ecosystem
  27. React Native Asynchronous execution ➔ bridge between JS and native

    platform is async ➔ JS runs in the background ➔ it makes apps fluid and responsive
  28. React Native Prerequisites: node/npm $ npm install -g react-native-cli Install

    XCode and/or Android Studio $ react-native run-ios $ react-native run-android
  29. React Native $ react-native init AwesomeProject Checkout App.js for your

    application code Checkout the android and ios folders for platform specific code
  30. React Native export default class App extends Component<Props> { constructor(props)

    { super(props); this.state = { count: 0 }; } incrementCount = () => (event) => { ... }; render() { return ( <View style={styles.container}> <Text style={styles.welcome}> Button Presses: {this.state.count} </Text> <Button onPress={this.incrementCount()} title="Click Me" /> </View> ); } } const styles = StyleSheet.create({ ... });
  31. React Native Navigation Navigation is a slight pain Obviously, URL

    is not available like a web app Routing libraries are/were still competing Navigation can get tricky when promises are involved… react-navigation uses navigation.navigate to change the current view
  32. React Native State State management & updates are tricky You

    can do a lot with props and state Relationships can get complicated…
  33. React Native Platform-Specific Android App Bar iOS navigation Android text

    components / underline iOS SafeAreaView Keyboard-aware layouts
  34. React Native React Native & React significantly accelerate mobile app

    development. But, they come with some pitfalls to be aware of.
  35. Hybrid Apps HTML5 with platform-specific UI Full access to native

    APIs and SDKs Familiar web development environment Single code base across native and the web
  36. Hybrid Apps ➔ Hybrid Apps: HTML5 that acts like native

    ➔ Web wrapped in native layer ➔ Direct access to native APIs ➔ Familiar web dev environment ➔ Develop a single code base (web platform)
  37. Hybrid Apps Performant animation with GPU compositing Advanced mobile device

    features and APIs Web-workers to offload tasks from UI Modern Chromium for Android
  38. Hybrid Apps It's the wild-west for hybrid apps A gap

    between web and native We need rich, native-like UI components and interactions We need UI APIs, not just jQuery widgets
  39. Hybrid Apps Frameworks ➔ Ionic ➔ Framework7 ➔ Mobile Angular

    UI ➔ Onsen UI ➔ Sencha ➔ Kendo ➔ Quasar ➔ NativeScript
  40. Hybrid Apps A hybrid app consists of two parts: 1.

    the backend code which is built using languages such as HTML, CSS, or JavaScript 2. native shell that is downloadable and loads the code using a webview.
  41. Hybrid Apps HOW IT ALL COMES TOGETHER 1. Your App

    2. UI Framework 3. Angular / EmberJs / VueJs / React 4. Cordova 5. Native SDKs
  42. Ionic COMPLEX LISTS ➔ AngularJS Directive ➔ Buttons exposed by

    swiping ➔ Reorder / Delete <ion-list> <button ion-item *ngFor="let item of items"> {{ item.title }} </button> </ion-list>
  43. Ionic VIRTUAL SCROLLING ➔ Inspired by iOS’s UICollectionView ➔ Scroll

    through thousands of items ➔ Only renders the viewable items ➔ Smooth scrolling! <ion-list [virtualScroll]="items"> <ion-item *virtualItem="let item"> <ion-avatar item-start> <ion-img [src]="item.avatarUrl"></ion-img> </ion-avatar> {{ item.firstName }} {{ item.lastName }} </ion-item> </ion-list>
  44. Ionic OTHER COMPONENTS Side Menus, Actionsheet, Modal, Pull To Refresh,

    Spinners, Slidebox, Infinite Scroll, Swipeable List Options, Popup, Popover, Loading Overlay, Inputs, Buttons etc.
  45. Ionic 3 vs Ionic 4 ➔ Ionic 4 was rebuilt

    using standard Web APIs, and each component is packaged as a Web Component. ➔ Ionic 4 is completely agnostic of the base framework. ➔ Ionic 4 now uses the Angular Router.
  46. Ionic 3 vs Ionic 4 $ npm install -g ionic@latest

    $ ionic start appName blank --type=angular $ ionic serve $ ionic g page User $ ionic g service services/user
  47. Ionic 3 vs Ionic 4 import { NgModule } from

    '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', loadChildren: './pages/home/home.module#HomePageModule' }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } import { Router } from '@angular/router'; constructor(private router: Router) { } navigateToHome(){ this.router.navigate(['/home']); }
  48. Ionic 3 vs Ionic 4 Life cycles that were used

    in Ionic 3 such as ionWillLoad will no longer be used in Ionic 4. Angular life cycles such as ngOnInit and ngAfterViewInit will be used.
  49. Ionic 3 vs Ionic 4 Angular Modules No longer necessary

    to import pages and services in the app.module.ts file. When using lazy loading, for each page there will be a module.
  50. Conclusion Choosing a development method should NOT be determined solely

    by cost. User experience should be the primary factor that helps you decide whether to build a native, PWA, React Native or hybrid app.
  51. Conclusion The choice between all options is dependent on a

    number of factors, including business needs, app requirements, developer skill, and timelines.