using System; using Firebase.Database; using Firebase.Extensions; using TMPro; using UnityEngine; using WebSocketSharp; namespace Firebase { public class FirebaseNoteManager : MonoBehaviour { public Transform contentPanel; public GameObject notePrefab; public GameObject addDialog; public GameObject addDialogBackdrop; private DatabaseReference _reference; private static string _inputText = ""; private bool _noteAdding; private bool _notesLoading; public void OpenAddNewNote() { _inputText = ""; addDialog.SetActive(true); addDialogBackdrop.SetActive(true); } public void CloseAddNewNote() { _inputText = ""; addDialog.SetActive(false); addDialogBackdrop.SetActive(false); } private void Awake() { FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { if (task.IsCompleted) { _reference = FirebaseDatabase.DefaultInstance.RootReference; ListenToNotes(); } else Debug.LogError("Firebase initialization failed."); }); } public void OnNewNoteTextChanged(string newText) { _inputText = newText; } public void SaveAddNewNote() { if (_inputText.IsNullOrEmpty()) return; AddANewNote(_inputText); CloseAddNewNote(); } private void AddANewNote(string message) { if (_reference == null) { Debug.LogError("Firebase reference is not initialized."); return; } var noteData = new NoteModel { id = Guid.NewGuid().ToString(), message = message, createdAt = DateTime.UtcNow.ToString("o") }; var noteKey = _reference.Child("notes").Push().Key; _noteAdding = true; var json = JsonUtility.ToJson(noteData); _reference.Child("notes").Child(noteKey).SetRawJsonValueAsync(json).ContinueWithOnMainThread(task => { Debug.Log("Full path: " + _reference.Child("notes").Child(noteKey)); if (task.IsFaulted) { Debug.LogError("Write faulted: " + task.Exception); } else if (task.IsCanceled) { Debug.LogWarning("Write was canceled."); } else if (task.IsCompleted) { Debug.Log("Saved note!"); } else { Debug.LogError(task.Exception); } }); // _reference.Child("notes").Child(noteKey).SetRawJsonValueAsync(JsonUtility.ToJson(noteData)) // .ContinueWith(task => // { // if (task.IsCompleted) // { // Debug.Log($"Note added with key: {noteKey}"); // } // else // { // Debug.LogError($"Failed to add note: {task.Exception}"); // } // // _noteAdding = false; // }); } // public void AddNoteReply(string noteKey, string reply) // { // if (_reference == null) // { // Debug.LogError("Firebase reference is not initialized."); // return; // } // // _noteAdding = true; // var replyData = new { content = reply, timestamp = DateTime.UtcNow.ToString("o") }; // _reference.Child("notes").Child(noteKey).Child("replies").Push() // .SetRawJsonValueAsync(JsonUtility.ToJson(replyData)) // .ContinueWith(task => // { // if (task.IsCompleted) // { // Debug.Log($"Reply added to note with key: {noteKey}"); // } // else // { // Debug.LogError($"Failed to add reply: {task.Exception}"); // } // // _noteAdding = false; // }); // } private void ListenToNotes() { FirebaseDatabase.DefaultInstance .GetReference("notes") .OrderByChild("createdAt") .ValueChanged += HandleNotesChanged; } private void HandleNotesChanged(object sender, ValueChangedEventArgs args) { if (args.DatabaseError != null) { Debug.LogError(args.DatabaseError.Message); return; } // Clear existing children foreach (Transform child in contentPanel) { Destroy(child.gameObject); } // Add new notes to ScrollView foreach (var noteSnapshot in args.Snapshot.Children) { var json = noteSnapshot.GetRawJsonValue(); var note = JsonUtility.FromJson(json); var noteGo = Instantiate(notePrefab, contentPanel); noteGo.GetComponentInChildren().text = note.message; } } } }