From c291a9be3673c5c6058d8af333d777955e4fa13b Mon Sep 17 00:00:00 2001 From: darshanxyz <darshanxyz@gmail.com> Date: Sun, 23 Apr 2017 23:10:24 +0530 Subject: [PATCH] Loads of improvements. Finally functional! --- .../odsystem/CustomListAdapter.java | 19 +- .../darshanbshah/odsystem/DataProvider.java | 50 ++++- .../darshanbshah/odsystem/MainActivity.java | 58 ++--- .../com/darshanbshah/odsystem/RollNumber.java | 10 +- .../odsystem/TeacherActivity.java | 199 ++++++++++++++---- app/src/main/res/layout/activity_teacher.xml | 31 ++- app/src/main/res/layout/list_item.xml | 34 +-- app/src/main/res/values/colors.xml | 2 +- 8 files changed, 264 insertions(+), 139 deletions(-) diff --git a/app/src/main/java/com/darshanbshah/odsystem/CustomListAdapter.java b/app/src/main/java/com/darshanbshah/odsystem/CustomListAdapter.java index 1e5e9d6..7e39b98 100644 --- a/app/src/main/java/com/darshanbshah/odsystem/CustomListAdapter.java +++ b/app/src/main/java/com/darshanbshah/odsystem/CustomListAdapter.java @@ -7,11 +7,14 @@ import android.content.Context; import android.graphics.Typeface; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import java.util.ArrayList; import java.util.List; @@ -20,7 +23,7 @@ public class CustomListAdapter extends ArrayAdapter { static class DataHandler { - TextView tv; + TextView tv, tv1; } List list = new ArrayList(); @@ -34,6 +37,12 @@ public class CustomListAdapter extends ArrayAdapter { list.add(object); } + @Override + public void remove(Object object) { + super.remove(object); + list.remove(object); + } + @Override public int getCount() { return this.list.size(); @@ -50,12 +59,13 @@ public class CustomListAdapter extends ArrayAdapter { public View getView(int position, View convertView, ViewGroup parent) { View row; row = convertView; - DataHandler handler; + final DataHandler handler; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.list_item, parent, false); handler = new DataHandler(); handler.tv = (TextView)row.findViewById(R.id.listText); + handler.tv1 = (TextView)row.findViewById(R.id.listTextEmail); row.setTag(handler); } else { @@ -64,9 +74,10 @@ public class CustomListAdapter extends ArrayAdapter { // Typeface one = Typeface.createFromAsset(parent.getContext().getAssets(), "fonts/BebasNeue Bold.ttf"); DataProvider provider = (DataProvider) this.getItem(position); - handler.tv.setText(provider.getDetector()); + handler.tv.setText(provider.getRollno()); + handler.tv1.setText(provider.getReason()); // handler.tv.setTypeface(one); - handler.tv.setTextSize(30); + handler.tv.setTextSize(20); return row; } } diff --git a/app/src/main/java/com/darshanbshah/odsystem/DataProvider.java b/app/src/main/java/com/darshanbshah/odsystem/DataProvider.java index 1385d50..0d3af5d 100644 --- a/app/src/main/java/com/darshanbshah/odsystem/DataProvider.java +++ b/app/src/main/java/com/darshanbshah/odsystem/DataProvider.java @@ -5,17 +5,53 @@ package com.darshanbshah.odsystem; */ public class DataProvider { - private String detector; + private String key, rollno, reason, from, to; - public DataProvider(String detector) { - this.setDetector(detector); + public DataProvider(String key, String rollno, String reason, String from, String to) { + this.rollno = rollno; + this.reason = reason; + this.from = from; + this.to = to; + this.key = key; } - public String getDetector() { - return detector; + public String getKey() { + return key; } - public void setDetector(String detector) { - this.detector = detector; + public void setKey(String key) { + this.key = key; + } + + public String getRollno() { + return rollno; + } + + public void setRollno(String rollno) { + this.rollno = rollno; + } + + public String getReason() { + return reason; + } + + public void setReason(String reason) { + this.reason = reason; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; } } \ No newline at end of file diff --git a/app/src/main/java/com/darshanbshah/odsystem/MainActivity.java b/app/src/main/java/com/darshanbshah/odsystem/MainActivity.java index edf73d4..ece9905 100644 --- a/app/src/main/java/com/darshanbshah/odsystem/MainActivity.java +++ b/app/src/main/java/com/darshanbshah/odsystem/MainActivity.java @@ -242,49 +242,21 @@ public class MainActivity extends AppCompatActivity { } final String str = adv_details.get(list1.get(0)); - od.addChildEventListener(new ChildEventListener() { - @Override - public void onChildAdded(DataSnapshot dataSnapshot, String s) { - for(DataSnapshot dsp : dataSnapshot.getChildren()){ - Log.e("DSP_KEY", dsp.getValue().toString()); - od_flag = od.child(str).child(mAuth.getCurrentUser().getUid()).child("flag"); - od_from = od.child(str).child(mAuth.getCurrentUser().getUid()).child("from"); - od_to = od.child(str).child(mAuth.getCurrentUser().getUid()).child("to"); - od_reason = od.child(str).child(mAuth.getCurrentUser().getUid()).child("reason"); - od_full_day = od.child(str).child(mAuth.getCurrentUser().getUid()).child("full day"); - - od_flag.setValue(1); - od_from.setValue(fromDate); - od_to.setValue(toDate); - od_reason.setValue(reasonString); - od_full_day.setValue(full); - } - reason.setText(""); - from.setText(""); - to.setText(""); - - } - - @Override - public void onChildChanged(DataSnapshot dataSnapshot, String s) { - - } - - @Override - public void onChildRemoved(DataSnapshot dataSnapshot) { - - } - - @Override - public void onChildMoved(DataSnapshot dataSnapshot, String s) { - - } - - @Override - public void onCancelled(DatabaseError databaseError) { - - } - }); + od_flag = od.child(str).child(mAuth.getCurrentUser().getUid()).child("flag"); + od_from = od.child(str).child(mAuth.getCurrentUser().getUid()).child("from"); + od_to = od.child(str).child(mAuth.getCurrentUser().getUid()).child("to"); + od_reason = od.child(str).child(mAuth.getCurrentUser().getUid()).child("reason"); + od_full_day = od.child(str).child(mAuth.getCurrentUser().getUid()).child("full day"); + + od_flag.setValue(1); + od_from.setValue(fromDate); + od_to.setValue(toDate); + od_reason.setValue(reasonString); + od_full_day.setValue(full); + + reason.setText(""); + from.setText(""); + to.setText(""); // recepient = list1.get(0); // diff --git a/app/src/main/java/com/darshanbshah/odsystem/RollNumber.java b/app/src/main/java/com/darshanbshah/odsystem/RollNumber.java index 6f399d4..ebcae5c 100644 --- a/app/src/main/java/com/darshanbshah/odsystem/RollNumber.java +++ b/app/src/main/java/com/darshanbshah/odsystem/RollNumber.java @@ -18,6 +18,7 @@ import android.widget.TextView; import android.widget.Toast; import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; @@ -54,10 +55,11 @@ public class RollNumber extends AppCompatActivity implements AdapterView.OnItemS @Override protected void onCreate(Bundle savedInstanceState) { + FirebaseUser user = mAuth.getCurrentUser(); root = database.getReference(); student = root.child("Student"); adv = root.child("Advisors"); - id = student.child(mAuth.getCurrentUser().getUid()); + id = student.child(user.getUid()); roll_no = id.child("RollNumber"); email = id.child("Email"); advisor = id.child("Advisor"); @@ -92,14 +94,11 @@ public class RollNumber extends AppCompatActivity implements AdapterView.OnItemS advisors.setOnItemSelectedListener(this); adv.addChildEventListener(new ChildEventListener() { - int i = 0; @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { for(DataSnapshot dsp : dataSnapshot.getChildren()){ lst.add(String.valueOf(dsp.getValue())); Log.e("EMAILS: ", String.valueOf(dsp.getValue())); -// adv_map.put(String.valueOf(dsp.getValue()), list.get(1)); -// ++i; } } @@ -123,7 +122,6 @@ public class RollNumber extends AppCompatActivity implements AdapterView.OnItemS } }); - Log.e("LSTSIZE", String.valueOf(lst.size())); // SharedPreferences preferences = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE); // @@ -141,7 +139,6 @@ public class RollNumber extends AppCompatActivity implements AdapterView.OnItemS } - @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { TextView textView = (TextView)view; @@ -166,6 +163,7 @@ public class RollNumber extends AppCompatActivity implements AdapterView.OnItemS Toast.makeText(this, "Enter Roll Number", Toast.LENGTH_SHORT).show(); } else { + Log.e("LST_SIZE", String.valueOf(lst.size())); for (String email : lst) { if (email.equals(mAuth.getCurrentUser().getEmail())) { startActivity(new Intent(this, TeacherActivity.class)); diff --git a/app/src/main/java/com/darshanbshah/odsystem/TeacherActivity.java b/app/src/main/java/com/darshanbshah/odsystem/TeacherActivity.java index 62f4511..cf3744d 100644 --- a/app/src/main/java/com/darshanbshah/odsystem/TeacherActivity.java +++ b/app/src/main/java/com/darshanbshah/odsystem/TeacherActivity.java @@ -1,12 +1,14 @@ package com.darshanbshah.odsystem; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; +import android.widget.AdapterView; import android.widget.ListView; -import android.widget.TextView; import android.widget.Toast; import com.google.firebase.auth.FirebaseAuth; @@ -15,31 +17,51 @@ 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.HashMap; +import java.util.Hashtable; import java.util.List; + public class TeacherActivity extends AppCompatActivity { + class ODTable { + String flag, reason, from, to, fullday; + + public ODTable(String flag, String reason, String from, String to, String fullday) { + this.flag = flag; + this.reason = reason; + this.from = from; + this.to = to; + this.fullday = fullday; + } + + } + ODTable table; + int i = 0; + AlertDialog.Builder builder; + DataProvider itemValue; FirebaseAuth mAuth; FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference root; - DatabaseReference od; DatabaseReference adv; DatabaseReference student; - + DatabaseReference od, od_flag, od_student; + String message = ""; List<String> uid_list = new ArrayList<String>(); HashMap<String, String> uid_map = new HashMap<String, String>(); HashMap<String, String> student_map = new HashMap<String, String>(); - + Hashtable<String, ODTable> odtable = new Hashtable<String, ODTable>(); ListView listView; - CustomListAdapter adapter; + String adv_name, key, value, uid = ""; + List<String> roll = new ArrayList<String>(); + List<String> random = new ArrayList<String>(); + List<DatabaseReference> od_student_list = new ArrayList<DatabaseReference>(); - String adv_name; + String flag = "", reason = "", from = "", to = "", fullday = ""; @Override protected void onCreate(Bundle savedInstanceState) { @@ -52,16 +74,19 @@ public class TeacherActivity extends AppCompatActivity { student = root.child("Student"); listView = (ListView)findViewById(R.id.listView); - adv.addChildEventListener(new ChildEventListener() { + student.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { for (DataSnapshot data : dataSnapshot.getChildren()) { - if (mAuth.getCurrentUser().getEmail().equals(data.getValue().toString())) { - Log.e("DATA_KEY", dataSnapshot.getKey()); - adv_name = dataSnapshot.getKey(); - + uid = dataSnapshot.getKey(); + if (data.getKey().toString().equals("RollNumber")) { + Log.e("MAPVAL", data.getValue().toString()); + roll.add(data.getValue().toString()); } } + for (int a = 0; a < roll.size(); a++) { + uid_map.put(uid, roll.get(a)); + } } @Override @@ -85,25 +110,14 @@ public class TeacherActivity extends AppCompatActivity { } }); - od.addChildEventListener(new ChildEventListener() { + adv.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { - if (dataSnapshot.getKey().equals(adv_name)) { - Log.e("SNAP", dataSnapshot.getKey()); - for (DataSnapshot data : dataSnapshot.getChildren()) { - Log.e("OD_LIST", data.getKey() + " " + data.getValue()); - uid_map.put(data.getKey(), data.getValue().toString()); - uid_list.add(data.getKey()); + for (DataSnapshot data : dataSnapshot.getChildren()) { + if (mAuth.getCurrentUser().getEmail().equals(data.getValue().toString())) { + adv_name = dataSnapshot.getKey(); } } - adapter = new CustomListAdapter(getApplicationContext(), R.layout.list_item); - listView.setAdapter(adapter); - - Log.e("UIDLIST_SIZE", String.valueOf(uid_list.size())); - for (String value: uid_list) { - DataProvider provider = new DataProvider(value); - adapter.add(provider); - } } @Override @@ -127,29 +141,65 @@ public class TeacherActivity extends AppCompatActivity { } }); - student.addChildEventListener(new ChildEventListener() { + od.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { -// for(DataSnapshot dsp : dataSnapshot.getChildren()){ -// if (uid_list.contains(dataSnapshot.getKey())) { -// if (dsp.getKey().toString().equals("RollNumber")) { + if (dataSnapshot.getKey().equals(adv_name)) { + Log.e("ADV_NAME", dataSnapshot.getKey()); + for (DataSnapshot data : dataSnapshot.getChildren()) { + Log.e("OD_LIST", data.getKey() + " " + data.getValue()); + uid_list.add(data.getKey()); + + } + } + for (DataSnapshot data : dataSnapshot.getChildren()) { + for (DataSnapshot d : data.getChildren()) { + Log.e("DATA_KEY", dataSnapshot.getKey()); + if (dataSnapshot.getKey().equals(adv_name)) + random.add(d.getValue().toString()); + } +// for (int i = 0; i < random.size(); i++) { +// Log.e("RANDOM_VALS", data.getKey() + ' ' + random.get(i)); // -// } // } -// } + if (random.size() != 0) { + try { + flag = random.get(0); + from = random.get(1); + fullday = random.get(2); + reason = random.get(3); + to = random.get(4); + table = new ODTable(flag, reason, from, to, fullday); + odtable.put(data.getKey(), table); + random.clear(); + } + catch (Exception e) { + + } - //Code yet to be decided + } -// for (int i = 0; i < uid_list.size(); i++) { -// for (DataSnapshot data : dataSnapshot.getChildren()) { -// if (dataSnapshot.getKey().equals(uid_list.get(i))) { -// if (data.getKey().equals("RollNumber")) { -// student_map.put(uid_list.get(i), data.getValue().toString()); -// } -// } -// } -// -// } + } + + adapter = new CustomListAdapter(getApplicationContext(), R.layout.list_item); + listView.setAdapter(adapter); + + Log.e("UIDLIST_SIZE", String.valueOf(uid_map.size())); + + for (int i = 0; i < uid_list.size(); i++) { + key = uid_list.get(i); + value = uid_map.get(key); + try { + if (odtable.get(key).flag.equals("1")) { + DataProvider provider = new DataProvider(key, value, odtable.get(key).reason, odtable.get(key).from, odtable.get(key).to); + adapter.add(provider); + } + } + catch (Exception e) { + + } + + } } @Override @@ -173,8 +223,69 @@ public class TeacherActivity extends AppCompatActivity { } }); + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { +// Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show(); + builder = new AlertDialog.Builder(TeacherActivity.this); + builder.setPositiveButton("Approve", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + od.addChildEventListener(new ChildEventListener() { + @Override + public void onChildAdded(DataSnapshot dataSnapshot, String s) { + DataProvider item_key = (DataProvider)adapter.getItem(position); + String item = item_key.getKey(); +// Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show(); + if (dataSnapshot.getKey().equals(adv_name)) { + for (DataSnapshot data : dataSnapshot.getChildren()) { + if (data.getKey().equals(item)) { + od_flag = od.child(adv_name).child(item).child("flag"); + od_flag.setValue(0); + od_student = od.child(adv_name).child(item); + od_student.removeValue(); + } + } + } + } + + @Override + public void onChildChanged(DataSnapshot dataSnapshot, String s) { + + } + + @Override + public void onChildRemoved(DataSnapshot dataSnapshot) { + + } + + @Override + public void onChildMoved(DataSnapshot dataSnapshot, String s) { + + } + + @Override + public void onCancelled(DatabaseError databaseError) { + + } + }); + } + }); + itemValue = (DataProvider) adapter.getItem(position); + builder.setMessage("Take Action".toUpperCase()); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + }); } + public void signOut(View view) { mAuth.signOut(); startActivity(new Intent(this, Login.class)); diff --git a/app/src/main/res/layout/activity_teacher.xml b/app/src/main/res/layout/activity_teacher.xml index 2212a00..9e00d13 100644 --- a/app/src/main/res/layout/activity_teacher.xml +++ b/app/src/main/res/layout/activity_teacher.xml @@ -13,26 +13,41 @@ android:id="@+id/button2" android:onClick="signOut" android:layout_alignParentTop="true" - android:layout_alignParentEnd="true" /> + android:layout_alignParentEnd="true" + style="@style/Widget.AppCompat.Button.Borderless.Colored" /> <ScrollView android:layout_width="match_parent" - android:layout_height="match_parent" + android:layout_height="wrap_content" android:layout_below="@+id/button2" - android:layout_alignParentStart="true"> + android:layout_alignParentStart="true" + android:layout_marginLeft="@dimen/activity_horizontal_margin" + android:layout_marginRight="@dimen/activity_horizontal_margin" + android:layout_marginTop="41dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> - <ListView - android:layout_width="match_parent" - android:layout_height="800dp" - android:id="@+id/listView"> + <ListView + android:layout_width="match_parent" + android:layout_height="500dp" + android:id="@+id/listView"> - </ListView> + </ListView> </LinearLayout> </ScrollView> + + <TextView + android:text="Pending ODs" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="12dp" + android:id="@+id/pendingTitle" + android:textAppearance="@style/TextAppearance.AppCompat.Display1" + android:layout_alignBottom="@+id/button2" + android:layout_alignParentStart="true" /> + </RelativeLayout> diff --git a/app/src/main/res/layout/list_item.xml b/app/src/main/res/layout/list_item.xml index 2fb8250..0342242 100644 --- a/app/src/main/res/layout/list_item.xml +++ b/app/src/main/res/layout/list_item.xml @@ -1,38 +1,20 @@ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:layout_marginTop="@dimen/activity_vertical_margin" + android:id="@+id/list_item"> <TextView android:id="@+id/listText" android:layout_width="match_parent" - android:layout_height="100dp" + android:layout_height="60dp" /> - <LinearLayout + <TextView + android:id="@+id/listTextEmail" android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:gravity="center" - android:layout_marginBottom="@dimen/activity_vertical_margin"> - - <Button - android:text="Approve" - android:layout_width="100dp" - android:layout_height="wrap_content" - android:id="@+id/approveButton" - android:background="@android:color/holo_green_light" - style="@style/Widget.AppCompat.Button.Colored" /> - - <Button - android:text="Deny" - android:layout_width="100dp" - android:layout_height="wrap_content" - android:id="@+id/denyButton" - android:background="@color/colorAccent" - android:layout_marginLeft="@dimen/activity_horizontal_margin" - style="@style/Widget.AppCompat.Button.Colored" /> - - </LinearLayout> + android:layout_height="40dp" + /> </LinearLayout> \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 4d44345..fbe1e87 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <resources> - <color name="colorPrimary">#3F51B5</color> + <color name="colorPrimary">#3f51b5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#ff4081</color> </resources> -- GitLab