Created
September 25, 2017 10:20
-
-
Save AswinpAshok/604358b5ff26186b724f679ecc132f96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sandftechnologies.schoolmanagementStaff.bottomSheet; | |
import android.app.Dialog; | |
import android.support.design.widget.BottomSheetDialogFragment; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.widget.DefaultItemAnimator; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.TextView; | |
import android.widget.ViewSwitcher; | |
import com.sandftechnologies.schoolmanagementStaff.DataCache; | |
import com.sandftechnologies.schoolmanagementStaff.GsonModels.Response; | |
import com.sandftechnologies.schoolmanagementStaff.GsonModels.StudentModel; | |
import com.sandftechnologies.schoolmanagementStaff.GsonModels.studentID; | |
import com.sandftechnologies.schoolmanagementStaff.Internet.GlobalVariables; | |
import com.sandftechnologies.schoolmanagementStaff.Internet.NetworkCall; | |
import com.sandftechnologies.schoolmanagementStaff.Internet.ServerResponseListner; | |
import com.sandftechnologies.schoolmanagementStaff.R; | |
import com.sandftechnologies.schoolmanagementStaff.RecyclerViewComponents.StudentSelectionRecyclerAdapter; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by PC4 on 8/31/2017. | |
*/ | |
public class StudentSelectionSheet extends BottomSheetDialogFragment implements ServerResponseListner,GlobalVariables,SelectedStudentChangedListener { | |
private List<StudentModel> studentList; | |
private List<String> selectedStudents; | |
private RecyclerView studentSelectionRecycler; | |
private TextView selectionCount; | |
private CheckBox selectAll; | |
private Button done; | |
private ViewSwitcher switcher; | |
private DataCache mDataCache; | |
private CoordinatorLayout baseLayout; | |
private String ClassID,SubjectID; | |
private Snackbar loadingStudents_snackbar; | |
private StudentSelectionRecyclerAdapter adapter; | |
private SelectionCompletedListener selectionDonelistener; | |
public void setIDs(String classID, String subjectID) { | |
this.ClassID = classID; | |
this.SubjectID = subjectID; | |
} | |
public void setClassID(String classID) { | |
this.ClassID = classID; | |
} | |
public void setSelectionCompletedListener(SelectionCompletedListener listener){ | |
this.selectionDonelistener=listener; | |
} | |
@Override | |
public void setupDialog(Dialog dialog, int style) { | |
super.setupDialog(dialog, style); | |
View contentView = View.inflate(getContext(), R.layout.student_selection, null); | |
dialog.setContentView(contentView); | |
mDataCache=DataCache.getInstance(); | |
selectedStudents=new ArrayList<>(); | |
switcher= (ViewSwitcher) contentView.findViewById(R.id.switcher); | |
baseLayout= (CoordinatorLayout) dialog.findViewById(R.id.baseLayout); | |
studentSelectionRecycler= (RecyclerView) contentView.findViewById(R.id.studentSelectionRecycler); | |
done= (Button) contentView.findViewById(R.id.done_button); | |
selectAll= (CheckBox) contentView.findViewById(R.id.checkboxSelectAll); | |
selectionCount= (TextView) contentView.findViewById(R.id.selectedStudentsCount); | |
switcher.setDisplayedChild(0); | |
loadingStudents_snackbar=Snackbar.make(baseLayout,"Loading students",Snackbar.LENGTH_INDEFINITE); | |
loadingStudents_snackbar.show(); | |
if(mDataCache.getStudentMap().get(ClassID)!=null) { | |
studentList=mDataCache.getStudentMap().get(ClassID); | |
NetworkCall networkCall = new NetworkCall(TYPE_GET_STUDENTS_OF_SUBJECT); | |
networkCall.setListener(StudentSelectionSheet.this); | |
networkCall.execute(createSubjectID_JSON(), GET_SUBJECT_DETAILS_URL); | |
}else { | |
NetworkCall networkCall = new NetworkCall(TYPE_GET_STUDENT_LIST); | |
networkCall.setListener(StudentSelectionSheet.this); | |
networkCall.execute(createClassID_JSON(), STUDENT_LIST_URL); | |
} | |
done.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
selectedStudents=adapter.getSelectedIDs(); | |
getDialog().dismiss(); | |
selectionDonelistener.onSelectoinCompleted(selectedStudents); | |
} | |
}); | |
selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if(selectAll.isChecked()){ | |
adapter.allStudentsSelected(); | |
}else { | |
if(adapter.getSelectedStudentsCount()==studentList.size()) { | |
adapter.allStudentsUnselected(); | |
} | |
} | |
} | |
}); | |
} | |
private String createClassID_JSON() { | |
String result=""; | |
try { | |
JSONObject object=new JSONObject(); | |
object.put("id",ClassID); | |
result=object.toString(); | |
}catch (JSONException e){ | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
private String createSubjectID_JSON() { | |
String result=""; | |
try { | |
JSONObject object=new JSONObject(); | |
object.put("id",SubjectID); | |
result=object.toString(); | |
}catch (JSONException e){ | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
@Override | |
public void onResponse(String type, Response response) throws Exception { | |
if(type.equals(TYPE_GET_STUDENTS_OF_SUBJECT)){ | |
loadingStudents_snackbar.dismiss(); | |
switcher.setDisplayedChild(1); | |
RecyclerView.ItemAnimator itemAnimator=new DefaultItemAnimator(); | |
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getActivity()); | |
List<studentID> studentIdList=response.getStudentListForSubjectResopnse(); | |
if(studentIdList.size()==1){ | |
if(studentIdList.get(0).getStudent_Id().equals("undefined")){ | |
//No students opted for this subject | |
studentSelectionRecycler.setLayoutManager( layoutManager ); | |
studentSelectionRecycler.setItemAnimator( itemAnimator ); | |
adapter=new StudentSelectionRecyclerAdapter(studentList); | |
adapter.setClassID(ClassID); | |
adapter.setSelectedStudentCountChangedListener(StudentSelectionSheet.this); | |
adapter.setSelectedStudents(new ArrayList<String>()); | |
studentSelectionRecycler.setHasFixedSize(true); | |
studentSelectionRecycler.setAdapter(adapter); | |
} | |
}else { | |
for (int i=0;i<studentIdList.size();i++){ | |
selectedStudents.add(studentIdList.get(i).getStudent_Id()); | |
} | |
studentSelectionRecycler.setLayoutManager( layoutManager ); | |
studentSelectionRecycler.setItemAnimator( itemAnimator ); | |
adapter=new StudentSelectionRecyclerAdapter(studentList); | |
adapter.setClassID(ClassID); | |
adapter.setSelectedStudentCountChangedListener(StudentSelectionSheet.this); | |
adapter.setSelectedStudents(selectedStudents); | |
studentSelectionRecycler.setHasFixedSize(true); | |
studentSelectionRecycler.setAdapter(adapter); | |
} | |
}else | |
if(type.equals(TYPE_GET_STUDENT_LIST)){ | |
studentList = response.getStudentList(); | |
mDataCache.addToStudentMap(ClassID,studentList); | |
NetworkCall networkCall = new NetworkCall(TYPE_GET_STUDENTS_OF_SUBJECT); | |
networkCall.setListener(StudentSelectionSheet.this); | |
networkCall.execute(createSubjectID_JSON(), GET_SUBJECT_DETAILS_URL); | |
} | |
} | |
@Override | |
public void onNetworkException(Exception e) { | |
loadingStudents_snackbar.dismiss(); | |
Snackbar snackbar=Snackbar.make(baseLayout,"No internet",Snackbar.LENGTH_SHORT); | |
snackbar.show(); | |
} | |
@Override | |
public void onSelectionCountChanged(int count) { | |
selectionCount.setText(String.valueOf(count)); | |
if(count==studentList.size()){ | |
selectAll.setChecked(true); | |
}else { | |
selectAll.setChecked(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment