Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Add certificate in Course Outline tab of Course tabs dashboard. #1046

Merged
merged 2 commits into from Jan 9, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -76,8 +76,8 @@ public interface DownloadListener {
private DownloadListener downloadListener;
private boolean isVideoMode;

public NewCourseOutlineAdapter(Context context, EnrolledCoursesResponse courseData,
IEdxEnvironment environment, NewCourseOutlineAdapter.DownloadListener listener,
public NewCourseOutlineAdapter(final Context context, final EnrolledCoursesResponse courseData,
final IEdxEnvironment environment, NewCourseOutlineAdapter.DownloadListener listener,
boolean isVideoMode, boolean isOnCourseOutline) {
this.context = context;
this.environment = environment;
Expand All @@ -91,6 +91,16 @@ public NewCourseOutlineAdapter(Context context, EnrolledCoursesResponse courseDa
adapterData = new ArrayList();
if (isOnCourseOutline && !isVideoMode) {
adapterData.add(new SectionRow(SectionRow.COURSE_CARD, null));
// Add certificate item
if (courseData.isCertificateEarned() && environment.getConfig().areCertificateLinksEnabled()) {
adapterData.add(new SectionRow(SectionRow.COURSE_CERTIFICATE, null,
new View.OnClickListener() {
@Override
public void onClick(View v) {
environment.getRouter().showCertificate(context, courseData);
}
}));
}
}
}

Expand Down Expand Up @@ -155,6 +165,11 @@ public final View getView(int position, View convertView, ViewGroup parent) {
}
return getCardView(convertView);
}
case SectionRow.COURSE_CERTIFICATE:
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_course_dashboard_cert, parent, false);
}
return getCertificateView(position, convertView);
case SectionRow.LAST_ACCESSED_ITEM: {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_last_accessed, parent, false);
Expand Down Expand Up @@ -207,19 +222,23 @@ public void setData(@Nullable CourseComponent component) {
* Clear all the course outline rows.
*/
private void clearCourseOutlineData() {
if (adapterData.isEmpty()) return;

int startIndex = 0;
// If data for course card is available on the first index, we don't want that cleared
if (adapterData.size() >= 1 && adapterData.get(0).type == SectionRow.COURSE_CARD) {
startIndex++;
if (adapterData.isEmpty()) {
return;
}
// If data for last accessed component is available on the second index, we don't want that cleared
if (adapterData.size() >= 2 && adapterData.get(1).type == SectionRow.LAST_ACCESSED_ITEM) {
startIndex++;
// Get index of first courseware row
int firstCoursewareRowIndex = -1;
int i = 0;
for (SectionRow sectionRow : adapterData) {
if (sectionRow.isCoursewareRow()) {
firstCoursewareRowIndex = i;
break;
}
i++;
}
if (firstCoursewareRowIndex >= 0) {
// Selectively clear adapter's data from a specific index onwards.
adapterData.subList(firstCoursewareRowIndex, adapterData.size()).clear();
}
// Selectively clear adapter's data from a specific index onwards.
adapterData.subList(startIndex, adapterData.size()).clear();
}

/**
Expand All @@ -228,19 +247,15 @@ private void clearCourseOutlineData() {
* @return <code>true</code> if there are course items, <code>false</code> otherwise.
*/
public boolean hasCourseData() {
if (adapterData.isEmpty()) return false;

int rowsToIgnore = 0;
// If data for course card is available on the first index, we need to skip it
if (adapterData.size() >= 1 && adapterData.get(0).type == SectionRow.COURSE_CARD) {
rowsToIgnore++;
if (adapterData.isEmpty()) {
return false;
}
// If data for last accessed component is available on the second index, we need to skip it
if (adapterData.size() >= 2 && adapterData.get(1).type == SectionRow.LAST_ACCESSED_ITEM) {
rowsToIgnore++;
for (SectionRow sectionRow : adapterData) {
if (sectionRow.isCoursewareRow()) {
return true;
}
}
// Means we have atleast 1 courseware item other than the course card and last accessed item
return adapterData.size() > rowsToIgnore;
return false;
}

public void reloadData() {
Expand Down Expand Up @@ -571,6 +586,12 @@ public View getCardView(View view) {
return view;
}

private View getCertificateView(int position, View convertView) {
final SectionRow sectionRow = getItem(position);
convertView.setOnClickListener(sectionRow.clickListener);
return convertView;
}

private View getLastAccessedView(int position, View convertView) {
final SectionRow sectionRow = getItem(position);
final TextView lastAccessTextView = (TextView) convertView.findViewById(R.id.last_accessed_text);
Expand All @@ -587,16 +608,62 @@ private View getLastAccessedView(int position, View convertView) {
* @param onClickListener The listener to invoke when the `view` button is pressed.
*/
public void addLastAccessedView(CourseComponent lastAccessedComponent, View.OnClickListener onClickListener) {
final int lastAccessedItemPlace = getNonCourseWareItemPlace(SectionRow.LAST_ACCESSED_ITEM);
// Update the last accessed item, if its already there in the list
if (adapterData.size() >= 2 && adapterData.get(1).type == SectionRow.LAST_ACCESSED_ITEM) {
adapterData.set(1, new SectionRow(SectionRow.LAST_ACCESSED_ITEM, lastAccessedComponent, onClickListener));
if (lastAccessedItemPlace >= 0) {
adapterData.set(lastAccessedItemPlace, new SectionRow(SectionRow.LAST_ACCESSED_ITEM, lastAccessedComponent, onClickListener));
} else {
// Add it otherwise
adapterData.add(1, new SectionRow(SectionRow.LAST_ACCESSED_ITEM, lastAccessedComponent, onClickListener));
adapterData.add(getLastAccessedItemPlace(), new SectionRow(SectionRow.LAST_ACCESSED_ITEM, lastAccessedComponent, onClickListener));
}
notifyDataSetChanged();
}

/**
* Tells the appropriate place for a {@link SectionRow#LAST_ACCESSED_ITEM} to put in the adapters list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adapter's list.

*
* @return list index (non-negative number) for a {@link SectionRow#LAST_ACCESSED_ITEM}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List index

*/
public int getLastAccessedItemPlace() {
return isNonCourseWareItemExist(SectionRow.COURSE_CERTIFICATE) ? 2 : 1;
}

/**
* Tells if specified non-courseware item exist in adapter list or not.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists in the adapter's list or not.

*
* @param sectionType A non-courseware section type whose existence needs to be checked.
* @return <code>true</code> if specified non-courseware item exist in adapter list,
* <code>false</code> otherwise.
*/
public boolean isNonCourseWareItemExist(int sectionType) {
return getNonCourseWareItemPlace(sectionType) >= 0;
}

/**
* Tells the place of a non-courseware item exist in adapter list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tells the place of a non-courseware item's existence in the adapter's list.

*
* @param sectionType A non-courseware section type whose place needs to be identified.
* @return list index (non-negative number) of a specified non-courseware item, -1 in case item
* doesn't exist.
*/
public int getNonCourseWareItemPlace(int sectionType) {
if (adapterData.isEmpty()) {
return -1;
}
SectionRow sectionRow;
for (int i = 0; i < adapterData.size(); i++) {
sectionRow = adapterData.get(i);
// return on finding first courseware item
if (sectionRow.isCoursewareRow()) {
break;
}
if (sectionRow.type == sectionType) {
return i;
}
}
return -1;
}

public ViewHolder getTag(View convertView) {
ViewHolder holder = new ViewHolder();
holder.rowType = (IconImageView) convertView
Expand Down Expand Up @@ -642,12 +709,14 @@ public static class ViewHolder {
}

public static class SectionRow {
public static final int ITEM = 0;
public static final int SECTION = 1;
public static final int COURSE_CARD = 2;
public static final int LAST_ACCESSED_ITEM = 3;
public static final int COURSE_CARD = 0;
public static final int COURSE_CERTIFICATE = 1;
public static final int LAST_ACCESSED_ITEM = 2;
public static final int SECTION = 3;
public static final int ITEM = 4;

// Update this count according to the section types mentioned above
public static final int NUM_OF_SECTION_ROWS = 4;
public static final int NUM_OF_SECTION_ROWS = 5;

public final int type;
public final boolean topComponent;
Expand All @@ -672,6 +741,11 @@ public SectionRow(int type, boolean topComponent, CourseComponent component, Vie
this.component = component;
this.clickListener = listener;
}

public boolean isCoursewareRow() {
return this.type == ITEM ||
this.type == SECTION;
}
}

public int getPositionByItemId(String itemId) {
Expand Down