public class DateActivity extends ExpandableListActivity {
// date, isAdmin and name are passed to this activity using shared preferences
private String date, name;
private boolean isAdmin;
// Adapter declaration
private CustomExpandableListAdapter mAdapter;
// ArrayList to store group items
private ArrayList<ParentHeader> parents = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
// initialize date, isAdmin, name here
// Fetch all shifts for selected date from Firebase
Validator.fetchShiftsPerDayFromDB(date, new Callback()
{
@Override
void shiftsCallback(ArrayList<Shift> shifts)
{
super.shiftsCallback(shifts);
if (shifts == null)
return;
for(Shift shift : shifts){
parents.add(new ParentHeader(shift));
}
mAdapter = new CustomExpandableListAdapter();
setListAdapter(mAdapter);
}
});
}
private class CustomExpandableListAdapter extends BaseExpandableListAdapter
{
private LayoutInflater inflater;
public CustomExpandableListAdapter()
{
// Create Layout Inflator
inflater = LayoutInflater.from(DateActivity.this);
}
@Override
public int getGroupCount()
{
return parents.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
int size = 0;
if(parents.get(groupPosition).getEmployeesList() != null)
size = parents.get(groupPosition).getEmployeesList().size();
return size;
}
@Override
public Object getGroup(int groupPosition)
{
return parents.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return parents.get(groupPosition).getEmployeesList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView)
{
final ParentHeader parentHeader = parents.get(groupPosition);
// Inflate group_row.xml file for group rows
convertView = inflater.inflate(R.layout.group_row, parentView, false);
// Get group_row.xml file elements and set values here
//Add User to selected shift
convertView.findViewById(R.id.add).setOnClickListener(v -> {
//TODO insert constraints
Validator.addUserToShiftInDB(parentHeader.getShift().getKey(), date, name, new Callback() {
@Override
void onUserAssignedToShiftCallback() {
super.onUserAssignedToShiftCallback();
// TODO attach as child-item to chosen group-item
}
});
});
convertView.findViewById(R.id.delete).setOnClickListener(v -> {
if(isAdmin){
//TODO open confirmation window
Validator.deleteShiftFromDateInDB(parentHeader.getShift().getKey(), date, new Callback() {
@Override
void onDeletedShiftCallback() {
super.onDeletedShiftCallback();
// shift gets deleted in Firebase but the expandable list does not refresh
parents.remove(groupPosition);
mAdapter.notifyDataSetChanged();
}
});
}
else{
Toast.makeText(getApplicationContext(),"Admin privileges required", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView)
{
final ParentHeader parentHeader = parents.get(groupPosition);
final String childString = parentHeader.getEmployeesList().get(childPosition);
// Inflate child_row.xml file for child rows
convertView = inflater.inflate(R.layout.child_row, parentView, false);
// Get child_row.xml file elements and set values here
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public boolean isEmpty()
{
return ((parents == null) || parents.isEmpty());
}
@Override
public void notifyDataSetChanged()
{
// Refresh List rows
super.notifyDataSetChanged();
}
}
}