|
| 1 | +import { EventData, Page, ScrollView, TextView, View } from '@nativescript/core'; |
| 2 | +import { DemoSharedInputAccessory } from '@demo/shared'; |
| 3 | +import { InputAccessoryManager } from '@nativescript/input-accessory'; |
| 4 | + |
| 5 | +export function navigatingTo(args: EventData) { |
| 6 | + const page = <Page>args.object; |
| 7 | + page.bindingContext = new DemoModel(page); |
| 8 | +} |
| 9 | + |
| 10 | +export class DemoModel extends DemoSharedInputAccessory { |
| 11 | + private accessoryManager: InputAccessoryManager | null = null; |
| 12 | + private textView: TextView | null = null; |
| 13 | + private page: Page; |
| 14 | + private isAccessorySetup = false; |
| 15 | + private inputText = ''; |
| 16 | + |
| 17 | + constructor(page: Page) { |
| 18 | + super(); |
| 19 | + this.page = page; |
| 20 | + |
| 21 | + this.set('inputText', ''); |
| 22 | + this.set('messages', [ |
| 23 | + 'Welcome to @nativescript/input-accessory 👋', |
| 24 | + 'Tap in the field below to attach the input to the keyboard.', |
| 25 | + 'Type and press Send to append messages while tracking keyboard transitions.', |
| 26 | + 'Random thought: pineapple absolutely belongs on pizza. 🍍', |
| 27 | + 'Just testing long text wrapping in this message bubble for layout stability.', |
| 28 | + 'Keyboard animation looks smooth on both platforms so far.', |
| 29 | + 'Reminder: drink water and stretch every hour 💧', |
| 30 | + 'If this were a real chat, someone would definitely send a GIF here.', |
| 31 | + 'This plugin is perfect for support chats and comment threads.', |
| 32 | + 'Interactive dismiss feels super natural when dragging the list.', |
| 33 | + 'Quick check-in: are we shipping this in the next release?', |
| 34 | + 'Edge case test: message count should keep scrolling correctly.', |
| 35 | + 'Last seeded message: ready for your own typing test 🚀', |
| 36 | + ]); |
| 37 | + |
| 38 | + this.page.on(Page.loadedEvent, () => { |
| 39 | + setTimeout(() => this.setupAccessory(), 100); |
| 40 | + }); |
| 41 | + |
| 42 | + this.page.on(Page.navigatingFromEvent, () => { |
| 43 | + this.accessoryManager?.cleanup(); |
| 44 | + this.accessoryManager = null; |
| 45 | + this.isAccessorySetup = false; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + onMessageInputLoaded(args: EventData) { |
| 50 | + this.textView = args.object as TextView; |
| 51 | + this.setupAccessory(); |
| 52 | + } |
| 53 | + |
| 54 | + onMessageTextChange() { |
| 55 | + this.inputText = this.textView?.text || ''; |
| 56 | + this.accessoryManager?.updateAccessoryHeight(); |
| 57 | + } |
| 58 | + |
| 59 | + onMessageReturnPress() { |
| 60 | + this.sendMessage(); |
| 61 | + } |
| 62 | + |
| 63 | + sendMessage() { |
| 64 | + const inputText = (this.inputText || '').trim(); |
| 65 | + if (!inputText) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + this.appendMessage(`You: ${inputText}`); |
| 70 | + this.set('inputText', ''); |
| 71 | + this.inputText = ''; |
| 72 | + } |
| 73 | + |
| 74 | + dismissKeyboard() { |
| 75 | + this.accessoryManager?.dismissKeyboard(); |
| 76 | + } |
| 77 | + |
| 78 | + private setupAccessory() { |
| 79 | + if (this.isAccessorySetup || !this.textView) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const scrollView = this.page.getViewById<ScrollView>('messagesScrollView'); |
| 84 | + const inputContainer = this.page.getViewById<View>('inputContainer'); |
| 85 | + |
| 86 | + if (!scrollView || !inputContainer) { |
| 87 | + setTimeout(() => this.setupAccessory(), 100); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + this.accessoryManager = new InputAccessoryManager(); |
| 92 | + this.accessoryManager.setup({ |
| 93 | + page: this.page, |
| 94 | + scrollView, |
| 95 | + inputContainer, |
| 96 | + textView: this.textView, |
| 97 | + }); |
| 98 | + |
| 99 | + this.isAccessorySetup = true; |
| 100 | + this.scrollToBottom(); |
| 101 | + } |
| 102 | + |
| 103 | + private appendMessage(message: string) { |
| 104 | + const currentMessages = (this.get('messages') as string[]) || []; |
| 105 | + this.set('messages', [...currentMessages, message]); |
| 106 | + this.scrollToBottom(); |
| 107 | + } |
| 108 | + |
| 109 | + private scrollToBottom() { |
| 110 | + setTimeout(() => { |
| 111 | + this.accessoryManager?.updateAccessoryHeight(); |
| 112 | + this.accessoryManager?.relayoutScrollViewContent(); |
| 113 | + const scrollView = this.page.getViewById<ScrollView>('messagesScrollView'); |
| 114 | + scrollView?.scrollToVerticalOffset(scrollView.scrollableHeight, true); |
| 115 | + }, 100); |
| 116 | + } |
| 117 | +} |
0 commit comments