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

Commit

Permalink
Using new NetworkManager for Course Enrollment (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedbashir authored and christopher lee committed Aug 25, 2016
1 parent f7b9206 commit 0a37051
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 55 deletions.
39 changes: 39 additions & 0 deletions Source/OEXCourseInfoViewController+Swift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// OEXCourseInfoViewController+Swift.swift
// edX
//
// Created by Saeed Bashir on 8/25/16.
// Copyright © 2016 edX. All rights reserved.
//

import Foundation

extension OEXCourseInfoViewController {

func enrollInCourse(courseID: String, emailOpt: Bool) {
guard let _ = OEXSession.sharedSession()?.currentUser else {
OEXRouter.sharedRouter().showSignUpScreenFromController(self, completion: {
self.enrollInCourse(courseID, emailOpt: emailOpt)
})
return;
}

let environment = OEXRouter.sharedRouter().environment;

if let _ = environment.dataManager.enrollmentManager.enrolledCourseWithID(courseID) {
showMainScreenWithMessage(Strings.findCoursesAlreadyEnrolledMessage, courseID: courseID)
return
}

let request = CourseCatalogAPI.enroll(courseID)
environment.networkManager.taskForRequest(request) {[weak self] response in
if response.response?.httpStatusCode.is2xx ?? false {
environment.analytics.trackUserEnrolledInCourse(courseID)
self?.showMainScreenWithMessage(Strings.findCoursesEnrollmentSuccessfulMessage, courseID: courseID)
}
else {
self?.showOverlayMessage(Strings.findCoursesEnrollmentErrorDescription)
}
}
}
}
2 changes: 1 addition & 1 deletion Source/OEXCourseInfoViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface OEXCourseInfoViewController : UIViewController

- (instancetype)initWithPathID:(NSString*)pathID bottomBar:(nullable UIView*) bottomBar;

- (void)showMainScreenWithMessage:(NSString*)message courseID:(NSString*)courseID;
@end

NS_ASSUME_NONNULL_END
55 changes: 1 addition & 54 deletions Source/OEXCourseInfoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ - (BOOL)webViewHelper:(FindCoursesWebViewHelper *)helper shouldLoadLinkWithReque
BOOL emailOptIn = false;
[self parseURL:request.URL getCourseID:&courseID emailOptIn:&emailOptIn];
if(courseID != nil) {
[self enrollInCourseWithCourseID:courseID emailOptIn:emailOptIn];
[self enrollInCourse:courseID emailOpt:emailOptIn];
return NO;
}
return YES;
Expand All @@ -98,64 +98,11 @@ - (void)parseURL:(NSURL*)url getCourseID:(NSString* __autoreleasing*)courseID em
}
}

- (void)enrollInCourseWithCourseID:(NSString*)courseID emailOptIn:(BOOL)emailOptIn {

OEXSession* session = [OEXSession sharedSession];
if (session.currentUser == nil) {
[[OEXRouter sharedRouter] showSignUpScreenFromController:self completion:^{
[self enrollInCourseWithCourseID:courseID emailOptIn:emailOptIn];
}];
return;
}

UserCourseEnrollment* courseEnrollment = [[OEXInterface sharedInterface] enrollmentForCourseWithID:courseID];

if(courseEnrollment) {
[self showMainScreenWithMessage:[Strings findCoursesAlreadyEnrolledMessage] courseID:courseID];
return;
}

OEXNetworkManager* networkManager = [OEXNetworkManager sharedManager];

NSDictionary* enrollmentDictionary = @{@"course_details":@{@"course_id": courseID, @"email_opt_in":@(emailOptIn)}};

NSData* enrollmentJSONData = [NSJSONSerialization dataWithJSONObject:enrollmentDictionary options:0 error:nil];

[[OEXAnalytics sharedAnalytics] trackUserEnrolledInCourse:courseID];

[networkManager callAuthorizedWebServiceWithURLPath:URL_COURSE_ENROLLMENT method:OEXHTTPMethodPOST body:enrollmentJSONData completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if(httpResponse.statusCode == 200) {
if([NSThread isMainThread]) {
[self showMainScreenWithMessage:[Strings findCoursesEnrollmentSuccessfulMessage] courseID:courseID];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self showMainScreenWithMessage:[Strings findCoursesEnrollmentSuccessfulMessage] courseID:courseID];
});
}
return;
}
if([NSThread isMainThread]) {
[self showEnrollmentError];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self showEnrollmentError];
});
}
}];
}

- (void)showMainScreenWithMessage:(NSString*)message courseID:(NSString*)courseID {
[[OEXRouter sharedRouter] showMyCoursesAnimated:YES pushingCourseWithID:courseID];
[self performSelector:@selector(postEnrollmentSuccessNotification:) withObject:message afterDelay:0.5];
}

