-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemsActivity.cs
160 lines (123 loc) · 4.88 KB
/
ItemsActivity.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
149
150
151
152
153
154
155
156
157
158
159
160
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using Java.Lang;
using List_App.Resources.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace List_App
{
[Activity(Label = "ItemsActivity")]
public class ItemsActivity : Activity
{
Button backButton;
TextView curListTextView;
EditText newItemList;
ListView itemListView;
Button shareButton;
GroceryListClass curList;
ItemRowListAdapter itemAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ItemsLayout); //Set Items view Here
InterfaceBuilder();
AppData.GetInstance();
int row = this.Intent.Extras.GetInt("row");
curList = AppData.curList[row];
curListTextView.Text = curList.Name;
itemAdapter = new ItemRowListAdapter(this, curList);
itemListView.Adapter = itemAdapter;
}
void InterfaceBuilder()
{
// set the view with Id
backButton = FindViewById(Resource.Id.backButton_id) as Button;
backButton.Click += GoBackAction;
curListTextView = FindViewById(Resource.Id.curListTextView_id) as TextView;
newItemList = FindViewById(Resource.Id.newItemEditText_id) as EditText;
newItemList.EditorAction += AddNewItem;
itemListView = FindViewById(Resource.Id.itemsListView_id) as ListView;
itemListView.ItemClick += ItemClicked;
itemListView.ItemLongClick += ItemLongClicked;
shareButton = FindViewById<Button>(Resource.Id.shareButton_id);
shareButton.Click += ShareAction;
}
void ShareAction(object sender, EventArgs e)
{
AlertDialog.Builder shareAlert = new AlertDialog.Builder(this);
shareAlert.SetTitle("Inviting Someone?");
shareAlert.SetMessage("Please enter the Email Address of the person you wish to invite to this list");
EditText input = new EditText(this);
input.TextSize = 22;
input.Gravity = GravityFlags.Center;
input.Hint = "Email Address";
input.SetSingleLine(true);
shareAlert.SetView(input);
shareAlert.SetPositiveButton("OK", async (shareSender, shareE) =>
{
await InviteSomone.Invite(this, curList, input.Text);
});
shareAlert.SetNegativeButton("Cancel", (senderAlert, args) => { });
Dialog dialog = shareAlert.Create();
dialog.Show();
}
void ItemLongClicked(object sender, AdapterView.ItemLongClickEventArgs e)
{
e.View.Animate()
.SetDuration(600)
.Alpha(0)
.WithEndAction(new Runnable(() =>
{
ItemClass toDelete = curList.Items[e.Position];
curList.Items.Remove(toDelete);
DeleteFromCloud.DeleteItem(toDelete,curList);
e.View.Alpha = 1;
itemAdapter.NotifyDataSetChanged();
ReadWrite.WriteData();
}));
}
void ItemClicked(object sender, AdapterView.ItemClickEventArgs e)
{
ItemClass thisItem = curList.Items[e.Position];
bool curStatus = false;
if (thisItem.Purchased == "true" || thisItem.Purchased == "True")
curStatus = true;
thisItem.Purchased = (!curStatus).ToString();
thisItem.Time = DateTime.UtcNow.ToString();
ReadWrite.WriteData();
itemAdapter.NotifyDataSetChanged();
}
void AddNewItem(object sender, TextView.EditorActionEventArgs e)
{
if (e.ActionId != ImeAction.Done)
return;
ItemClass newItem = new ItemClass
{
Name = newItemList.Text,
Purchased = false.ToString(),
Time = DateTime.UtcNow.ToString()
};
curList.Items.Add(newItem);
ReadWrite.WriteData();
SaveItemOnCloud.Save(newItem, curList); //Save the new list to the DataBase
newItemList.Text = "";
itemAdapter.NotifyDataSetChanged();
this.CurrentFocus.ClearFocus();
InputMethodManager inputManager =
(InputMethodManager)GetSystemService(Context.InputMethodService);
inputManager.HideSoftInputFromWindow(this.CurrentFocus.WindowToken,
HideSoftInputFlags.None);
}
void GoBackAction(object sender, EventArgs e)
{
Finish();
}
}
}