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

iOS - Update web view cookie with in-app selected language #1333

Closed
wants to merge 28 commits into from

Conversation

mumer92
Copy link
Contributor

@mumer92 mumer92 commented Jan 28, 2020

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

  • Web view language should be updated as device selected language.

@codecov
Copy link

codecov bot commented Jan 28, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@d680f2f). Click here to learn what that means.
The diff coverage is 0%.

Impacted file tree graph

@@           Coverage Diff            @@
##             master   #1333   +/-   ##
========================================
  Coverage          ?     52%           
========================================
  Files             ?     432           
  Lines             ?   27317           
  Branches          ?       0           
========================================
  Hits              ?   14473           
  Misses            ?   12844           
  Partials          ?       0
Impacted Files Coverage Δ
Source/WKWebView+LanguageCookie.swift 0% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d680f2f...afcf622. Read the comment docs.

Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
Source/AuthenticatedWebViewController.swift Outdated Show resolved Hide resolved
}

getCookie(with: languageCookieName) { cookie in
if cookie == nil {
Copy link
Contributor

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.

Copy link
Contributor Author

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>)

Copy link
Contributor Author

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

self?.load(request)
}
} else {
self?.load(request)
Copy link
Contributor

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.


extension URL {
var rootDomain: String? {
guard let hostName = self.host else { return nil }
Copy link
Contributor

Choose a reason for hiding this comment

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

self can be remove.

@@ -2882,6 +2884,7 @@
77503E8D1B287DBF00C47229 /* KeyboardInsetsSource.swift */,
7784C68A1B03BA1E00529C7C /* LoadStateViewController.swift */,
9EAB5BE81B564C2F00CA9F3C /* ProgressController.swift */,
5F60728023E445E2004DBF07 /* WKWebViewExtension.swift */,
Copy link
Contributor

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.

return "prod-edx-language-preference"
}

private var defaultLanguage: String {
Copy link
Contributor

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")
}

Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

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

remove this extra line.

@@ -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 */; };
Copy link
Contributor

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"

@salman2013
Copy link
Contributor

@mumer92 Please rebase the code with latest master update.

@salman2013
Copy link
Contributor

@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?

IMG_0220

@mumer92
Copy link
Contributor Author

mumer92 commented Feb 6, 2020

@salman2013

Cookie is being applied to webview, but seems like it is not loading specific language.

Screenshot 2020-02-06 at 12 54 11 PM

@mumer92
Copy link
Contributor Author

mumer92 commented Feb 6, 2020

Seems like it is being applied on device.

IMG_8969

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)
Copy link
Contributor

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.

  1. 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.

  2. 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?

Copy link
Contributor Author

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.

@mumer92 mumer92 marked this pull request as draft April 14, 2022 11:18
@kdmccormick kdmccormick closed this May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants