From d0bb13560721497d214097b435ea7472bd118cd7 Mon Sep 17 00:00:00 2001 From: Martin Hettiger Date: Mon, 1 Apr 2019 11:26:37 +0200 Subject: [PATCH] fix(#29): implement a carousel friendly swipe refresh layout --- demo/app/main-page.xml | 37 ++++-- demo/package.json | 4 + src/pulltorefresh.android.ts | 213 ++++++++++++++++++++--------------- 3 files changed, 156 insertions(+), 98 deletions(-) diff --git a/demo/app/main-page.xml b/demo/app/main-page.xml index 561d4f5..f8b667d 100644 --- a/demo/app/main-page.xml +++ b/demo/app/main-page.xml @@ -1,13 +1,36 @@ - + - - - + + + + + - \ No newline at end of file + diff --git a/demo/package.json b/demo/package.json index ead74a5..a74fff8 100644 --- a/demo/package.json +++ b/demo/package.json @@ -3,9 +3,13 @@ "id": "org.nativescript.pulltorefresh", "tns-ios": { "version": "5.2.0" + }, + "tns-android": { + "version": "5.2.1" } }, "dependencies": { + "nativescript-carousel": "^4.1.0", "nativescript-pulltorefresh": "file:../src", "nativescript-theme-core": "^1.0.4", "nativescript-unit-test-runner": "^0.5.0", diff --git a/src/pulltorefresh.android.ts b/src/pulltorefresh.android.ts index 58c13c2..ed88e11 100644 --- a/src/pulltorefresh.android.ts +++ b/src/pulltorefresh.android.ts @@ -1,91 +1,122 @@ -/// - -import { Color } from 'tns-core-modules/color'; -import { - PullToRefreshBase, - backgroundColorProperty, - colorProperty, - refreshingProperty -} from './pulltorefresh-common'; - -export * from './pulltorefresh-common'; - -export class PullToRefresh extends PullToRefreshBase { - private _androidViewId: number; - - public nativeView: any; // android.support.v4.widget.SwipeRefreshLayout; - - get android(): any { - return this.nativeView; // android.support.v4.widget.SwipeRefreshLayout - } - - public createNativeView() { - const swipeRefreshLayout = new (android.support.v4 - .widget as any).SwipeRefreshLayout(this._context); - - if (!this._androidViewId) { - this._androidViewId = android.view.View.generateViewId(); - } - swipeRefreshLayout.setId(this._androidViewId); - - const refreshListener = new TNS_SwipeRefreshListener(new WeakRef(this)); - swipeRefreshLayout.setOnRefreshListener(refreshListener); - (swipeRefreshLayout as any).refreshListener = refreshListener; - - return swipeRefreshLayout; - } - - public initNativeView() { - super.initNativeView(); - - const nativeView = this.nativeView as any; - nativeView.refreshListener.owner = new WeakRef(this); - } - - public disposeNativeView() { - const nativeView = this.nativeView as any; - nativeView.refreshListener.owner = null; - - super.disposeNativeView(); - } - - [refreshingProperty.getDefault](): boolean { - return false; - } - [refreshingProperty.setNative](value: boolean) { - this.nativeView.setRefreshing(value); - } - - [colorProperty.setNative](value: Color | number) { - const color = value instanceof Color ? value.android : value; - this.nativeView.setColorSchemeColors([color]); - } - - [backgroundColorProperty.setNative](value: Color | number) { - const color = value instanceof Color ? value.android : value; - this.nativeView.setProgressBackgroundColorSchemeColor(color); - } -} - -@Interfaces([ - (android.support.v4.widget as any).SwipeRefreshLayout.OnRefreshListener -]) -class TNS_SwipeRefreshListener extends java.lang.Object { - constructor(private owner: WeakRef) { - super(); - - return global.__native(this); - } - - public onRefresh(v) { - const owner = this.owner.get(); - - if (owner) { - owner.refreshing = true; - owner.notify({ - eventName: PullToRefreshBase.refreshEvent, - object: owner - }); - } - } -} +/// + +import { Color } from 'tns-core-modules/color'; +import { + PullToRefreshBase, + backgroundColorProperty, + colorProperty, + refreshingProperty +} from './pulltorefresh-common'; + +export * from './pulltorefresh-common'; + +class CarouselFriendlySwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout { + private _touchSlop: number; + private _previousX: number; + + public constructor(context: android.content.Context, attrs: android.util.AttributeSet) { + super(context, attrs); + + this._touchSlop = android.view.ViewConfiguration.get(context).getScaledTouchSlop(); + } + + public onInterceptTouchEvent(event: android.view.MotionEvent): boolean { + switch (event.getAction()) { + case android.view.MotionEvent.ACTION_DOWN: { + this._previousX = android.view.MotionEvent.obtain(event).getX(); + break; + } + case android.view.MotionEvent.ACTION_MOVE: { + const eventX = event.getX(); + const xDifference = Math.abs(eventX - this._previousX); + + if (xDifference > this._touchSlop) { + return false; + } + + break; + } + } + + return super.onInterceptTouchEvent(event); + } +} + +export class PullToRefresh extends PullToRefreshBase { + private _androidViewId: number; + + public nativeView: any; // android.support.v4.widget.SwipeRefreshLayout; + + get android(): any { + return this.nativeView; // android.support.v4.widget.SwipeRefreshLayout + } + + public createNativeView() { + const swipeRefreshLayout = new (CarouselFriendlySwipeRefreshLayout as any)(this._context); + + if (!this._androidViewId) { + this._androidViewId = android.view.View.generateViewId(); + } + swipeRefreshLayout.setId(this._androidViewId); + + const refreshListener = new TNS_SwipeRefreshListener(new WeakRef(this)); + swipeRefreshLayout.setOnRefreshListener(refreshListener); + (swipeRefreshLayout as any).refreshListener = refreshListener; + + return swipeRefreshLayout; + } + + public initNativeView() { + super.initNativeView(); + + const nativeView = this.nativeView as any; + nativeView.refreshListener.owner = new WeakRef(this); + } + + public disposeNativeView() { + const nativeView = this.nativeView as any; + nativeView.refreshListener.owner = null; + + super.disposeNativeView(); + } + + [refreshingProperty.getDefault](): boolean { + return false; + } + [refreshingProperty.setNative](value: boolean) { + this.nativeView.setRefreshing(value); + } + + [colorProperty.setNative](value: Color | number) { + const color = value instanceof Color ? value.android : value; + this.nativeView.setColorSchemeColors([color]); + } + + [backgroundColorProperty.setNative](value: Color | number) { + const color = value instanceof Color ? value.android : value; + this.nativeView.setProgressBackgroundColorSchemeColor(color); + } +} + +@Interfaces([ + (android.support.v4.widget as any).SwipeRefreshLayout.OnRefreshListener +]) +class TNS_SwipeRefreshListener extends java.lang.Object { + constructor(private owner: WeakRef) { + super(); + + return global.__native(this); + } + + public onRefresh(v) { + const owner = this.owner.get(); + + if (owner) { + owner.refreshing = true; + owner.notify({ + eventName: PullToRefreshBase.refreshEvent, + object: owner + }); + } + } +}