PageRenderTime 110ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 1ms

/Assets/Scripts/Chat/Views/RoomView.cs

https://gitlab.com/nicolas.diazb97/chrome-score
C# | 354 lines | 279 code | 51 blank | 24 comment | 36 complexity | 1ccdb16ecf3e244228c10c69ccc543c4 MD5 | raw file
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. public class RoomView : MonoBehaviour,IChatView {
  8. public CardChat cardLocalPrefab;
  9. public CardChat cardRemotePrefab;
  10. public MyInputField inputF;
  11. public Button buttonSend;
  12. public Text title;
  13. public Button btnBlocks;
  14. public Button btnInvites;
  15. public Button btnEdit;
  16. public Button btnExit;
  17. public GameObject instructions;
  18. public TitleBtns titleBtns;
  19. List<CardChat> cards = new List<CardChat>();
  20. List<Member> remoteMembers = new List<Member>();
  21. Rooms thisRomm;
  22. DirectoryInfo folder;
  23. // Use this for initialization
  24. public void SetupView () {
  25. CancelInvoke();
  26. if( ! DataApp.main.AlreadyViewChatInstructions()){//new
  27. instructions.SetActive(true);//new
  28. }
  29. thisRomm = ChatController.main.actualRoom;
  30. CreateDirectoryToHistory();
  31. this.gameObject.SetActive(true);
  32. RegisterListeners();
  33. // CLEARView();
  34. SetTitleRoom();
  35. GetRemoteMembers();
  36. }
  37. public void ViewedInstructions(){//new
  38. DataApp.main.SetViewChatInstructions("1");//new
  39. }//new
  40. void RSTINSTRUCTIONS(){//new
  41. DataApp.main.DeleteVar("ViewChatInstructions");//new
  42. }//new
  43. void GetRemoteMembers(){
  44. Parameter p1 = new Parameter("ChatId",thisRomm.ChatId);
  45. List <Parameter> parameters = new List<Parameter>();
  46. parameters.Add(p1);
  47. ApiChat.main.StartCoroutine(ApiChat.main.GET<ChatsMembersList>( myChatsMembers => {
  48. if(myChatsMembers != null){
  49. foreach (ChatsMembers cm in myChatsMembers.list ) {
  50. remoteMembers.Add(cm.Members);
  51. }
  52. ShowPastMessages();
  53. InvokeRepeating ("ShowNewMessages",0f,2f);
  54. }
  55. } ,PCI.CHATS_MEMBERS,parameters ));
  56. }
  57. void CreateDirectoryToHistory(){
  58. folder = new DirectoryInfo(Application.persistentDataPath +"/"+thisRomm.ChatId);
  59. if(!folder.Exists){
  60. folder.Create();
  61. print(" LA CARPETA; "+folder.ToString());
  62. }
  63. }
  64. public void SetTitleRoom(){
  65. bool isMA = thisRomm.ChatId == ChatController.main.muroAmarillo.ChatId;
  66. title.text = (isMA) ? thisRomm.DisplayName + " Tiro Amarillo" : thisRomm.DisplayName;
  67. title.text = (isMA) ? thisRomm.DisplayName + " Tiro Amarillo" : thisRomm.DisplayName;
  68. //titleBtns.SetStates(!isMA,true,!isMA);
  69. titleBtns.SetStates(!isMA, true, true);//Eduardo
  70. btnEdit.gameObject.SetActive(!isMA);
  71. btnExit.gameObject.SetActive(!isMA);
  72. }
  73. void ShowNewMessages(){
  74. Parameter p1 = new Parameter("ChatId",thisRomm.ChatId);
  75. Parameter p2 = new Parameter("Receiver",ChatController.main.myUserIDchat);
  76. List <Parameter> parameters = new List<Parameter>();
  77. parameters.Add(p1);
  78. parameters.Add(p2);
  79. ApiChat.main.StartCoroutine(ApiChat.main.PUT<MessagesList>( msjs => {
  80. if(msjs != null){
  81. // CheckRemoteMembersAndThenPutMessage(msjs, true);
  82. PutMessages(msjs, true);
  83. }else{
  84. }
  85. } ,PCI.MESSAGES,parameters ));
  86. }
  87. void ShowPastMessages(){
  88. if(File.Exists(folder.ToString()+"/history.json")){
  89. string json = "{\"list\":["+File.ReadAllText( folder.ToString()+"/history.json" )+"]}";
  90. PutMessages( JsonUtility.FromJson<MessagesList>(json), false );
  91. }else{
  92. File.WriteAllText(folder.ToString() +"/history.json", "{}" );
  93. }
  94. }
  95. // void CheckRemoteMembersAndThenPutMessage(MessagesList msjs, bool isNew){
  96. void CheckRemoteMembersAndThenInstantiateCard(Messages _msj){
  97. CardChat cc = _msj.Sender == ChatController.main.myUserIDchat ? cardLocalPrefab :cardRemotePrefab;
  98. // string nameToShow = "";
  99. Member remoteMember = null;
  100. foreach (Member m in remoteMembers) {
  101. if(m.MemberId == _msj.Sender){
  102. remoteMember = m;
  103. // nameToShow = m.DisplayName;
  104. }
  105. }
  106. if(remoteMember == null){
  107. ApiChat.main.StartCoroutine(ApiChat.main.GETraw<MembersList>( users => {
  108. if(users == null){
  109. // nameToShow ="Unknown User";
  110. }else{
  111. remoteMember = users.list[0];
  112. remoteMembers.Add( remoteMember);
  113. // nameToShow = users.list[0].DisplayName;
  114. InstantiateCard(cc,_msj.Text, remoteMember);//GUARDAR
  115. }
  116. DataApp2.main.EnableLoading();
  117. // InternetManager.main.loading.SetActive(true);
  118. } ,PCI.MEMBERS,_msj.Sender.ToString() ));
  119. }else{
  120. InstantiateCard(cc,_msj.Text, remoteMember);//GUARDAR
  121. }
  122. }
  123. void PutMessages(MessagesList msjs, bool isNewMessages){
  124. foreach (Messages m in msjs.list) {
  125. CheckRemoteMembersAndThenInstantiateCard(m);
  126. if(isNewMessages){
  127. File.AppendAllText(folder.ToString() +"/history.json", CreateObjectRemotoAndTRansformToJson(m) );
  128. CheckSystemCommand(m);
  129. }
  130. }
  131. }
  132. //NUEVOS
  133. void CheckSystemCommand(Messages _m){
  134. if( _m.Sender == 1 || _m.Sender == 1004 ){
  135. string[] command = _m.Text.Split(',');
  136. if(command.Length > 1 ){
  137. if(command[0] == ChatController.main.myUserIDchat.ToString()){
  138. Notif(command[1]);
  139. }
  140. }else{
  141. Invoke(_m.Text,0);
  142. }
  143. }
  144. }
  145. void CLCH(){
  146. File.Delete(folder.ToString() +"/history.json");
  147. DisposeView();
  148. SetupView();
  149. }
  150. void Notif(string _m){
  151. ChatController.main.SetWarningMessage(_m);
  152. }
  153. // void PutMessages(MessagesList msjs, bool isNewMessages){
  154. // foreach (Messages m in msjs.list) {
  155. // CheckRemoteMembersAndThenInstantiateCard(m);
  156. // if(isNewMessages){
  157. // File.AppendAllText(folder.ToString() +"/history.json", CreateObjectRemotoAndTRansformToJson(m) );
  158. // }
  159. // }
  160. // }
  161. public void PutLocalMessageUI(){
  162. buttonSend.interactable = false;
  163. string tmpMsg = inputF.text;
  164. inputF.text = "";
  165. if(!string.IsNullOrEmpty(tmpMsg) ){
  166. PutLocalMessageToServer(tmpMsg);
  167. }else{
  168. buttonSend.interactable = true;
  169. }
  170. }
  171. void PutLocalMessageToServer(string _text){
  172. Parameter p1 = new Parameter("SenderMemberId",ChatController.main.myUserIDchat);
  173. Parameter p2 = new Parameter("ChatId",ChatController.main.actualRoom.ChatId);
  174. Parameter p3 = new Parameter("text",System.Uri.EscapeUriString(_text));
  175. List <Parameter> parameters = new List<Parameter>();
  176. parameters.Add(p1); parameters.Add(p2); parameters.Add(p3);
  177. ApiChat.main.StartCoroutine(ApiChat.main.POST( result => {
  178. if(result == "\"Sucessfully\""){
  179. InstantiateCard(cardLocalPrefab,_text,ChatController.main.me);
  180. File.AppendAllText(folder.ToString() +"/history.json", CreateObjectLocalAndTRansformToJson(_text) );
  181. inputF.text ="";
  182. buttonSend.interactable = true;
  183. }else{
  184. buttonSend.interactable = true;
  185. }
  186. } ,PCI.MESSAGES,parameters ));
  187. }
  188. void InstantiateCard(CardChat _c, string _msj, Member _member){
  189. // print("$$$$$$$$$ llega este name"+_name);
  190. if(!string.IsNullOrEmpty(_msj))
  191. {
  192. CardChat card = Instantiate(_c) ;
  193. card.transform.SetParent (_c.transform.parent, false);
  194. cards.Add(card);
  195. card.gameObject.SetActive(true);
  196. card.SetTextMessage(_msj,_member);
  197. // card.userName.text = _mem;//Eduardo afecta todos incluso los remotos con mi nombre de usuario
  198. }
  199. }
  200. string CreateObjectRemotoAndTRansformToJson(Messages _m){
  201. string result = "";
  202. MessagesCreator myMsj = new MessagesCreator(_m.Receivers, _m.Sender, _m.Text, _m.IsFile );
  203. result = JsonUtility.ToJson( myMsj);
  204. return ","+result;
  205. }
  206. string CreateObjectLocalAndTRansformToJson(string _m){
  207. string result = "";
  208. MessagesCreator myMsj = new MessagesCreator(new ChatsMembers(ChatController.main.actualRoom,ChatController.main.me) , ChatController.main.myUserIDchat , _m, false );
  209. result = JsonUtility.ToJson( myMsj);
  210. return ","+result;
  211. }
  212. string NameOfThisID(int _id){
  213. string result = "";
  214. foreach (Member m in remoteMembers) {
  215. if(m.MemberId == _id){
  216. result = m.DisplayName;
  217. }
  218. }
  219. if(string.IsNullOrEmpty(result)){
  220. ApiChat.main.StartCoroutine(ApiChat.main.GETraw<MembersList>( users => {
  221. if(users == null){
  222. result ="Unknown User";
  223. }else{
  224. remoteMembers.Add( users.list[0]);
  225. result = users.list[0].DisplayName;
  226. }
  227. // InternetManager.main.loading.SetActive(true);
  228. DataApp2.main.EnableLoading();
  229. } ,PCI.MEMBERS,_id.ToString() ));
  230. }
  231. return result;
  232. }
  233. void DeleteMeFromThisRoom(){
  234. Parameter p1 = new Parameter("ChatId",thisRomm.ChatId);
  235. Parameter p2 = new Parameter("MemberId",ChatController.main.myUserIDchat);
  236. List <Parameter> parameters = new List<Parameter>();
  237. parameters.Add(p1);
  238. parameters.Add(p2);
  239. ApiChat.main.StartCoroutine(ApiChat.main.DELETE( deletes => {
  240. if( deletes ){
  241. ChatController.main.ActiveView(VIEW_CHAT.LISTS);
  242. }else{
  243. ChatController.main.SetWarningMessage("No fue posible salir del grupo. (18)");
  244. }
  245. } ,PCI.CHATS_MEMBERS,parameters ));
  246. }
  247. public void DisposeView(){
  248. foreach (CardChat c in cards) {
  249. Destroy(c.gameObject,0.03f);
  250. }
  251. cards.Clear();
  252. this.gameObject.SetActive(false);
  253. buttonSend.onClick.RemoveAllListeners();
  254. btnBlocks.onClick.RemoveAllListeners();
  255. btnInvites.onClick.RemoveAllListeners();
  256. btnEdit.onClick.RemoveAllListeners();
  257. btnExit.onClick.RemoveAllListeners();
  258. titleBtns.addMemberBtn.onClick.RemoveAllListeners();
  259. titleBtns.backBtn.onClick.RemoveAllListeners();
  260. remoteMembers.Clear();
  261. CancelInvoke();
  262. }
  263. void RegisterListeners(){
  264. if(buttonSend.onClick.GetPersistentEventCount() <1){
  265. btnBlocks.onClick.AddListener(()=>{
  266. ChatController.main.ActiveView(VIEW_CHAT.BLOCKS);
  267. titleBtns.SettingsBtn.isOn = false;
  268. });
  269. btnInvites.onClick.AddListener(()=>{
  270. ChatController.main.ActiveView(VIEW_CHAT.INVITES);
  271. titleBtns.SettingsBtn.isOn = false;
  272. });
  273. btnEdit.onClick.AddListener(()=>{
  274. ChatController.main.ActiveView(VIEW_CHAT.EDIT);
  275. ChatController.main.SetLikeEdit(thisRomm );
  276. titleBtns.SettingsBtn.isOn = false;
  277. });
  278. btnExit.onClick.AddListener(()=>{
  279. ChatController.main.cp.gameObject.SetActive(true);
  280. ApiChat.main.StartCoroutine(ChatController.main.cp.AskToUser(result =>{
  281. if(result == "ok"){
  282. print ("ELIGIO OK");
  283. DeleteMeFromThisRoom();
  284. }else{
  285. print ("ELIGIO "+result);
  286. }
  287. ChatController.main.cp.gameObject.SetActive(false);
  288. },"¿Desea salir del grupo <b>"+thisRomm.DisplayName+"</b>?" ));
  289. });
  290. titleBtns.addMemberBtn.onClick.AddListener(()=>{
  291. // btnEdit.onClick.Invoke();
  292. ChatController.main.ActiveView(VIEW_CHAT.EDIT);
  293. // ChatController.main.ActiveView(VIEW_CHAT.MEMBERS);
  294. ChatController.main.SetLikeEdit(thisRomm );
  295. print("JUEPUTA AQUI DEBE SALIR");
  296. });
  297. titleBtns.backBtn.onClick.AddListener(()=>{
  298. ChatController.main.ActiveView(VIEW_CHAT.LISTS);
  299. });
  300. buttonSend.onClick.AddListener( PutLocalMessageUI);
  301. }
  302. }
  303. }