예제 #1
0
		public static void NavigateToChatDetailsPage(User userInfo, string tosusrID, string userImageUrl, string toUserName )
		{
			Device.BeginInvokeOnMainThread(  async () => 
				{
					ObservableCollection<ChatDetails> chats = new ObservableCollection<ChatDetails>();

					ChatDetails test = new ChatDetails();
					test.AuthorName = "prvn";
					test.CurrentUserid = App.Settings.GetUser().UserId;
					test.Message = "test chat";

					chats.Add( test );

					await Navigator.PushModalAsync( new ChatDetailsPage( chats,tosusrID, userImageUrl, toUserName) );


				});
		}
		public ChatDetailsPage ( ObservableCollection<ChatDetails> chats,string tosusrID, string userImageUrl, string toUserName )
		{


			App.CurrentChatUserID = tosusrID;
			NavigationPage.SetHasNavigationBar(this, false);

			chatList = chats;
			touserID = tosusrID;
			currentuser = App.Settings.GetUser ();

		    timer = DependencyService.Get<IAdvancedTimer>();
			timer.initTimer (30000, SyncChatHistoryFromServer, true);
			timer.startTimer ();
			//Xamarin.Forms.Device.StartTimer ( TimeSpan.FromSeconds( 1 ), SyncChatHistoryFromServer );


			string chatTouser = toUserName;

			if (chatTouser.Length > 30)
			{
				chatTouser = chatTouser.Substring(0, 30);
				chatTouser += "...";
			}


			progressBar = DependencyService.Get< IProgressBar > ();
			mainTitleBar = new PurposeColorTitleBar(Color.FromRgb(8, 135, 224), chatTouser, Color.Black, userImageUrl, true);
			mainTitleBar.imageAreaTapGestureRecognizer.Tapped += (object sender, EventArgs e) => 
			{
				App.masterPage.IsPresented = true;
			};
			subTitleBar = new CommunityGemChatTitleBar(Constants.SUB_TITLE_BG_COLOR, chatTouser, userImageUrl, false);
			subTitleBar.BackButtonTapRecognizer.Tapped += async (object sender, EventArgs e) => 
			{
				timer.stopTimer ();
				App.CurrentChatUserID = null;
				await Navigation.PopAsync();
			};

			masterLayout = new CustomLayout ();
			masterLayout.WidthRequest = App.screenWidth;
			masterLayout.HeightRequest = App.screenHeight - 50;
			masterLayout.BackgroundColor = Color.FromRgb(45, 62, 80);

			chatHistoryListView = new ListView();
			chatHistoryListView.ItemTemplate = new DataTemplate(typeof(ChatHistoryListCell));
			chatHistoryListView.SeparatorVisibility = SeparatorVisibility.None;
			chatHistoryListView.HeightRequest = App.screenHeight * 70 / 100;
			chatHistoryListView.HasUnevenRows = true;
			chatHistoryListView.BackgroundColor =  Color.FromRgb(54, 79, 120);
			chatHistoryListView.ItemsSource = chatList;
			if( chatList != null && chatList.Count > 1 )
				chatHistoryListView.ScrollTo( chatList[ chatList.Count -1 ], ScrollToPosition.End, true );
			
			chatHistoryListView.ItemTapped += (object sender, ItemTappedEventArgs e) => 
			{
				chatHistoryListView.SelectedItem = null;
			};
			chatHistoryListView.ItemSelected += (object sender, SelectedItemChangedEventArgs e) => 
			{
				chatHistoryListView.SelectedItem = null;
			};


		

			ExtendedEntry chatEntry = new ExtendedEntry
			{
				Placeholder = "Enter your chat...",
				BackgroundColor = Color.White,//Color.White,
				WidthRequest = App.screenWidth * .80,
				HorizontalOptions = LayoutOptions.Start,
				TextColor = Color.Black
			};
			chatEntry.TextChanged += ChatEntry_TextChanged;

			Image postChatButton = new Image();
			postChatButton.Source = Device.OnPlatform("icon_send.png", "icon_send.png", "//Assets//icon_send.png");

			postChatButton.VerticalOptions = LayoutOptions.Center;
			postChatButton.HorizontalOptions = LayoutOptions.Center;
			TapGestureRecognizer postChatButtonTap = new TapGestureRecognizer();

			postChatButton.GestureRecognizers.Add(postChatButtonTap);

			StackLayout inputCountainer = new StackLayout
			{
				Spacing = Device.OnPlatform(5, 5, 1),
				Padding = Device.OnPlatform(5, 5, 5),
				Orientation = StackOrientation.Horizontal,
				BackgroundColor = Color.FromRgb( 45, 62, 80 ),
				Children = { chatEntry, postChatButton },
				WidthRequest = App.screenWidth
			};


			masterLayout.AddChildToLayout(mainTitleBar, 0, 0);
			masterLayout.AddChildToLayout(subTitleBar, 0, Device.OnPlatform(9, 10, 10));
			masterLayout.AddChildToLayout ( chatHistoryListView, 0, 17 );
			masterLayout.AddChildToLayout ( inputCountainer, 0, 85 );

			ScrollView masterScroll = new ScrollView ();
			masterScroll.Orientation = ScrollOrientation.Vertical;
			masterScroll.Content = masterLayout;
			masterScroll.IsClippedToBounds = true;

			postChatButtonTap.Tapped += async (object sender, EventArgs e) => 
			{
				ChatDetails detail = new ChatDetails();
				detail.AuthorName = "prvn";
				detail.Message = chatEntry.Text;
				detail.FromUserID = currentuser.UserId.ToString();
				detail.CurrentUserid = currentuser.UserId.ToString();
				chatList.Add( detail );
				chatEntry.Text = "";
				chatHistoryListView.ScrollTo( chatList[ chatList.Count -1 ], ScrollToPosition.End, true );

				if(!string.IsNullOrEmpty( detail.Message ))
					await ServiceHelper.SendChatMessage( currentuser.UserId.ToString(), touserID, detail.Message );
			};



			/*	this.Appearing += async (object sender, EventArgs e) => 
			{

				progressBar.ShowProgressbar( "Preparing chat window..." );
				masterScroll.IsVisible = true;

				chatUsersList = await ServiceHelper.GetAllChatUsers ();



				progressBar.HideProgressbar();

			};*/


			MessagingCenter.Subscribe<CrossPushNotificationListener, string>(this, "boom", (page, message) =>
				{
					string pushResult = message;
					string[] delimiters = { "&&" };
					string[] clasIDArray = pushResult.Split(delimiters, StringSplitOptions.None);
					string chatMessage = clasIDArray [0];
					string fromUser = clasIDArray [1];


					if( touserID == fromUser )
					{
						ChatDetails detail = new ChatDetails();
						detail.AuthorName = fromUser;
						detail.Message = chatMessage;
						detail.FromUserID = fromUser;
						detail.CurrentUserid = currentuser.UserId.ToString();
						chatList.Add( detail );

						if( chatList != null && chatList.Count > 1 )
						chatHistoryListView.ScrollTo( chatList[ chatList.Count -1 ], ScrollToPosition.End, true );
					}


				});


			Content = masterScroll;

		}
		public ChatPage ()
		{
			NavigationPage.SetHasNavigationBar(this, false);
			progressBar = DependencyService.Get< IProgressBar > ();
			mainTitleBar = new PurposeColorTitleBar(Color.FromRgb(8, 135, 224), "Purpose Color", Color.Black, "back", true);
			mainTitleBar.imageAreaTapGestureRecognizer.Tapped += (object sender, EventArgs e) => 
			{
				App.masterPage.IsPresented = true;
			};
			subTitleBar = new CommunityGemSubTitleBar(Constants.SUB_TITLE_BG_COLOR, Constants.COMMUNITY_GEMS, true);
			subTitleBar.BackButtonTapRecognizer.Tapped += async (object sender, EventArgs e) => 
			{
				await Navigation.PopAsync();
			};

			this.Appearing += OnChatPageAppearing;
			masterLayout = new CustomLayout ();
			masterLayout.WidthRequest = App.screenWidth;
			masterLayout.HeightRequest = App.screenHeight;


			chatContactsListView = new ListView();
			chatContactsListView.ItemTemplate = new DataTemplate(typeof(ChatContactListCell));
			chatContactsListView.SeparatorVisibility = SeparatorVisibility.Default;
			chatContactsListView.BackgroundColor = Color.White;
			chatContactsListView.HeightRequest = App.screenHeight * 90 / 100;
			chatContactsListView.HasUnevenRows = true;
			//chatContactsListView.RowHeight = (int) App.screenHeight * 10 / 100;
			chatContactsListView.SeparatorColor = Color.FromRgb (8, 135, 224);
			chatContactsListView.ItemSelected +=  async (object sender, SelectedItemChangedEventArgs e) => 
			{
				string curentUserId = App.Settings.GetUser().UserId.ToString();
				ObservableCollection<ChatDetails> chats = new ObservableCollection<ChatDetails>();
				ChatUsersInfo selItem = chatContactsListView.SelectedItem as ChatUsersInfo;
				if( selItem != null )
				{
					ChatHistoryObject history = await ServiceHelper.GetChatHistory ( selItem.user_id, curentUserId );

					if (history != null)
					{
						foreach (var item in history.resultarray) 
						{
							ChatDetails detail = new ChatDetails ();
							detail.CurrentUserid = curentUserId;
							detail.FromUserID = item.from_id;
							detail.Message = item.msg;
							chats.Add ( detail );
						}
					}

					await Navigation.PushAsync( new ChatDetailsPage( chats, selItem.user_id, selItem.profileImgUrl,  selItem.firstname ) );
				}
				chatContactsListView.SelectedItem= null;
			};


			masterLayout.AddChildToLayout(mainTitleBar, 0, 0);
			masterLayout.AddChildToLayout(subTitleBar, 0, Device.OnPlatform(9, 10, 10));
			masterLayout.AddChildToLayout ( chatContactsListView, 0, 10 );

			Content = masterLayout;
		}