-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
205 lines (180 loc) · 7.73 KB
/
MainActivity.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
package com.rusta.gpa3;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.rustam.gpa3.R;
import com.google.firebase.auth.FirebaseAuth;
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.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button btn_add;
Button btn_show;
private FirebaseAuth mAuth;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference databaseSubjects = FirebaseDatabase.getInstance().getReference("users").child(userID);
DatabaseReference database_one_subject;
List<Subject> subjectList;
private final int list_of_act = 1;
Subject subject;
TextView textView_gpa;
int credits;
double unitgpa;
int position1;
Toolbar toolbar;
@Override
protected void onStart() {
super.onStart();
databaseSubjects.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
subjectList.clear();
for (DataSnapshot subjectSnapShot : dataSnapshot.getChildren()) {
subject = subjectSnapShot.getValue(Subject.class);
subjectList.add(subject);
position1 = subjectList.size();
}
SubjectList adapter = new SubjectList(MainActivity.this, subjectList);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
btn_add = findViewById(R.id.btn_add);
btn_show = findViewById(R.id.btn_showgpa);
subjectList = new ArrayList<>();
textView_gpa = findViewById(R.id.textview_gpa);
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("GPA Calculator");
toolbar.setTitleTextColor(0xFFFFFFFF);
setSupportActionBar(toolbar);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AddActivity.class);
startActivity(intent);
}
});
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView_gpa.setText("" + calculateGPA());
}
});
//Dialog: Редактировать, удалить
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
subject = subjectList.get(position);
Log.d("Mytag ", String.valueOf(subject.getSubjectCredits() + " --- " + position1 + "; GPA: " + position));
showDialog(list_of_act);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
//Toolbar Menu "Log out"
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_logout:
FirebaseAuth.getInstance().signOut();
finish();
startActivity(new Intent(this, LoginActivity.class));
break;
}
return true;
}
public double calculateGPA() {
for (int position = 0; position < subjectList.size(); position++) {
Subject subject = subjectList.get(position);
unitgpa += subject.getCgpa()*subject.getSubjectCredits();
credits += subject.getSubjectCredits();
}
double gpa = Math.round((unitgpa/credits)*100.0)/100.0;
return gpa;
}
@Override
protected Dialog onCreateDialog(int id){
String[] acts = {"Edit", "Delete"};
switch (id){
case list_of_act:
AlertDialog.Builder builder = new AlertDialog.Builder(this); // ??
builder.setTitle("Choose option");
builder.setItems(acts, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent intent = new Intent(MainActivity.this, EditActivity.class);
intent.putExtra("name", subject.getSubjectName());
intent.putExtra("grade", String.valueOf(subject.getSpinnerGrade()));
intent.putExtra("credits", String.valueOf(subject.getSubjectCredits()));
intent.putExtra("key", subject.getSubjectKey());
startActivity(intent);
break;
case 1:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Do you want to delete a subject?").setCancelable(true).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteSubject();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
}
}
});
builder.setCancelable(true);
return builder.create();
default:
return null;
}
}
public void deleteSubject() {
database_one_subject = FirebaseDatabase.getInstance().getReference().child("users").child(userID).child(String.valueOf(subject.getSubjectKey()));
database_one_subject.setValue(null);
}
@Override
public void onBackPressed() {
return;
}
}