152 lines
5.0 KiB
C#
152 lines
5.0 KiB
C#
using System;
|
|
using Firebase.Database;
|
|
using Firebase.Extensions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace Firebase
|
|
{
|
|
public class FirebaseNoteManager : MonoBehaviour
|
|
{
|
|
public Transform contentPanel;
|
|
public GameObject notePrefab;
|
|
private DatabaseReference _reference;
|
|
|
|
private bool _noteAdding;
|
|
private bool _notesLoading;
|
|
|
|
private void Awake()
|
|
{
|
|
Debug.Log("[DCDC] Initializing [Firebase] Note Manager");
|
|
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
|
|
{
|
|
if (task.IsCompleted)
|
|
{
|
|
_reference = FirebaseDatabase.DefaultInstance.RootReference;
|
|
ListenToNotes();
|
|
}
|
|
else Debug.LogError("[DCDC] Firebase initialization failed.");
|
|
});
|
|
}
|
|
|
|
public void AddTestNote()
|
|
{
|
|
AddANewNote("[DCDC] Test note at " + DateTime.UtcNow.ToString("o"));
|
|
}
|
|
|
|
public void AddANewNote(string message)
|
|
{
|
|
if (_reference == null)
|
|
{
|
|
Debug.LogError("[DCDC] 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("[DCDC] Full path: " + _reference.Child("notes").Child(noteKey));
|
|
|
|
if (task.IsFaulted)
|
|
{
|
|
Debug.LogError("[DCDC] Write faulted: " + task.Exception);
|
|
}
|
|
else if (task.IsCanceled)
|
|
{
|
|
Debug.LogWarning("[DCDC] Write was canceled.");
|
|
}
|
|
else if (task.IsCompleted)
|
|
{
|
|
Debug.Log("[DCDC] 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<NoteModel>(json);
|
|
var noteGo = Instantiate(notePrefab, contentPanel);
|
|
noteGo.GetComponentInChildren<TextMeshProUGUI>().text = note.message;
|
|
}
|
|
}
|
|
}
|
|
} |