protected async override void OnResume()
        {
            try
            {
                base.OnResume();
                if (!ListActivity.initialized)
                {
                    return;
                }

                string responseString = await c.MakeRequest("action=loadmessagelist&ID=" + Session.ID + "&SessionID=" + Session.SessionID);

                if (responseString.Substring(0, 2) == "OK")
                {
                    responseString = responseString.Substring(3);
                    if (responseString != "")
                    {
                        NoMatch.Visibility = ViewStates.Gone;
                        ServerParser <MatchItem> parser = new ServerParser <MatchItem>(responseString);
                        matchList = parser.returnCollection;
                        adapter   = new ChatUserListAdapter(this, matchList);
                        ImageCache.imagesInProgress     = new List <string>();
                        ImageCache.imageViewToLoadLater = new Dictionary <ImageView, string>();
                        ChatUserList.Adapter            = adapter;
                        NoofMatches.Text = (matchList.Count == 1) ? "1 " + res.GetString(Resource.String.ChatListMatch) : matchList.Count + " " + res.GetString(Resource.String.ChatListMatches);
                    }
                    else
                    {
                        matchList            = new List <MatchItem>();
                        adapter              = new ChatUserListAdapter(this, matchList);
                        ChatUserList.Adapter = adapter;
                        NoMatch.Visibility   = ViewStates.Visible;
                        NoofMatches.Text     = "";
                    }
                }
                else
                {
                    c.ReportError(responseString);
                }
            }
            catch (Exception ex)
            {
                c.ReportErrorSilent(ex.Message + System.Environment.NewLine + ex.StackTrace);
            }
        }
        public override async void ViewWillAppear(bool animated)
        {
            try
            {
                base.ViewWillAppear(animated);

                string responseString = await c.MakeRequest("action=loadmessagelist&ID=" + Session.ID + "&SessionID=" + Session.SessionID);

                if (responseString.Substring(0, 2) == "OK")
                {
                    responseString = responseString.Substring(3);
                    if (responseString != "")
                    {
                        NoMatch.Hidden = true;
                        ServerParser <MatchItem> parser = new ServerParser <MatchItem>(responseString);
                        matchList           = parser.returnCollection;
                        adapter             = new ChatUserListAdapter(matchList);
                        ChatUserList.Source = adapter;
                        NoofMatches.Text    = (matchList.Count == 1) ? "1 " + LangEnglish.ChatListMatch : matchList.Count + " " + LangEnglish.ChatListMatches;
                    }
                    else
                    {
                        matchList           = new List <MatchItem>();
                        adapter             = new ChatUserListAdapter(matchList);
                        ChatUserList.Source = adapter;
                        NoMatch.Hidden      = false;
                        NoofMatches.Text    = "";
                    }
                    ChatUserList.ReloadData();
                }
                else
                {
                    c.ReportError(responseString);
                }

                ChatUserList.RowHeight = 101;  //If the delegate is set before setting the Source, row height is gotten from the adapter allright. Otherwise the first row is about 20, and the subsequent rows are about 70.
                ChatUserList.Delegate  = this; //must be called after setting the Source, otherwise the Scrolled event is not fired.
            }
            catch (Exception ex)
            {
                c.ReportErrorSilent(ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }
Esempio n. 3
0
        public override void OnReceive(Context context, Intent intent)
        {
            int    sep1Pos;
            int    sep2Pos;
            int    sep3Pos;
            int    sep4Pos;
            int    matchID;
            string senderName;
            string text;

            this.context = context;
            int    senderID = int.Parse(intent.GetStringExtra("fromuser"));
            int    targetID = int.Parse(intent.GetStringExtra("touser"));
            string type     = intent.GetStringExtra("type");
            string meta     = intent.GetStringExtra("meta");
            bool   inApp    = intent.GetBooleanExtra("inapp", false);

            ((BaseActivity)context).c.Log("ChatReceiver senderID " + senderID + " type " + type + " meta " + meta + " inApp " + inApp);

            if (targetID != Session.ID)
            {
                return;
            }

            try
            {
                switch (type)
                {
                case "sendMessage":
                    string title = intent.GetStringExtra("title");
                    string body  = intent.GetStringExtra("body");

                    if (context is ChatOneActivity)
                    {
                        long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

                        //we need to update the Read time locally for display purposes before
                        sep1Pos = meta.IndexOf('|');
                        sep2Pos = meta.IndexOf('|', sep1Pos + 1);
                        sep3Pos = meta.IndexOf('|', sep2Pos + 1);

                        int  messageID = int.Parse(meta.Substring(0, sep1Pos));
                        long sentTime  = long.Parse(meta.Substring(sep2Pos + 1, sep3Pos - sep2Pos - 1));
                        long seenTime  = unixTimestamp;
                        long readTime  = unixTimestamp;

                        meta = messageID + "|" + senderID + "|" + sentTime + "|" + seenTime + "|" + readTime + "|";

                        if (senderID != Session.ID && senderID == Session.CurrentMatch.TargetID)                                 //for tests, you can use 2 accounts from the same device, and a sent message would appear duplicate.
                        {
                            ((ChatOneActivity)context).AddMessageItemOne(meta + body);
                            ((ChatOneActivity)context).c.MakeRequest("action=messagedelivered&ID=" + Session.ID + "&SessionID=" + Session.SessionID + "&MatchID=" + Session.CurrentMatch.MatchID + "&MessageID=" + messageID + "&Status=Read");
                        }
                        else if (inApp && senderID != Session.ID)
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChatNoOpen(senderID); }));
                        }
                    }
                    else
                    {
                        if (inApp)
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChat(senderID); }));
                        }

                        //update message list
                        if (context is ChatListActivity)
                        {
                            ((ChatListActivity)context).InsertMessage(meta, body);
                        }
                    }
                    break;

                case "messageDelivered":
                case "loadMessages":
                case "loadMessageList":
                    if (context is ChatOneActivity && senderID == Session.CurrentMatch.TargetID)
                    {
                        string[] updateItems = meta.Substring(1, meta.Length - 2).Split("}{");
                        foreach (string item in updateItems)
                        {
                            ((ChatOneActivity)context).UpdateMessageItem(item);
                        }
                    }
                    break;

                case "matchProfile":
                    if (inApp)                             //it is impossible to stand in that chat if wasn't previously a match
                    {
                        title = intent.GetStringExtra("title");
                        if (context is ChatOneActivity)
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChatNoOpen(senderID); }));
                        }
                        else
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChat(senderID); }));
                        }
                    }

                    if (context is ChatListActivity)
                    {
                        string matchItem = meta;
                        ServerParser <MatchItem> parser = new ServerParser <MatchItem>(matchItem);
                        ((ChatListActivity)context).AddMatchItem(parser.returnCollection[0]);
                    }

                    AddUpdateMatch(senderID, true);
                    if (context is ProfileViewActivity)
                    {
                        ((ProfileViewActivity)context).UpdateStatus(senderID, true);
                    }
                    break;

                case "rematchProfile":
                    sep1Pos = meta.IndexOf('|');

                    matchID = int.Parse(meta.Substring(0, sep1Pos));
                    bool active = bool.Parse(meta.Substring(sep1Pos + 1));

                    if (inApp)
                    {
                        title = intent.GetStringExtra("title");
                        if (context is ChatOneActivity && Session.CurrentMatch.TargetID == senderID)
                        {
                            ((BaseActivity)context).c.SnackStr(title);
                        }
                        else if (context is ChatOneActivity)
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChatNoOpen(senderID); }));
                        }
                        else
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChat(senderID); }));
                        }
                    }

                    AddUpdateMatch(senderID, true);
                    if (context is ChatListActivity)
                    {
                        ((ChatListActivity)context).UpdateMatchItem(matchID, active, null);
                    }
                    else if (context is ChatOneActivity)
                    {
                        ((ChatOneActivity)context).UpdateStatus(senderID, active, null);
                    }
                    else if (context is ProfileViewActivity)
                    {
                        ((ProfileViewActivity)context).UpdateStatus(senderID, true);
                    }

                    break;

                case "unmatchProfile":
                    sep1Pos = meta.IndexOf('|');

                    matchID = int.Parse(meta.Substring(0, sep1Pos));
                    long unmatchDate = long.Parse(meta.Substring(sep1Pos + 1));

                    if (((BaseActivity)context).IsUpdatingFrom(senderID))
                    {
                        ((BaseActivity)context).RemoveUpdatesFrom(senderID);
                    }
                    if (((BaseActivity)context).IsUpdatingTo(senderID))
                    {
                        ((BaseActivity)context).RemoveUpdatesTo(senderID);
                    }

                    if (inApp)
                    {
                        title = intent.GetStringExtra("title");
                        if (context is ChatOneActivity && Session.CurrentMatch.TargetID == senderID)
                        {
                            ((BaseActivity)context).c.SnackStr(title);
                        }
                        else if (context is ChatOneActivity)
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChatNoOpen(senderID); }));
                        }
                        else
                        {
                            ((BaseActivity)context).c.SnackAction(title, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToChat(senderID); }));
                        }
                    }

                    AddUpdateMatch(senderID, false);
                    if (context is ChatListActivity)
                    {
                        ((ChatListActivity)context).UpdateMatchItem(matchID, false, unmatchDate);
                    }
                    else if (context is ChatOneActivity)
                    {
                        ((ChatOneActivity)context).UpdateStatus(senderID, false, unmatchDate);
                    }
                    else if (context is ProfileViewActivity)
                    {
                        ((ProfileViewActivity)context).UpdateStatus(senderID, false);
                    }

                    break;

                case "locationUpdate":
                    sep1Pos = meta.IndexOf('|');
                    sep2Pos = meta.IndexOf('|', sep1Pos + 1);

                    senderName = meta.Substring(0, sep1Pos);
                    int frequency = int.Parse(meta.Substring(sep1Pos + 1, sep2Pos - sep1Pos - 1));

                    if (!((BaseActivity)context).IsUpdatingFrom(senderID))
                    {
                        ((BaseActivity)context).AddUpdatesFrom(senderID);

                        text = senderName + " " + context.Resources.GetString(Resource.String.LocationUpdatesFromStart) + " " + frequency + " s.";
                        if (context is ProfileViewActivity)
                        {
                            ((ProfileViewActivity)context).UpdateLocationStart(senderID, text);
                        }
                        else
                        {
                            ((BaseActivity)context).c.SnackAction(text, Resource.String.ShowReceived, new Action <View>(delegate(View obj) { GoToProfile(senderID); }));
                        }
                    }

                    sep3Pos = meta.IndexOf('|', sep2Pos + 1);
                    sep4Pos = meta.IndexOf('|', sep3Pos + 1);

                    long   time      = long.Parse(meta.Substring(sep2Pos + 1, sep3Pos - sep2Pos - 1));
                    double latitude  = double.Parse(meta.Substring(sep3Pos + 1, sep4Pos - sep3Pos - 1), CultureInfo.InvariantCulture);
                    double longitude = double.Parse(meta.Substring(sep4Pos + 1), CultureInfo.InvariantCulture);

                    ((BaseActivity)context).AddLocationData(senderID, latitude, longitude, time);

                    if (!(ListActivity.listProfiles is null))
                    {
                        foreach (Profile user in ListActivity.listProfiles)
                        {
                            if (user.ID == senderID)
                            {
                                user.LastActiveDate = time;
                                user.Latitude       = latitude;
                                user.Longitude      = longitude;
                                user.LocationTime   = time;
                            }
                        }
                    }

                    if (context is ListActivity && (bool)Settings.IsMapView)
                    {
                        foreach (Marker marker in ListActivity.profileMarkers)
                        {
                            if (marker.Title == senderID.ToString())
                            {
                                marker.Position = new LatLng(latitude, longitude);
                            }
                        }
                    }
                    else if (context is ProfileViewActivity)
                    {
                        ((ProfileViewActivity)context).UpdateLocation(senderID, time, latitude, longitude);
                    }
                    break;

                case "locationUpdateEnd":
                    senderName = meta;

                    if (((BaseActivity)context).IsUpdatingFrom(senderID))                             //user could have gone to the background, clearing out the list of people to receive updates from.
                    {
                        ((BaseActivity)context).RemoveUpdatesFrom(senderID);

                        text = senderName + " " + context.Resources.GetString(Resource.String.LocationUpdatesFromEnd);
                        ((BaseActivity)context).c.SnackStr(text);
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                CommonMethods c = new CommonMethods(null);
                c.ReportErrorSilent(ex.Message + System.Environment.NewLine + ex.StackTrace + System.Environment.NewLine + " Error in ChatReceiver");
            }
        }