-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField1Fragment.java
234 lines (179 loc) · 11.8 KB
/
Field1Fragment.java
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.example.padel;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputEditText;
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.ValueEventListener;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Field1Fragment extends Fragment {
DatabaseReference database;
FragmentManager fragmentManager;
private String stringDateSelected;
private Spinner possibleHours, possibleTypes;
private TextInputEditText player1, player2 , player3, player4;
ArrayList<String> items_reservations;
ArrayList<String> items_rating;
private String field; //identificativo passato dalla'avtivty main che mi servirà per prendermi il nodo del campo
public Field1Fragment() {
// Required empty public constructor
}
public static Field1Fragment newInstance(String param1, String param2) {
Field1Fragment fragment = new Field1Fragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager = getParentFragmentManager();
Bundle data = this.getArguments();
if(data != null){
field = data.getString("field");
System.out.println(field);
}else{
System.out.println("data null");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_field1, container, false);
CalendarView calendarView = (CalendarView) view.findViewById(R.id.calendarView);
Button button = (Button)view.findViewById(R.id.saveEventBtn);
Button button2 = (Button)view.findViewById(R.id.insertRating);
DatabaseReference database = FirebaseDatabase.getInstance("https://padel-5d8f6-default-rtdb.europe-west1.firebasedatabase.app").getReference("reservations/"+field);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
String[] aux_items = new String[]{"09 10", "10 11", "11 12", "12 13", "13 14", "14 15", "15 16", "16 17", "17 18", "18 19", "19 20", "20 21", "21 22", "22 23", "23 24"};
stringDateSelected = Integer.toString(dayOfMonth) + ":" + Integer.toString(month + 1) + ":" + Integer.toString(year);
System.out.println(stringDateSelected);
items_reservations = new ArrayList<String>(); //simensione massima
items_rating = new ArrayList<String>(); //simensione massima
int length = 15;
String prenotazione = stringDateSelected;
if(stringDateSelected != null){
String[] date_information = stringDateSelected.split(":");
//-1900 perchè ritorna a partire dal 1900
Date date_selected = new Date(Integer.parseInt(date_information[2])-1900,Integer.parseInt(date_information[1])-1,Integer.parseInt(date_information[0]),23,59);
//se selezionata data futura vedo posti disponibili
if (date_selected.after(new Date())) {
database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//faccio qiery al database per vedere orari disponibili per quel giorno
for (int i = 0; i < length; i++) {
if (snapshot.hasChild(prenotazione + "/" + aux_items[i])) {
//nothing
} else {
//System.out.println("Impossbile prenotare");
items_reservations.add(aux_items[i]);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}else{ //nel caso sto cercando di valutare partita prendo solo partite possibili
database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//faccio qiery al database per vedere orari disponibili per quel giorno
for (int i = 0; i < length; i++) {
if (snapshot.hasChild(prenotazione + "/" + aux_items[i])) {
items_rating.add(aux_items[i]);
} else {
//System.out.println("Impossbile prenotare");
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (stringDateSelected != null) {
String[] date_information = stringDateSelected.split(":");
//-1900 perchè ritorna a partire dal 1900
Date date_selected = new Date(Integer.parseInt(date_information[2]) - 1900, Integer.parseInt(date_information[1]) - 1, Integer.parseInt(date_information[0]), 23,59);
if (date_selected.before(new Date())) {
Toast.makeText(getContext(), "Data selezionata errata", Toast.LENGTH_LONG).show();
} else {
Bundle data = new Bundle();
data.putStringArrayList("items", items_reservations);
data.putString("stringDateSelected", stringDateSelected);
data.putString("field", field);
ReservationFragment fragment = new ReservationFragment();
fragment.setArguments(data);
fragmentManager.beginTransaction().replace(R.id.fragmentContainerView, fragment, null) //vogliamo indicare di spaostarci da questo fragment ad un altro, cambia quello che è in questo containetr con un nuovo fragment
.setReorderingAllowed(true) //reordering allowed
.addToBackStack("name") // così che se faccio indietro ritorna a questo fragment
.commit();
}
}else{
Toast.makeText(getContext(), "Seleziona una data", Toast.LENGTH_LONG).show();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
Bundle data = new Bundle();
data.putStringArrayList("items",items);
data.putString("stringDateSelected",stringDateSelected);
data.putString("field",field);
*/
if(stringDateSelected != null) {
String[] date_information = stringDateSelected.split(":");
//-1900 perchè ritorna a partire dal 1900
Date date_selected = new Date(Integer.parseInt(date_information[2]) - 1900, Integer.parseInt(date_information[1]) - 1, Integer.parseInt(date_information[0]));
if (date_selected.after(new Date())) {
Toast.makeText(getContext(), "Non puoi valutare una partita futura", Toast.LENGTH_LONG).show();
} else {
//fragment.setArguments(data);
Bundle data = new Bundle();
data.putStringArrayList("items", items_rating); //ho preso solo le partite possibili e gliele passos
data.putString("stringDateSelected", stringDateSelected);
data.putString("field", field);
MatchRating fragment = new MatchRating();
fragment.setArguments(data);
fragmentManager.beginTransaction().replace(R.id.fragmentContainerView, fragment, null) //vogliamo indicare di spaostarci da questo fragment ad un altro, cambia quello che è in questo containetr con un nuovo fragment
.setReorderingAllowed(true) //reordering allowed
.addToBackStack("name") // così che se faccio indietro ritorna a questo fragment
.commit();
}
}else{
Toast.makeText(getContext(), "Seleziona una data", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}