Skip to content

Instantly share code, notes, and snippets.

@Dobermensch
Last active February 17, 2019 14:41
Show Gist options
  • Save Dobermensch/49ee9d8adb9a83a38790b691c857691d to your computer and use it in GitHub Desktop.
Save Dobermensch/49ee9d8adb9a83a38790b691c857691d to your computer and use it in GitHub Desktop.
CS codebehind file for chat page
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Chatfer.Pages.Chat" xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" >
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<OnPlatform.Platforms>
<On Platform="iOS" Value="0, 40, 0, 0" />
<On Platform="Android, UWP" Value="0, 0, 0, 0" />
</OnPlatform.Platforms>
</OnPlatform>
</ContentPage.Padding>
<ContentPage.Content>
<StackLayout>
<StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal" BackgroundColor="#727272">
<Button Text="&lt; Back" FontAttributes="Bold" FontSize="14" TextColor="#FBFBFB" HorizontalOptions="StartAndExpand" BackgroundColor="Transparent" Clicked="Back_Clicked"/>
<Label x:Name="nameLabel" HorizontalOptions="CenterAndExpand" FontSize="24" VerticalOptions="Center" TextColor="#FBFBFB"/>
<ffimageloading:CachedImage x:Name="recipientImage" HorizontalOptions="EndAndExpand" VerticalOptions="Center" HeightRequest="42" Margin="13,0,16,0">
<ffimageloading:CachedImage.Transformations>
<fftransformations:CircleTransformation>
<x:Arguments>
<x:Double>2</x:Double>
<x:String>#e8e8e8</x:String>
</x:Arguments>
</fftransformations:CircleTransformation>
</ffimageloading:CachedImage.Transformations>
</ffimageloading:CachedImage>
</StackLayout>
<ListView VerticalOptions="FillAndExpand" HorizontalOptions="Fill" x:Name="msgList" SeparatorVisibility="None" SelectionMode="None" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell IsEnabled="False">
<Label Text="{Binding Mess}" FontSize="16" Margin="20,5,20,20" FontFamily="GillSans-Light" TextColor="Gray" HorizontalTextAlignment="{Binding textAlignment}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Horizontal" VerticalOptions="End">
<Entry x:Name="enteredMessage" Placeholder="Say Something..." PlaceholderColor="#d1d1d1" TextColor="#807E7E" HorizontalOptions="FillAndExpand" Keyboard="Chat"/>
<Button Text="Send" TextColor="#0076FF" FontSize="14" HorizontalOptions="Start" Clicked="Send_Clicked" BackgroundColor="Transparent" FontAttributes="Bold"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chatfer.Shared;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using SendBird;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Chatfer.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Chat : ContentPage
{
private string name { get; set; }
private string image { get; set; }
private int matchUID;
private int currentUserID;
public GroupChannel Channel;
private SendBirdClient.ChannelHandler ch;
private ObservableCollection<MessageObj> Messages = new ObservableCollection<MessageObj>();
//public ObservableCollection<MessageObj> Messages
//{
// get { return messages; }
// set
// {
// messages = value;
// OnPropertyChanged(nameof(Messages));
// }
//}
//public event PropertyChangedEventHandler PropertyChanged;
//public void OnPropertyChanged([CallerMemberName]string propertyName = null)
//{
// if (PropertyChanged != null)
// {
// PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// }
//}
public Chat() { }
public Chat(string name, string imageURL, int UID)
{
InitializeComponent();
this.name = name;
this.image = imageURL;
this.matchUID = UID;
this.currentUserID = API.userUID;
msgList.ItemsSource = Messages;
List<string> userIDs = new List<string>();
ch = new SendBirdClient.ChannelHandler();
userIDs.Add(this.matchUID.ToString());
userIDs.Add(API.userUID.ToString());
GroupChannel.CreateChannelWithUserIds(userIDs, true, (GroupChannel groupChannel, SendBirdException err) => {
if (err != null)
{
// Error.
return;
}
Channel = groupChannel;
});
ch.OnMessageReceived = (BaseChannel baseChannel, BaseMessage baseMessage) => {
//Debug.WriteLine("MSG Received");
//Debug.WriteLine(((UserMessage)baseMessage).Message);
Messages.Add(new MessageObj(((UserMessage)baseMessage).Message, TextAlignment.Start));
msgList.ScrollTo(Messages.Last(), ScrollToPosition.End, false);
};
SendBirdClient.AddChannelHandler(currentUserID.ToString() + matchUID.ToString(), ch);
}
public class MessageObj
{
public string Mess { get; set; }
public TextAlignment textAlignment { get; set; }
public MessageObj(string Mess, TextAlignment textAlignment)
{
this.Mess = Mess;
this.textAlignment = textAlignment;
}
}
// private string _mess;
// private TextAlignment _textAlignment;
// public string Mess
// {
// get { return _mess; }
// set
// {
// _mess = value;
// OnPropertyChanged("Mess");
// }
// }
// public TextAlignment Alignment
// {
// get { return _textAlignment; }
// set
// {
// _textAlignment = value;
// OnPropertyChanged("Alignment");
// }
// }
// public event PropertyChangedEventHandler PropertyChanged;
// public void OnPropertyChanged([CallerMemberName]string propertyName = null)
// {
// if (PropertyChanged != null)
// {
// PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// }
// }
//}
protected override void OnAppearing()
{
base.OnAppearing();
LoadChat(); //Function that adds Data to Messages Collection
}
protected override void OnDisappearing()
{
base.OnDisappearing();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment