Skip to content

Instantly share code, notes, and snippets.

View NathanWalker's full-sized avatar

Nathan Walker NathanWalker

View GitHub Profile
@NathanWalker
NathanWalker / NativeScript background service bootstrap
Last active June 30, 2022 04:10
NativeScript for Angular (No UI) module to handle only background services
@NgModule({
imports: [NativeScriptModule],
})
export class BackgroundModule {
ngDoBootstrap() {
// do nothing, this is not an UI module
}
}
const moduleRefPromise = platformNativeScript().bootstrapModule(AppModule);
@NathanWalker
NathanWalker / NativeScript > main.ts
Last active June 30, 2022 04:08
NativeScript for Angular bootstrap
import {
platformNativeScript,
runNativeScriptAngularApp,
} from "@nativescript/angular";
import { AppModule } from "./app/app.module";
// runs the NativeScript application, nothing is bootstrapped yet, as we're only setting up the platform and callbacks
runNativeScriptAngularApp({
// this function will be called when we need to display the application UI
// a major difference from the web is that this module may be destroyed when the user leaves the application and recreated when the user opens it again
@NathanWalker
NathanWalker / Web > main.ts
Created June 30, 2022 04:07
Angular web bootstrap
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./app/app.module";
// locate the app-root and bootstrap the component in it's place
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
@NathanWalker
NathanWalker / usage-intructions.md
Last active June 19, 2021 01:42
@nativescript/schematics with NativeScript 7.x.x and Angular 10.1.x - 11.x.x
  1. Install the latest NativeScript cli (7.x.x)
npm i -g nativescript
  1. Choose A or B depending on your use case:

A) Starting a new Angular web project and adding {N} to it afterwards:

@NathanWalker
NathanWalker / http.interceptor.ts
Last active February 23, 2021 18:43
Simple offline handling with NativeScript using Angular as example
// pseudo code using angular flavor as example
@Injectable({ providedIn: 'root' })
export class SomeHttpInterceptor implements HttpInterceptor {
_mobileOffline = new MobileOffline();
constructor(private mobileStorage: MobileAPIStorageService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!this._mobileOffline.isOnline) {
@NathanWalker
NathanWalker / native-class.ios.ts
Last active October 28, 2020 18:30
Example of using a single file containing a single @nativeclass decorated class with NativeScript 7
/**
* {N} 7 uses es2017 targeting
* @NativeClass decorator transforms native class extensions to work with iOS/Android runtimes
* When used in a single file by itself, the transformation modifies the esm export syntax
* Without any other esm exports in the file, you will want to export the symbol at bottom explicitly
* This will ensure a proper esm export of your extended native class symbol will work as expected.
*/
@NativeClass()
class MyNativeClassExt extends NSObject {
@NathanWalker
NathanWalker / webpack.config.js
Last active June 28, 2020 01:44
webpack to work with @nativescript/core and alias tns-core-modules
const { join, relative, resolve, sep, dirname } = require('path');
const webpack = require('webpack');
const nsWebpack = require('nativescript-dev-webpack');
const nativescriptTarget = require('nativescript-dev-webpack/nativescript-target');
const {
nsReplaceBootstrap
} = require('nativescript-dev-webpack/transformers/ns-replace-bootstrap');
const {
nsReplaceLazyLoader
@NathanWalker
NathanWalker / core.module.ts
Last active June 6, 2020 15:31
xplat storage service which uses localStorage on the web and ApplicationSettings on mobile.
// /xplat/nativescript/core/core.module.ts
import { MobileStorageService } from './services/mobile-storage.service';
// Add this to providers, for example:
@NgModule({
imports: [
NativeScriptModule,
...
CoreModule.forRoot([
@NathanWalker
NathanWalker / fixAndroidScrollMomentum
Created April 21, 2020 01:36
Invert NativeScript ListView trick to feed chat items in from bottom up (Android workaround for proper momentum scroll on API level 28)
export function fixAndroidScrollMomentum(listview: ListView) {
if (android.os.Build.VERSION.SDK_INT !== 28) {
return;
}
// suppress the default momentum behaviour
listview.android.setVelocityScale(0);
// stores the last 5 move events in this array
let lastMoveEvents = [];
(<any>listview).on(GestureTypes.touch, (args, b) => {
if (args.action === 'up') {
@NathanWalker
NathanWalker / animate.directive.ts
Last active January 21, 2019 01:09
NativeScript for Angular animate directive for silky smooth mobile (iOS/Android) animations anytime, anywhere and on anything.
/**
* Add this to your app's SharedModule declarations
*/
import { Directive, ElementRef, Input } from '@angular/core';
// nativescript
import { View } from 'tns-core-modules/ui/core/view';
import { Animation, AnimationDefinition } from 'tns-core-modules/ui/animation';