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

Commit

Permalink
Merge pull request #701 from edx/mkatz/search
Browse files Browse the repository at this point in the history
query for courses
  • Loading branch information
mikekatz committed Apr 8, 2016
2 parents f2fa3f7 + c55db0a commit fbad37f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
7 changes: 5 additions & 2 deletions Source/EnrollmentConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ enum EnrollmentType : String {
private enum EnrollmentKeys: String, RawStringExtractable {
case CourseSearchURL = "COURSE_SEARCH_URL"
case CourseInfoURLTemplate = "COURSE_INFO_URL_TEMPLATE"
case NativeSearchBarEnabled = "SEARCH_BAR_ENABLED"
case EnrollmentType = "TYPE"
case Webview = "WEBVIEW"
}

class EnrollmentWebviewConfig : NSObject {
let searchURL: NSURL?
let courseInfoURLTemplate: String?
let nativeSeachbarEnabled: Bool

init(dictionary: [String: AnyObject]) {
self.searchURL = (dictionary[EnrollmentKeys.CourseSearchURL] as? String).flatMap { NSURL(string:$0)}
self.courseInfoURLTemplate = dictionary[EnrollmentKeys.CourseInfoURLTemplate] as? String
searchURL = (dictionary[EnrollmentKeys.CourseSearchURL] as? String).flatMap { NSURL(string:$0)}
courseInfoURLTemplate = dictionary[EnrollmentKeys.CourseInfoURLTemplate] as? String
nativeSeachbarEnabled = dictionary[EnrollmentKeys.NativeSearchBarEnabled] as? Bool ?? false
}
}

Expand Down
64 changes: 60 additions & 4 deletions Source/FindCoursesWebViewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class FindCoursesWebViewHelper: NSObject, WKNavigationDelegate {
weak var delegate : FindCoursesWebViewHelperDelegate?

let webView : WKWebView = WKWebView()
let searchBar = UISearchBar()
private var loadController = LoadStateViewController()

private var request : NSURLRequest? = nil
var searchBaseURL: NSURL?

init(config : OEXConfig?, delegate : FindCoursesWebViewHelperDelegate?) {
self.config = config
Expand All @@ -34,10 +36,33 @@ class FindCoursesWebViewHelper: NSObject, WKNavigationDelegate {

if let container = delegate?.containingControllerForWebViewHelper(self) {
loadController.setupInController(container, contentView: webView)
container.view.insertSubview(self.webView, atIndex: 0)

self.webView.snp_makeConstraints { make in
make.edges.equalTo(container.view)

let searchbarEnabled = config?.courseEnrollmentConfig.webviewConfig.nativeSeachbarEnabled ?? false

let webviewTop: ConstraintItem
if searchbarEnabled {
searchBar.delegate = self

container.view.insertSubview(searchBar, atIndex: 0)

searchBar.snp_makeConstraints{ make in
make.leading.equalTo(container.view)
make.trailing.equalTo(container.view)
make.top.equalTo(container.view)
}
webviewTop = searchBar.snp_bottom
} else {
webviewTop = container.view.snp_top
}


container.view.insertSubview(webView, atIndex: 0)

webView.snp_makeConstraints { make in
make.leading.equalTo(container.view)
make.trailing.equalTo(container.view)
make.bottom.equalTo(container.view)
make.top.equalTo(webviewTop)
}
}
}
Expand Down Expand Up @@ -101,3 +126,34 @@ class FindCoursesWebViewHelper: NSObject, WKNavigationDelegate {
}
}
}

extension FindCoursesWebViewHelper: UISearchBarDelegate {
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
return true
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()

guard let searchTerms = searchBar.text, searchURL = searchBaseURL else { return }
if let URL = FindCoursesWebViewHelper.buildQuery(searchURL.URLString, toolbarString: searchTerms) {
loadRequestWithURL(URL)
}
}

@objc static func buildQuery(baseURL: String, toolbarString: String) -> NSURL? {
let items = toolbarString.componentsSeparatedByString(" ")
let escapedItems = items.flatMap { $0.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) }
let searchTerm = "search_query=" + escapedItems.joinWithSeparator("+")
let newQuery: String
if baseURL.containsString("?") {
newQuery = baseURL + "&" + searchTerm
} else {
newQuery = baseURL + "?" + searchTerm

}
return NSURL(string: newQuery)
}
}


3 changes: 2 additions & 1 deletion Source/OEXFindCoursesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ - (void)viewDidLoad {
self.webViewHelper = [[FindCoursesWebViewHelper alloc] initWithConfig:[OEXConfig sharedConfig] delegate:self];
self.view.backgroundColor = [[OEXStyles sharedStyles] standardBackgroundColor];

[self.webViewHelper loadRequestWithURL:[self enrollmentConfig].webviewConfig.searchURL];
self.webViewHelper.searchBaseURL = [self enrollmentConfig].webviewConfig.searchURL;
[self.webViewHelper loadRequestWithURL:self.webViewHelper.searchBaseURL];
}

- (EnrollmentConfig*)enrollmentConfig {
Expand Down
5 changes: 4 additions & 1 deletion Test/EnrollmentConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ class EnrollmentConfigTests : XCTestCase {
"TYPE": "webview",
"WEBVIEW" : [
"COURSE_SEARCH_URL" : sampleSearchURL,
"COURSE_INFO_URL_TEMPLATE" : sampleInfoURLTemplate
"COURSE_INFO_URL_TEMPLATE" : sampleInfoURLTemplate,
"SEARCH_BAR_ENABLED" : true
]
]
]
let config = OEXConfig(dictionary: configDictionary)
XCTAssertEqual(config.courseEnrollmentConfig.type, EnrollmentType.Webview)
XCTAssertEqual(config.courseEnrollmentConfig.webviewConfig.searchURL!.absoluteString, sampleSearchURL)
XCTAssertEqual(config.courseEnrollmentConfig.webviewConfig.courseInfoURLTemplate!, sampleInfoURLTemplate)
//TODO: re-enable once we figure out the compiler's deal
// XCTAssertTrue(config.courseEnrollmentConfig.webviewConfig.nativeSearchbarEnabled)
}

}
20 changes: 20 additions & 0 deletions Test/OEXFindCoursesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,24 @@ -(void)disable_testCourseInfoURLTemplateSubstitution{
XCTAssertEqualObjects(courseURLString, @"https://webview.edx.org/course/science-happiness-uc-berkeleyx-gg101x", @"Course Info URL incorrectly determined");
}

- (void) testSearchQueryBare {
NSString* baseURL = @"http://www.fakex.com/course";
NSString* queryString = @"mobile linux";

NSString* expected = @"http://www.fakex.com/course?search_query=mobile+linux";
NSURL* expectedURL = [NSURL URLWithString:expected];
NSURL* output = [FindCoursesWebViewHelper buildQuery:baseURL toolbarString:queryString];
XCTAssertEqualObjects(output, expectedURL);
}

- (void) testSearchQueryAlreadyHasQuery {
NSString* baseURL = @"http://www.fakex.com/course?type=mobile";
NSString* queryString = @"mobile linux";

NSString* expected = @"http://www.fakex.com/course?type=mobile&search_query=mobile+linux";
NSURL* expectedURL = [NSURL URLWithString:expected];
NSURL* output = [FindCoursesWebViewHelper buildQuery:baseURL toolbarString:queryString];
XCTAssertEqualObjects(output, expectedURL);
}

@end

0 comments on commit fbad37f

Please sign in to comment.