iOS - Update web view cookie with in-app selected language #1333
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1333 +/- ##
========================================
Coverage ? 52%
========================================
Files ? 432
Lines ? 27317
Branches ? 0
========================================
Hits ? 14473
Misses ? 12844
Partials ? 0
Continue to review full report at Codecov.
|
} | ||
|
||
getCookie(with: languageCookieName) { cookie in | ||
if cookie == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you confirm that either the cookie will persist with each session start? If it is then we don't need to set cookie every time by check if the cookie already set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Printing out cookie value gives:
Optional(<NSHTTPCookie
version:1
name:prod-edx-language-preference
value:en
expiresDate:'2020-03-13 03:30:32 +0000'
created:'2020-01-31 11:30:32 +0000'
sessionOnly:FALSE
domain:.edx.org
partition:none
sameSite:none
path:/
isSecure:FALSE
path:"/" isSecure:FALSE>)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Execution time for this function:
Time elapsed for cookie found: 1.77224600315094 seconds
Time elapsed for cookie not found and setting cookie: 1.77321195602417 seconds
Time elapsed for cookie found: 1.9854040145874023 seconds
Time elapsed for cookie not found and setting cookie: 1.9858750104904175 seconds
Time elapsed for cookie found: 1.8449450731277466 seconds
Time elapsed for cookie not found and setting cookie: 1.846182107925415 seconds
Time elapsed for cookie found: 1.8192960023880005 seconds
Time elapsed for cookie not found and setting cookie: 1.8198130130767822 seconds
Source/WKWebViewExtension.swift
Outdated
self?.load(request) | ||
} | ||
} else { | ||
self?.load(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
load(request) seems to be redundant in all conditions.
Source/WKWebViewExtension.swift
Outdated
|
||
extension URL { | ||
var rootDomain: String? { | ||
guard let hostName = self.host else { return nil } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self can be remove.
edX.xcodeproj/project.pbxproj
Outdated
@@ -2882,6 +2884,7 @@ | |||
77503E8D1B287DBF00C47229 /* KeyboardInsetsSource.swift */, | |||
7784C68A1B03BA1E00529C7C /* LoadStateViewController.swift */, | |||
9EAB5BE81B564C2F00CA9F3C /* ProgressController.swift */, | |||
5F60728023E445E2004DBF07 /* WKWebViewExtension.swift */, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you place the extension class along with all other extensions, this will look more organized. The extension name can be more specific as per apple recommendations.
Source/WKWebViewExtension.swift
Outdated
return "prod-edx-language-preference" | ||
} | ||
|
||
private var defaultLanguage: String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done like
guard let language = NSLocale.preferredLanguages.first, Bundle.main.preferredLocalizations.contains(language) else { return "en" } return language
} else { | ||
request.addValue("\(languageCookieName)=\(defaultLanguage))", forHTTPHeaderField: "Cookie") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this extra line.
|
||
func loadRequest(_ request: URLRequest) { | ||
var request = request | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this extra line.
edX.xcodeproj/project.pbxproj
Outdated
@@ -176,7 +176,7 @@ | |||
5DD0FFCF1B1D225C00837121 /* DiscussionNewPostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DD0FFCD1B1D225C00837121 /* DiscussionNewPostViewController.swift */; }; | |||
5DD0FFD21B1D23DA00837121 /* DiscussionNewPostViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5DD0FFD11B1D23DA00837121 /* DiscussionNewPostViewController.xib */; }; | |||
5DEA47D71B62F64300831EC9 /* DiscussionObjectModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DEA47D61B62F64300831EC9 /* DiscussionObjectModel.swift */; }; | |||
5F60728123E445E3004DBF07 /* WKWebViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F60728023E445E2004DBF07 /* WKWebViewExtension.swift */; }; | |||
5F60728123E445E3004DBF07 /* WKWebView+LoadRequestExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F60728023E445E2004DBF07 /* WKWebView+LoadRequestExtension.swift */; }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name should be more specific to the functionality. I think "WKWebView+LanguageCookie"
@mumer92 Please rebase the code with latest master update. |
@mumer92 When i am changing the language to Spanish the discovery webvew is not updating the language. Can you please check whats the reason for it? |
Cookie is being applied to webview, but seems like it is not loading specific language. |
private var defaultLanguage: String { | ||
guard let language = NSLocale.preferredLanguages.first, | ||
Bundle.main.preferredLocalizations.contains(language) else { return "en" } | ||
return language | ||
} | ||
|
||
private var storedLanguageCookieValue: String { | ||
set { | ||
UserDefaults.standard.set(newValue, forKey: SelectedLanguageCookieValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can not read the language code from the existing cookie? My thinking is that there are two cases.
-
Cookie exists
If cookie exists then check if the cookie language value is not equal to the current app selected language then update the cookie with language otherwise do nothing. -
Cookie Not exist
If cookie do not exist then create the new cookie with the app selected language the set it.
I don't think there is a need to use userDefaults whats your thoughts on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have a look into updated logic.
Description
LEARNER-7498
Create and set web view cookie with the selected language locale.
Cookie name should be "prod-edx-language-preference" and its value should be selected language locale.
How to test this PR