- (void)showEnrollmentError {
[[OEXFlowErrorViewController sharedInstance] showErrorWithTitle:[Strings findCoursesEnrollmentErrorTitle] message:[Strings findCoursesEnrollmentErrorDescription] onViewController:self.view shouldHide:YES];
}

- (void)postEnrollmentSuccessNotification:(NSString*)message {
[[NSNotificationCenter defaultCenter] postNotificationName:EnrollmentShared.successNotification object:message];

Expand Down
1 change: 1 addition & 0 deletions Source/edX-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#import "OEXVideoSummary.h"
#import "OEXVideoPlayerInterface.h"
#import "UIAppearance+Swift.h"
#import "OEXCourseInfoViewController.h"

#import "Reachability.h"
#import "SWRevealViewController.h"
Expand Down
4 changes: 4 additions & 0 deletions edX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@
E0A246211D5DB3FD0066C766 /* AppUpgradeConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0A246201D5DB3FD0066C766 /* AppUpgradeConfigTests.swift */; };
E0AF4E8E1BFB19CC0083753C /* PassthroughView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0AF4E8D1BFB19CC0083753C /* PassthroughView.swift */; };
E0C6EF971BFF4B9900B315E3 /* UIButton+TintColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0C6EF961BFF4B9900B315E3 /* UIButton+TintColor.swift */; };
E0ED96E71D6EED1C00B48B64 /* OEXCourseInfoViewController+Swift.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0ED96E61D6EED1C00B48B64 /* OEXCourseInfoViewController+Swift.swift */; };
E0FC64C31C85B492004E3E92 /* DiscussionDataParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0FC64C11C85B46C004E3E92 /* DiscussionDataParsingTests.swift */; };
F22A0ABB1CE60D5E00D50DFF /* NetworkManager+Authenticators.swift in Sources */ = {isa = PBXBuildFile; fileRef = F22A0ABA1CE60D5E00D50DFF /* NetworkManager+Authenticators.swift */; };
F22A0ABD1CE660B300D50DFF /* LoginApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = F22A0ABC1CE660B300D50DFF /* LoginApi.swift */; };
Expand Down Expand Up @@ -1575,6 +1576,7 @@
E0A246201D5DB3FD0066C766 /* AppUpgradeConfigTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppUpgradeConfigTests.swift; sourceTree = "<group>"; };
E0AF4E8D1BFB19CC0083753C /* PassthroughView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PassthroughView.swift; sourceTree = "<group>"; };
E0C6EF961BFF4B9900B315E3 /* UIButton+TintColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+TintColor.swift"; sourceTree = "<group>"; };
E0ED96E61D6EED1C00B48B64 /* OEXCourseInfoViewController+Swift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "OEXCourseInfoViewController+Swift.swift"; sourceTree = "<group>"; };
E0FC64C11C85B46C004E3E92 /* DiscussionDataParsingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiscussionDataParsingTests.swift; sourceTree = "<group>"; };
F22A0ABA1CE60D5E00D50DFF /* NetworkManager+Authenticators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NetworkManager+Authenticators.swift"; sourceTree = "<group>"; };
F22A0ABC1CE660B300D50DFF /* LoginApi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginApi.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -2724,6 +2726,7 @@
9C9B7F641A8CC68400A857B2 /* OEXFindCoursesViewController.m */,
9C9B7F651A8CC68400A857B2 /* OEXCourseInfoViewController.h */,
9C9B7F661A8CC68400A857B2 /* OEXCourseInfoViewController.m */,
E0ED96E61D6EED1C00B48B64 /* OEXCourseInfoViewController+Swift.swift */,
);
name = "Course Enrollment";
sourceTree = "<group>";
Expand Down Expand Up @@ -4213,6 +4216,7 @@
8F562F881A1F2D2C00320DB3 /* OEXGoogleSocial.m in Sources */,
E03E6A151D38C91F00944AAA /* OfflineSupportViewController.swift in Sources */,
B4B6D6291A949F1B000F44E8 /* OEXRegistrationFieldEmailController.m in Sources */,
E0ED96E71D6EED1C00B48B64 /* OEXCourseInfoViewController+Swift.swift in Sources */,
773B1D421B1F48F100B861DF /* CourseOutlineHeaderView.swift in Sources */,
778B61061B06BD2100489532 /* NSNotificationCenter+SafeSwift.swift in Sources */,
B4B6D6011A949EFC000F44E8 /* OEXRegistrationError.m in Sources */,
Expand Down

0 comments on commit 0a37051

Please sign in to comment.