protected override void OnCreate(Bundle savedInstanceState) { // Well, just config with your own name app in manifest.xml // and yout own "google-services.json" file config from Firebase. that's all. base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); fireApp = Firebase.FirebaseApp.InitializeApp(this); fireFirestore = Firebase.Firestore.FirebaseFirestore.Instance; fireDatabase = Firebase.Database.FirebaseDatabase.Instance; Button btn1 = FindViewById(Resource.Id.Button1) as Button; btn1.Click += delegate { // So here it's crash the App. DocumentReference docRef = fireFirestore.Collection("collection1").Document("doc1"); docRef.Get() .AddOnCompleteListener(this) .AddOnFailureListener(this); }; }
void InitializeFirebase() { auth = Firebase.Auth.FirebaseAuth.DefaultInstance; db = FirebaseFirestore.DefaultInstance; user = auth.CurrentUser; if (user != null) { loadingPanel.SetActive(false); Debug.Log("FireBase Started Successfully"); SceneManager.LoadScene("Home"); } else { loadingPanel.SetActive(false); loginPanel.SetActive(false); loginSignUpPanel.SetActive(true); signUpPanel.SetActive(false); Debug.Log("FireBase Started Successfully"); } //auth.StateChanged += AuthStateChanged; //AuthStateChanged(this, null); }
async Task LoadCacheAsync() { Debug.LogFormat("User logged in:" + AccountManager.Instance.Logged); if (!AccountManager.Instance.Logged) { return; } Firebase.Firestore.FirebaseFirestore db = Firebase.Firestore.FirebaseFirestore.DefaultInstance; DocumentSnapshot user = await db.Collection("users").Document(AccountManager.Instance.GetUserId()).GetSnapshotAsync(); Debug.LogFormat("User found:" + user.Exists); if (!user.Exists) { return; } // User found loaded = true; Dictionary <string, object> data = user.ToDictionary(); if (!data.ContainsKey(CacheName)) { return; } Debug.LogFormat("Found save game:" + data[CacheName]); string[] splits = data[CacheName].ToString().Split(' '); speed = int.Parse(splits[0]); levelId = int.Parse(splits[1]); OnLoadedOrUpdated?.Invoke(); }
// Start is called before the first frame update void Start() { Debug.Log((Playerprefs.gameMode).ToString()); JoinRoomPanel.SetActive(false); app = Firebase.FirebaseApp.DefaultInstance; auth = Firebase.Auth.FirebaseAuth.DefaultInstance; db = FirebaseFirestore.DefaultInstance; user = auth.CurrentUser; if (Playerprefs.gameMode == 0) { MMPanel.SetActive(true); JCPanel.SetActive(false); startTimer = true; } else if (Playerprefs.gameMode == 1) { MMPanel.SetActive(false); JCPanel.SetActive(true); startTimer = false; joinButton.onClick.AddListener(delegate { setRoom(1); }); createButton.onClick.AddListener(delegate { setRoom(0); }); joinButton1.onClick.AddListener(setRoomCode); } }
public async Task <bool> SetLevelBeatenAsync(int levelId, int speed) { Debug.LogFormat("Saving progress - levelId:{0}, speed:{1}", levelId, speed); if (speed < this.speed) { return(true); } if (levelId < this.levelId) { return(true); } if (!AccountManager.Instance.Logged) { return(false); } Debug.LogFormat("Init firestore"); Firebase.Firestore.FirebaseFirestore db = Firebase.Firestore.FirebaseFirestore.DefaultInstance; DocumentSnapshot user = await db.Collection("users").Document(AccountManager.Instance.GetUserId()).GetSnapshotAsync(); Debug.LogFormat("User {0} found: {1}", AccountManager.Instance.GetUserId(), user.Exists); if (!user.Exists) { return(false); } int speedOld = this.speed; int levelIdOld = this.levelId; if (levelId < GameManager.Instance.GetNumberOfLevels()) { this.levelId++; } else { this.levelId = 1; this.speed++; } // User found //progress++; Dictionary <string, object> data = user.ToDictionary(); string value = string.Format("{0} {1}", this.speed, this.levelId); if (!data.ContainsKey(CacheName)) { data.Add(CacheName, value); } else { data[CacheName] = value; } bool ret = false; await user.Reference.SetAsync(data, SetOptions.MergeAll).ContinueWith(task => { if (task.IsFaulted || task.IsCanceled) { Debug.LogWarning("Task not completed; rolling bask progress..."); //progress--; this.levelId = levelIdOld; this.speed = speedOld; } else { Debug.Log("Task completed; progress saved..."); ret = true; } }); if (ret) { OnLoadedOrUpdated?.Invoke(); } Debug.Log("Returning " + ret); return(ret); }