void OrganizedData(Java.Lang.Object value) { var snapshot = (QuerySnapshot)value; if (!snapshot.IsEmpty) { if (ListOfPost.Count > 0) { ListOfPost.Clear(); } foreach (DocumentSnapshot item in snapshot.Documents) { Post post = new Post(); post.ID = item.Id; post.PostBody = item.Get("post_body") != null?item.Get("post_body").ToString() : ""; post.Author = item.Get("author") != null?item.Get("author").ToString() : ""; post.ImageId = item.Get("image_id") != null?item.Get("image_id").ToString() : ""; post.OwnerId = item.Get("owner_id") != null?item.Get("owner_id").ToString() : ""; post.DownloadUrl = item.Get("download_url") != null?item.Get("download_url").ToString() : ""; string datestring = item.Get("post_date") != null?item.Get("post_date").ToString() : ""; post.PostDate = DateTime.ParseExact(datestring, @"dd/MM/yyyy HH:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture); var data = item.Get("likes") != null?item.Get("likes") : null; if (data != null) { var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister); //retrieve our own id string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid; //check the number of users liked the post post.LikeCount = dictionaryFromHashMap.Count; //verifies if we liked the post or not. if (dictionaryFromHashMap.Contains(uid)) { post.Liked = true; } } ListOfPost.Add(post); } OnPostRetrieved?.Invoke(this, new PostEventArgs { Posts = ListOfPost }); } }
void OrganizeData(Java.Lang.Object Value) { var snapshot = (QuerySnapshot)Value; if (!snapshot.IsEmpty) { if (ListOfPost.Count > 0) { ListOfPost.Clear(); } foreach (DocumentSnapshot item in snapshot.Documents) { Post post = new Post(); post.ID = item.Id; post.PostBody = item.Get("post_body") != null?item.Get("post_body").ToString() : ""; post.Author = item.Get("author") != null?item.Get("author").ToString() : ""; post.ImageId = item.Get("image_id") != null?item.Get("image_id").ToString() : ""; post.OwnerId = item.Get("owner_id") != null?item.Get("owner_id").ToString() : ""; post.DownloadUrl = item.Get("download_url") != null?item.Get("download_url").ToString() : ""; string datestring = item.Get("post_date") != null?item.Get("post_date").ToString() : ""; post.PostDate = DateTime.Parse(datestring); var data = item.Get("likes") != null?item.Get("likes") : null; if (data != null) { var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister); string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid; post.LikeCount = dictionaryFromHashMap.Count; if (dictionaryFromHashMap.Contains(uid)) { post.Liked = true; } } ListOfPost.Add(post); } OnPostRetrieved?.Invoke(this, new PostEventArgs { Posts = ListOfPost }); } }
public void OnSuccess(Java.Lang.Object result) { DocumentSnapshot snapshot = (DocumentSnapshot)result; if (!snapshot.Exists()) { return; } DocumentReference likeReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID); if (like) { likeReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true); } else { //check for null if (snapshot.Get("likes") == null) { return; } var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null; if (data != null) { var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister); //retrieve our own id string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid; //check if our user ID is contained inside the dictionary if (dictionaryFromHashMap.Contains(uid)) { //remove our user ID to unlike the post dictionaryFromHashMap.Remove(uid); //update the hashmap withour our userid included likeReference.Update("likes", dictionaryFromHashMap); } } } }
public void OnSuccess(Java.Lang.Object result) { DocumentSnapshot snapshot = (DocumentSnapshot)result; if (!snapshot.Exists()) { return; } DocumentReference likesReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID); if (Like) { likesReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true); } else { if (snapshot.Get("likes") == null) { return; } var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null; if (data != null) { var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister); string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid; if (dictionaryFromHashMap.Contains(uid)) { dictionaryFromHashMap.Remove(uid); likesReference.Update("likes", dictionaryFromHashMap); } } } }