Skip to content

Commit

Permalink
Merge pull request #28 from virresh/sessions
Browse files Browse the repository at this point in the history
Sessions - The checkbox version
  • Loading branch information
gupta-meghna64 authored Nov 2, 2019
2 parents 76821ef + 657489e commit 4b0acfa
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:fullBackupContent="false"
android:icon="@drawable/app_logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@drawable/app_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.UserDashboard" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class proposalShowFragment extends Fragment {

public static final String TAG = "PROPOSAL-FRAGMENT";

public proposalShowFragment() {
// Required empty public constructor
}
Expand All @@ -44,16 +49,21 @@ public proposalShowFragment() {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final int[] numberOfChecks = {0};
final int[] durationTracker = {0};
final ArrayList<Date> checkedTimes = new ArrayList<Date>();
final ArrayList<Boolean> taken = new ArrayList<Boolean>();
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_proposal_show, container, false);
final Button b = (Button) v.findViewById(R.id.submitButton);
final Proposal mProposal = (Proposal) getArguments().getSerializable("PROPOSAL_INFO");

LinearLayout sessionsLayout= (LinearLayout) v.findViewById(R.id.sessionsList);
final int durationCap = mProposal.getDurationCap();

final LinearLayout sessionsLayout = (LinearLayout) v.findViewById(R.id.sessionsList);

TextView mProposal_skill = (TextView) v.findViewById(R.id.proposal_skill);
mProposal_skill.setText(mProposal.getSkill());
String mSkill = mProposal.getSkill();
mProposal_skill.setText(mSkill);

TextView mProposal_rating = (TextView) v.findViewById(R.id.proposal_rating);
mProposal_rating.setText("Instructor Rating: " + mProposal.getRating());
Expand All @@ -62,54 +72,165 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mProposal_description.setText("Category: " + mProposal.getCategory());
//mProposal_description.setText("Description: " + mProposal.getDescription());

final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
final SimpleDateFormat dateFormat = new SimpleDateFormat("E, HH:mm");
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(mProposal.getStartDate());

int quanta = 15;
int running_sum = 0;
while(running_sum<mProposal.getDuration()){
SessionLab sl = SessionLab.get(getContext());
List<Session> existingSessions = sl.getSessions();
ArrayList<Session> eSessions_mProposal = new ArrayList<Session>();
for (int i = 0; i < existingSessions.size(); i++) {
Session e = existingSessions.get(i);
if (e.getTeacher().equals(mProposal.getMentorName()) & e.getSkill().equals(mSkill)) {
eSessions_mProposal.add(e);
}
}

while (running_sum < mProposal.getDuration()) {
final Date start = startCalendar.getTime();
startCalendar.add(Calendar.MINUTE, quanta);
running_sum+=quanta;
running_sum += quanta;
Date end = startCalendar.getTime();

final CheckBox sessionCheckBox = new CheckBox(getContext());
sessionCheckBox.setText(dateFormat.format(start)+" - "+dateFormat.format(end));
sessionCheckBox.setText(dateFormat.format(start) + " - " + dateFormat.format(end));
sessionsLayout.addView(sessionCheckBox);
//if taken, then set enabled to false
taken.add(false);
for (int i = 0; i < eSessions_mProposal.size(); i++) {
Session e = eSessions_mProposal.get(i);
Date ssDate = e.getInteractionDate();
int ssDuration = e.getDuration();

Calendar cal = Calendar.getInstance();
cal.setTime(ssDate);

while (ssDuration>0){
if (start.equals(cal.getTime())) {
sessionCheckBox.setEnabled(false);
taken.set((taken.size() - 1), true);
durationTracker[0] += quanta;
}
cal.add(Calendar.MINUTE, 15);
ssDuration-=15;
}
}


sessionCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
numberOfChecks[0] +=1;
checkedTimes.add(start);
}
else if(!isChecked){
numberOfChecks[0] -=1;
if (!isChecked) {
numberOfChecks[0] -= 1;
durationTracker[0] -= 15;
checkedTimes.remove(start);

if (durationTracker[0] <= durationCap) {
//enable all others
for (int i = 0; i < sessionsLayout.getChildCount(); i++) {
View cbView = sessionsLayout.getChildAt(i);
if (cbView instanceof CheckBox) {
if (!(((CheckBox) cbView).isEnabled())) {
if (taken.get(i) == false) {
cbView.setEnabled(true);
}
}
}
}

}
} else if (isChecked) {
numberOfChecks[0] += 1;
durationTracker[0] += 15;

if (durationTracker[0] <= durationCap) checkedTimes.add(start);

if (durationTracker[0] >= durationCap) {
//if strictly greater, then uncheck this also and show dialogue
new SweetAlertDialog(getContext())
.setContentText("The mentor is not available for more than " + durationCap + " minutes")
.show();
if (durationTracker[0] > durationCap) {
numberOfChecks[0] -= 1;
durationTracker[0] -= 15;
sessionCheckBox.toggle();
new SweetAlertDialog(getContext())
.setTitleText("Oops :(")
.setContentText("The mentor is not available for more than " + durationCap + " minutes")
.show();

}
//disable all others
for (int i = 0; i < sessionsLayout.getChildCount(); i++) {
View cbView = sessionsLayout.getChildAt(i);
if (cbView instanceof CheckBox) {
if (!(((CheckBox) cbView).isChecked())) {
cbView.setEnabled(false);
}
}
}


}

}
if(numberOfChecks[0]<=0){
if (numberOfChecks[0] <= 0) {
b.setEnabled(false);
}
else if(numberOfChecks[0]>0){
} else if (numberOfChecks[0] > 0) {
b.setEnabled(true);
}

}
});

}


if (durationTracker[0] >= durationCap) {
for (int i = 0; i < sessionsLayout.getChildCount(); i++) {
View cbView = sessionsLayout.getChildAt(i);
if (cbView instanceof CheckBox) {
cbView.setEnabled(false);

}
}
}


b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SessionLab sl = SessionLab.get(view.getContext());

for(int i=0; i<checkedTimes.size(); i++){
Session newSession = new Session(sl.getSessions().size(), mProposal);
newSession.setInteractionDate(checkedTimes.get(i));
sl.AddSession(newSession);
Collections.sort(checkedTimes);

Date ssDate = checkedTimes.get(0);
checkedTimes.remove(ssDate);
Session newSession = newSession = new Session(sl.getSessions().size(), mProposal);
newSession.setInteractionDate(ssDate);
int ssDuration = 15;
while(checkedTimes.size()>0){
long difference = TimeUnit.MILLISECONDS.toMinutes(checkedTimes.get(0).getTime() - ssDate.getTime());

ssDate = checkedTimes.get(0);
checkedTimes.remove(ssDate);
if(difference>15){
newSession.setDuration(ssDuration);
sl.AddSession(newSession);

newSession = new Session(sl.getSessions().size(), mProposal);
newSession.setInteractionDate(ssDate);
ssDuration = 15;

}
else {
ssDuration+=15;
}

}
newSession.setDuration(ssDuration);
sl.AddSession(newSession);

//Toast.makeText(view.getContext(), "Session for " + newSession.getSkill() + " requested successfully!", Toast.LENGTH_SHORT).show();
new SweetAlertDialog(getContext(), SweetAlertDialog.SUCCESS_TYPE)
.setTitleText("Session requested successfully!")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_proposal_show.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:layout_marginBottom="8dp"
Expand Down

0 comments on commit 4b0acfa

Please sign in to comment.