Example #1
0
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView ?? _activity.LayoutInflater.Inflate(
                Resource.Layout.ContactListItem, parent, false);
            var         contactName  = view.FindViewById <TextView> (Resource.Id.ContactName);
            var         contactImage = view.FindViewById <ImageView> (Resource.Id.ContactImage);
            List <User> users        = DatabaseAccessors.ChatRoomDatabaseAccessor.GetUsers(_chatroomLists [position].webID).ToList();
            string      chatroomName = "Untitled";

            if (users.Count > 0)
            {
                chatroomName = users.ElementAt(0).name;
                if (users.Count > 1 && chatroomName == DatabaseAccessors.CurrentUser().name)
                {
                    chatroomName = users.ElementAt(1).name;
                }
            }
            contactName.Text = chatroomName;


            contactImage = view.FindViewById <ImageView> (Resource.Id.ContactImage);
            contactImage.SetImageResource(Resource.Drawable.ContactImage);


            return(view);
        }
        protected async Task SynchronizeWithParse()
        {
            Console.WriteLine("Synching");
            Console.WriteLine("sy " + DatabaseAccessors.CurrentUser().webID);
            ParseChatRoomDatabase parseDB = new ParseChatRoomDatabase();
            await parseDB.GetAndSyncChatRoomsAsync();

            Console.WriteLine("synched");
            RunOnUiThread(() => chatroomsAdapter.NotifyDataSetChanged());
        }
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			chatroom = DatabaseAccessors.ChatRoomDatabaseAccessor.GetChatRoom (Intent.GetStringExtra ("chatroom"));
			SetContentView (Resource.Layout.ChatRoom);
			chatsAdapter = new ChatAdapter (this);
			var chatsListView = FindViewById<ListView> (Resource.Id.chatsListView);
			chatsListView.Adapter = chatsAdapter;
			chatsListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {

			};

			var sendButton = FindViewById<Button> (Resource.Id.send_message_button);
			var textBoxEdit = FindViewById<EditText> (Resource.Id.chatInputTextBox);
			textBoxEdit.Click += (sender, e) => {
				RunOnUiThread( () => FindViewById<ListView>(Resource.Id.chatsListView).SmoothScrollToPosition(chatsAdapter.GetCount() - 1));
			};
			sendButton.Click += async (sender, e) => {
				EditText textEdit = FindViewById<EditText>(Resource.Id.chatInputTextBox);
				string messageContent = textEdit.Text.ToString();
				RunOnUiThread( () => textEdit.Text = "");
				ParseChatItemDatabase parseItemDB = new ParseChatItemDatabase();
				ChatItem chat = new ChatItem();
				chat.chatroomID = chatroom.webID;
				chat.senderID = DatabaseAccessors.CurrentUser().webID;
				chat.content = messageContent;

				await parseItemDB.SaveChatItemAsync(chat);
				DatabaseAccessors.ChatDatabaseAccessor.SaveItem(chat);
				RunOnUiThread(() => chatsAdapter.NotifyDataSetChanged());
				RunOnUiThread( () => FindViewById<ListView>(Resource.Id.chatsListView).SmoothScrollToPosition(chatsAdapter.GetCount() - 1));

				var push = new ParsePush();
				push.Channels = new List<string> {chatroom.webID};
				push.Alert = "Your men might be requesting help!";
				await push.SendAsync();
			};

			Task getUpdatedInfo = new Task (async () => {
				await SynchronizeWithParse ();
			});

			ParsePush.ParsePushNotificationReceived += async (sender, args) => {
				Console.WriteLine("push");
				await SynchronizeWithParse();
			};

			getUpdatedInfo.Start ();
		}
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Contacts);
            Button searchContactsBtn = FindViewById <Button> (Resource.Id.search_contacts);

            searchContactsBtn.Click += async(sender, e) => {
                GetParseUsers();
                contactsAdapter = new ContactsAdapter(this);
                var contactsListView = FindViewById <ListView> (Resource.Id.contactsListView);
                contactsListView.Adapter    = contactsAdapter;
                contactsListView.ItemClick += async(object sender2, AdapterView.ItemClickEventArgs e2) => {
                    User curritem = contactsAdapter.GetUserAt(e2.Position);
                    ParseChatRoomDatabase pcrd = new ParseChatRoomDatabase();
                    ChatRoom newchatroom       = new ChatRoom();
                    await pcrd.SaveChatRoomAsync(newchatroom);

                    DatabaseAccessors.ChatRoomDatabaseAccessor.SaveChatRoom(newchatroom);
                    List <User> chatroomUsers = new List <User>();
                    chatroomUsers.Add(curritem);
                    chatroomUsers.Add(DatabaseAccessors.CurrentUser());
                    DatabaseAccessors.ChatRoomDatabaseAccessor.SaveChatRoomUsers(chatroomUsers, newchatroom);
                    var crus = DatabaseAccessors.ChatRoomDatabaseAccessor.GetChatRoomUsers(newchatroom.webID);
                    foreach (ChatRoomUser cru in crus)
                    {
                        await pcrd.SaveChatRoomUsersAsync(cru);

                        var push = new ParsePush();
                        push.Channels = new List <string> {
                            cru.userID
                        };
                        push.Alert = "Your men might be requesting help!";
                        await push.SendAsync();
                    }
                    ChatsActivity.NotifyChatRoomsUpdate();

                    var intent = new Intent(this, typeof(ChatRoomActivity));
                    intent.PutExtra("chatroom", newchatroom.webID);
                    StartActivity(intent);

                    this.Finish();
                };
            };
        }
        protected void CreateChatRooms()
        {
            chatroomsAdapter = new ChatRoomsAdapter(this);
            var chatRoomsListView = FindViewById <ListView> (Resource.Id.chatroomsListView);

            chatRoomsListView.Adapter    = chatroomsAdapter;
            chatRoomsListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
                ChatRoom currChatRoom = chatroomsAdapter.GetChatRoomAt(e.Position);
                var      intent       = new Intent(this, typeof(ChatRoomActivity));
                intent.PutExtra("chatroom", currChatRoom.webID);
                StartActivity(intent);
            };
            chatRoomsListView.ItemLongClick += async(object sender, AdapterView.ItemLongClickEventArgs e) => {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetTitle("Do you want to delete this chatroom?");
                alert.SetPositiveButton("Yes", (senderAlert, args) => {
                    ChatRoom currChatRoom = chatroomsAdapter.GetChatRoomAt(e.Position);
                    ParsePush.UnsubscribeAsync(currChatRoom.webID);
                    ChatRoomUser cru = DatabaseAccessors.ChatRoomDatabaseAccessor.DeleteChatRoomUser(DatabaseAccessors.CurrentUser().webID, currChatRoom.webID);
                    DatabaseAccessors.ChatRoomDatabaseAccessor.DeleteChatRoom(currChatRoom.webID);
                    ParseChatRoomDatabase pcrd = new ParseChatRoomDatabase();
                    pcrd.DeleteChatRoomUserAsync(cru);
                    Console.WriteLine("ERASED");
                    chatroomsAdapter.NotifyDataSetChanged();
                });
                alert.SetNegativeButton("No", (senderAlert, args) => {
                });
                alert.Show();
            };
        }