-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminPageActivity.java
More file actions
197 lines (164 loc) · 7.04 KB
/
AdminPageActivity.java
File metadata and controls
197 lines (164 loc) · 7.04 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package pizza.olin.consamables;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ListView;
import android.widget.TextView;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import pizza.olin.consamables.data.FirebaseHandler;
import pizza.olin.consamables.types.GroupOrder;
import pizza.olin.consamables.types.HalfPizza;
import pizza.olin.consamables.types.OrderItem;
import pizza.olin.consamables.types.Topping;
public class AdminPageActivity extends AppCompatActivity {
private static final long UPDATE_TIMER_MS = 1000;
private FirebaseHandler handler;
private Optional<GroupOrder> maybeNewestOrder = Optional.empty();
private FloatingActionButton fab;
private TextView timeLeft;
private CountDownTimer countDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
handler = new FirebaseHandler();
timeLeft = (TextView) findViewById(R.id.time_left_order);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isActiveOrder = false;
// ^This variable is not used
if (maybeNewestOrder.isPresent()) {
if (maybeNewestOrder.get().isClosed) {
// cool
handler.createGroupOrder(30);
} else {
String uid = maybeNewestOrder.get().getUid();
handler.closeGroupOrder(uid);
}
} else {
handler.createGroupOrder(30);
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fetchFirebaseData();
}
private void updatePizzaButton(boolean isClosed) {
if (isClosed) {
// show the last order, have the ability to start a new order
fab.clearAnimation();
final LinearInterpolator interpolator = new LinearInterpolator();
ViewCompat.animate(fab)
.rotation(0f)
.withLayer()
.setDuration(300)
.setInterpolator(interpolator).start();
} else {
// spin the pizza around to indicate an active order
final LinearInterpolator interpolator = new LinearInterpolator();
RotateAnimation animation = new RotateAnimation(
0f,
360f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f
);
animation.setDuration(1000);
animation.setInterpolator(interpolator);
animation.setRepeatCount(Animation.INFINITE);
fab.startAnimation(animation);
}
}
private void updateListView(GroupOrder order) {
ListView listView = (ListView) findViewById(R.id.order_list);
ArrayList<OrderItem> orderItems = new ArrayList<>();
//TODO: get the actual list
listView.setAdapter(new OrderItemsAdapter(this, orderItems));
}
private void updateView() {
if (maybeNewestOrder.isPresent()) {
GroupOrder newestOrder = maybeNewestOrder.get();
updatePizzaButton(newestOrder.isClosed);
updateListView(newestOrder);
updateTimer();
}
}
private void updateTimer() {
if (maybeNewestOrder.isPresent()) {
GroupOrder newestOrder = maybeNewestOrder.get();
if (!newestOrder.isClosed) {
long timeLeftInOrder = (newestOrder.getStartTime().getTime() +
(newestOrder.getDurationMinutes() * 60 * 1000)) - new Date().getTime();
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(timeLeftInOrder, UPDATE_TIMER_MS) {
public void onTick(long millisUntilFinished) {
// To make your code more readable, do this:
long minutes = (millisUntilFinished / 1000) / 60;
long seconds = (millisUntilFinished / 1000) % 60;
timeLeft.setText(String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
}
public void onFinish() {
timeLeft.setText(R.string.order_finished);
}
}.start();
} else {
if (countDownTimer != null) {
countDownTimer.cancel();
}
// "Time's up" needs to be in strings.xml
timeLeft.setText("Time's up!");
}
}
}
private void fetchFirebaseData() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference ordersRef = database.getReference("orders");
Query query = ordersRef.limitToLast(1);
final AdminPageActivity thisActivity = this;
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<GroupOrder> groupOrders = new ArrayList<>();
for (DataSnapshot groupOrderSnapshot : dataSnapshot.getChildren()) {
String uid = groupOrderSnapshot.getKey();
GroupOrder groupOrder = groupOrderSnapshot.getValue(GroupOrder.class);
groupOrder.setUid(uid);
groupOrders.add(groupOrder);
}
Optional<GroupOrder> maybeNewestOrder = Stream.of(groupOrders)
.findFirst();
thisActivity.maybeNewestOrder = maybeNewestOrder;
thisActivity.updateView();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}