using System; using System.Collections.Generic; using System.Linq; using ChatComponents; using uiwidgets; using Unity.UIWidgets.async; using Unity.UIWidgets.cupertino; using Unity.UIWidgets.engine; using Unity.UIWidgets.foundation; using Unity.UIWidgets.material; using Unity.UIWidgets.painting; using Unity.UIWidgets.rendering; using Unity.UIWidgets.widgets; using UnityEngine; using Color = Unity.UIWidgets.ui.Color; namespace UIWidgetsSample { public class ChatRoomDemo : UIWidgetsPanel { protected void OnEnable() { base.OnEnable(); } protected override void main() { ui_.runApp(new MyApp()); } class MyApp : StatelessWidget { public override Widget build(BuildContext context) { return new CupertinoApp( home: new DateAndTimePickerWidget() ); } } } [System.Serializable] public class ChatMessages { public List chatMessages; } [System.Serializable] public class ChatMessage { public ChatUser author; public int createdAt; public string id; public string status; public string text; public string type; } [System.Serializable] public class ChatUser { public string firstName; public string id; public string imageUrl; } public class ChatPage : StatefulWidget { public ChatPage(Key key = null) : base(key) { } public override State createState() { return new _ChatPageState(); } } public class _ChatPageState : State { public readonly ChatComponents.User _user = new ChatComponents.User("06c33e8b-e835-4736-80f4-63f44b66666c"); private List _messages = new List(); public int _page = 0; public override void initState() { base.initState(); _loadMessages(); } private void _addMessage(ChatComponents.Message message) { setState(() => { _messages.Insert(0, message); }); } private void _handleAtachmentPressed() { material_.showModalBottomSheet( context, context => { return new SizedBox( height: 144, child: new Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: new List { new CupertinoButton( onPressed: () => { Navigator.pop(context); //_handleImageSelection(); }, child: new Align( alignment: Alignment.centerLeft, child: new Text("Photo") ) ), new CupertinoButton( onPressed: () => { Navigator.pop(context); //_handleFileSelection(); }, child: new Align( alignment: Alignment.centerLeft, child: new Text("File") ) ), new CupertinoButton( onPressed: () => Navigator.pop(context), child: new Align( alignment: Alignment.centerLeft, child: new Text("Cancel") ) ) } ) ); } ); } /* private void _handleFileSelection() { var result = FilePicker.platform.pickFiles( type: FileType.any ); if (result != null) { var message = new ChatComponents.FileMessage( _user, createdAt: DateTime.Now.Millisecond, name: result.files.single.name, id: Uuid().v4(), mimeType: lookupMimeType(result.files.single.path ?? ""), size: result.files.single.size, uri: result.files.single.path ?? "" ); _addMessage(message); } }*/ /* private void _handleImageSelection() { var result = new ImagePicker().getImage( imageQuality: 70, maxWidth: 1440, source: ImageSource.gallery ); if (result != null) { var bytes = result.readAsBytes(); var image = decodeImageFromList(bytes); var name = result.path.split("/").last; var message = new ChatComponents.ImageMessage( _user, createdAt: DateTime.Now.Millisecond, height: image.height.toDouble(), id: Uuid().v4(), name: name, size: bytes.length, uri: result.path, width: image.width.toDouble() ); _addMessage(message); } }*/ private void _handleMessageTap(ChatComponents.Message message) { if (message is ChatComponents.FileMessage) //OpenFile.open(message.uri); Debug.Log("OPEN FILE"); } private void _handlePreviewDataFetched( ChatComponents.TextMessage message, ChatComponents.PreviewData previewData ) { var index = 0; foreach (var element in _messages) { if (element.id == message.id) { index = _messages.IndexOf(element); } } var updatedMessage = _messages[index].copyWith(previewData: previewData); WidgetsBinding.instance?.addPostFrameCallback(_ => { setState(() => { _messages[index] = updatedMessage; }); }); } private void _handleSendPressed(ChatComponents.PartialText message) { var textMessage = new ChatComponents.TextMessage( _user, createdAt: DateTime.Now.Millisecond, id: "b4878b96-efbc-479a-8291-474ef323aa" + _messages.Count.ToString(), text: message.text ); _addMessage(textMessage); } private Future _handleEndReached() { setState(()=> { { _messages.AddRange(_messages); _page = _page + 1; } }); return Future.value(); } private void _loadMessages() { List results = new List(); TextAsset info = Resources.Load("assets/messages"); List chatMessages = new List(); List jsoninfo = info.text.Split('&').ToList(); foreach (var _info in jsoninfo) { var _message = JsonUtility.FromJson(_info); chatMessages.Add(_message); } foreach (var _message in chatMessages) { results.Add( new ChatComponents.TextMessage( author: new User( id: _message.author.id, firstName: _message.author.firstName, imageUrl: _message.author.imageUrl ), createdAt: _message.createdAt, id: _message.id, status: ChatRoomUtils.getStatusFromString(_message.status), text: _message.text ) ); } setState(() => { _messages = results; }); } public override Widget build(BuildContext context) { // return new Container( // color: Color.fromARGB(0, 0, 0, 0), // child: new Chat( // messages: _messages, // onAttachmentPressed: _handleAtachmentPressed, // onMessageTap: _handleMessageTap, // onPreviewDataFetched: ( // previewData, // message) => // { // _handlePreviewDataFetched(message, previewData); // }, // onTextChanged: (_str) => { }, // onSendPressed: _handleSendPressed, // onEndReached: _handleEndReached, // //onEndReachedThreshold: 0.65f, // user: _user // ) // ); return new Scaffold( backgroundColor:Color.fromARGB(0,0,0,0), appBar: new AppBar( backgroundColor: Color.white, centerTitle: false, title: new IconButton( icon: new Icon(Icons.arrow_back, color: Color.black), onPressed: () => { FocusScope.of(context).unfocus(); ChatPanelManager.Switch(); }, padding: EdgeInsets.zero ) ), body: new Container( child: new Chat( messages: _messages, onAttachmentPressed: _handleAtachmentPressed, onMessageTap: _handleMessageTap, onPreviewDataFetched: ( previewData, message) => { _handlePreviewDataFetched(message, previewData); }, onTextChanged: (_str) => { }, onSendPressed: _handleSendPressed, onEndReached: _handleEndReached, //onEndReachedThreshold: 0.65f, user: _user ) ) ); } } }