refactor: Converted message data to its own struct

This commit is contained in:
Gerard Gascón 2024-01-27 15:42:31 +01:00
parent 1fc809141c
commit 9dd340eeb7
10 changed files with 339 additions and 18 deletions

View file

@ -6,6 +6,8 @@ namespace Messaging.Composer {
public class InputField : MonoBehaviour {
[SerializeField] private TMP_Text fieldText;
public string Text => fieldText.text;
public void EmptyText() {
fieldText.SetText("");
}

View file

@ -0,0 +1,25 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Messaging.Composer {
public class SendButton : MonoBehaviour {
[SerializeField] private InputField inputField;
private MessageManager _messageManager;
private void Awake() {
_messageManager = FindObjectOfType<MessageManager>();
}
public void SendEmojis() {
if (CanSendText()) {
_messageManager.CreateMessage(inputField.Text, true);
}
}
private bool CanSendText() {
return !string.IsNullOrEmpty(inputField.Text) && !string.IsNullOrWhiteSpace(inputField.Text);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3f321cddc02443098ca224011b58aa9e
timeCreated: 1706364183

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 25fe495ec1e84081bb966070ed1d4e10
timeCreated: 1706365431

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace Messaging.MessageData {
public struct MessageData {
public KeyValuePair<string, string> QuestionMessage;
public KeyValuePair<string, string> AnswerMessage;
public MessageData(string text) {
string[] jokeParts = text.Split(':');
string[] question = jokeParts[0].Split('|');
string[] answer = jokeParts[1].Split('|');
QuestionMessage = new KeyValuePair<string, string>(question[0], question[1]);
AnswerMessage = new KeyValuePair<string, string>(answer[0], answer[1]);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a53c45e3eeca441d81be59b5e03893bc
timeCreated: 1706365538

View file

@ -1,12 +0,0 @@
using System.Collections.Generic;
namespace Messaging {
public static class MessageParser {
private const char MessageDifferentiator = '|';
public static KeyValuePair<string, string> SplitMessage(string message) {
string[] result = message.Split(MessageDifferentiator);
return new KeyValuePair<string, string>(result[0], result[1]);
}
}
}

View file

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: d7e3a5f1cf5d487eab9b2b5e232efeda
timeCreated: 1706346013

View file

@ -16,14 +16,14 @@ namespace Messaging {
}
private async void Start() {
string[] fLines = textAsset.text.Split(':');
MessageData.MessageData data = new(textAsset.text);
int i = 0;
while (Application.isPlaying) {
bool isAnswer = i % 2 != 0;
CreateMessage(MessageParser.SplitMessage(fLines[i % 2]).Key, isAnswer);
CreateMessage(data.QuestionMessage.Key, isAnswer);
await Task.Delay(2000);
CreateRealMessage(MessageParser.SplitMessage(fLines[i % 2]).Value, isAnswer);
CreateRealMessage(data.QuestionMessage.Value, isAnswer);
await Task.Delay(2000);
++i;
}