//Click handler method. This method in being called from CollectionCellViewController. private void ClickHandler(CollectionCellViewController sender, CustomEventCellArgs args) { var cell = sender as CollectionCellViewController; if (cell != null) { } var cellArgs = args as CustomEventCellArgs; if (cellArgs != null) { user_id = lst_employees[args.Position]; PerformSegue("EditSegue", this); } }
/// <summary> /// Method that initialize all the buttons contained in the cell. /// All the buttons work with anonymous functions or delegates. /// </summary> /// <param name="cell">Cell.</param> public void initilizeButton(CollectionCellViewController cell) { //Event that delete an employee. It prompts an alert as confirmation to proceed with the delete. cell.BtnDelete.TouchUpInside += delegate { try { var alert = UIAlertController.Create("Delete employee", "Are you sure you want to delete this employee?", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("Delete", UIAlertActionStyle.Destructive, delegate { //Looks for a user in the team_members node. DatabaseReference userNode_temp = userNode.GetChild(cell.Id); userNode_temp.RemoveValue(); //Sets the value to null. userNode_temp.SetValue <NSObject>(null); object[] nodes = { $"team_members/{cell.Id} " }; object[] nodesValues = { null }; var childUpdates = NSDictionary.FromObjectsAndKeys(nodesValues, nodes, nodes.Length); //Updates the child to delete the information from the firebase database. root.UpdateChildValues(childUpdates); InitializeFirebase(); })); alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null)); PresentViewController(alert, true, null); } catch (Exception ex) {} }; //Method to check if the user is online or offline. cell.BtnGoOnline.TouchUpInside += delegate { try { if (cell.Status == "Online") { //If the user is online then it looks for the child related to the user id. DatabaseReference onlineNode = tempNode.GetChild(cell.Id); //Executes a single event to try to get the value from that node. onlineNode.ObserveSingleEvent(DataEventType.Value, (snapshot) => { string fecha = DateTime.Now.ToString(); if (snapshot.ChildrenCount > 0) { //Gets the value of the response from firebase. var onlineUser = snapshot.GetValue <NSDictionary>(); //tt_values gets the values of the child node from onlineUser. var tt_values = onlineUser.Values; //Creates a node in timetracking with the following information /timetracking/{user.id}/{autogenkey}/ DatabaseReference tt_node = timetrackingNode.GetChild(cell.Id).GetChild(onlineUser.Keys[0].ToString()); //Create an object with the fields that the node must have. object[] timetracking_key = { "end_date", "start_date", "status", "uid_worker" }; //Gets the information from the temp_entrance. var start_date = tt_values[0].ValueForKey(new NSString("start_date")); var status = tt_values[0].ValueForKey(new NSString("status")); var uid_worker = tt_values[0].ValueForKey(new NSString("uid_worker")); //Adds the information from temp_entrance to the object timetracking_value. object[] timetracking_value = { fecha, start_date, 1, uid_worker }; //Create a dictonary containing the keys, values and the length of the object. var data = NSDictionary.FromObjectsAndKeys(timetracking_value, timetracking_key, timetracking_key.Length); //Adds the information to the timetracking node. tt_node.SetValue <NSDictionary>(data); //Deletes the information from the temp_entrance node. tempNode.GetChild(cell.Id).SetValue <NSDictionary>(null); int index = lst_employees.FindIndex(x => x.Id.Contains(cell.Id)); lst_employees[index].Status = "Offline"; CheckIfOnline(); } else { int index = lst_employees.FindIndex(x => x.Id.Contains(cell.Id)); lst_employees[index].Status = "Offline"; CheckIfOnline(); } }); } if (cell.Status == "Offline") { string fecha = DateTime.Now.ToString(); //Create a key object containing the user_id as key. object[] k = { cell.Id }; object[] vs = { null }; var da = NSDictionary.FromObjectsAndKeys(vs, k, k.Length); //Adds the child node to the temp_entrance table. tempNode.UpdateChildValues(da); //Obtained the information from the created node. DatabaseReference subNode = tempNode.GetChild(cell.Id); //Generated an autoid as child of the node created with the user id. DatabaseReference subNode2 = subNode.GetChildByAutoId(); //Created objects with the required fields, and information. object[] keys = { "end_date", "start_date", "status", "uid_worker" }; object[] values = { "", new NSString(fecha), 0, cell.Id }; //Created a dictionary with the value, keys and its length. var data = NSDictionary.FromObjectsAndKeys(values, keys, keys.Length); //Adds the value to the child node. subNode2.SetValue <NSDictionary>(data); int index = lst_employees.FindIndex(x => x.Id.Contains(cell.Id)); lst_employees[index].Status = "Online"; CheckIfOnline(); } } catch (Exception ex) { } }; }