forked from eddydn/XamarinFirebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
149 lines (122 loc) · 4.94 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Views;
using XamarinFirebase.Model;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Firebase.Xamarin.Database;
using Firebase.Xamarin.Database.Query;
using Firebase.Xamarin.Auth;
namespace XamarinFirebase
{
[Activity(Label = "XamarinFirebase", MainLauncher = true, Icon = "@drawable/icon",Theme ="@style/AppTheme")]
public class MainActivity : AppCompatActivity
{
private EditText input_name, input_email;
private ListView list_data;
private ProgressBar circular_progress;
private List<Account> list_users = new List<Account>();
private ListViewAdapter adapter;
private Account selectedAccount;
private const string FirebaseURL = "https://xamarinfirebase-e2a82.firebaseio.com/";
protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
//Add toolbar
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "Firebase Demo";
SetSupportActionBar(toolbar);
//View
circular_progress = FindViewById<ProgressBar>(Resource.Id.circularProgress);
input_name = FindViewById<EditText>(Resource.Id.name);
input_email = FindViewById<EditText>(Resource.Id.email);
list_data = FindViewById<ListView>(Resource.Id.list_data);
list_data.ItemClick += (s, e) => {
Account acc = list_users[e.Position];
selectedAccount = acc;
input_name.Text = acc.name;
input_email.Text = acc.email;
};
await LoadData();
//var authProvider = new FirebaseAuthProvider(new FirebaseConfig(""));
//var auth = authProvider.CreateUserWithEmailAndPasswordAsync("eddydn@gmail.com", "1234");
//var resetPass = authProvider.SendPasswordResetEmailAsync("eddydn@gmail.com");
//var signIn = authProvider.SignInWithEmailAndPasswordAsync("eddydn@gmail.com", "1234");
//signIn.
}
private async Task LoadData()
{
circular_progress.Visibility = ViewStates.Visible;
list_data.Visibility = ViewStates.Invisible;
var firebase = new FirebaseClient(FirebaseURL);
var items = await firebase
.Child("users")
.OnceAsync<Account>();
list_users.Clear();
adapter = null;
foreach(var item in items)
{
Account acc = new Account();
acc.uid = item.Key;
acc.name = item.Object.name;
acc.email = item.Object.email;
list_users.Add(acc);
}
adapter = new ListViewAdapter(this, list_users);
adapter.NotifyDataSetChanged();
list_data.Adapter = adapter;
circular_progress.Visibility = ViewStates.Invisible;
list_data.Visibility = ViewStates.Visible;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if(id == Resource.Id.menu_add)
{
CreateUser();
}
else if (id == Resource.Id.menu_save) // Update
{
UpdateUser(selectedAccount.uid, input_name.Text, input_email.Text);
}
else if (id == Resource.Id.menu_remove)
{
DeleteUser(selectedAccount.uid);
}
return base.OnOptionsItemSelected(item);
}
private async void DeleteUser(string uid)
{
var firebase = new FirebaseClient(FirebaseURL);
await firebase.Child("users").Child(uid).DeleteAsync();
await LoadData();
}
private async void UpdateUser(string uid, string name, string email)
{
var firebase = new FirebaseClient(FirebaseURL);
await firebase.Child("users").Child(uid).Child("name").PutAsync(name);
await firebase.Child("users").Child(uid).Child("email").PutAsync(email);
await LoadData();
}
private async void CreateUser()
{
Account user = new Account();
user.uid = String.Empty;
user.name = input_name.Text;
user.email = input_email.Text;
var firebase = new FirebaseClient(FirebaseURL);
//Add item
var item = await firebase.Child("users").PostAsync<Account>(user);
await LoadData();
}
}
}