Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themes: Fix theme url to include the correct image resized url #16914

Merged
merged 9 commits into from
Aug 4, 2021
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [*] Updated the header text sizes to better support large texts on Choose a Domain and Choose a Design flows. [#16923]
* [internal] Made a change to how Comment content is displayed. Should be no visible changes, but could cause regressions. [#16933]
* [*] Posts: Ampersands are correctly decoded in publishing notices instead of showing as HTML entites. [#16972]
* [***] Adjusted the image size of Theme Images for more optimal download speeds. [#16914]

17.9
-----
Expand Down
35 changes: 31 additions & 4 deletions WordPress/Classes/ViewRelated/Themes/ThemeBrowserCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,38 @@ open class ThemeBrowserCell: UICollectionViewCell {
activityView.stopAnimating()
}

private func imageUrlForWidth(imageUrl: String) -> String {
var screenshotUrl = imageUrl
// Themes not hosted on WP.com have an incorrect screenshotUrl
// it uses a // url (this is used on the web) when the scheme is not known.
if !screenshotUrl.hasPrefix("http") && screenshotUrl.hasPrefix("//") {
// Since not all sites support https
screenshotUrl = String(format: "http:%@", imageUrl)
}

guard var components = URLComponents(string: screenshotUrl) else {
return screenshotUrl
}

var queryItems: [URLQueryItem] = components.queryItems ?? []

if let screenshotWidth = presenter?.screenshotWidth {
queryItems.append(URLQueryItem(name: "w", value: "\(presenter!.screenshotWidth)"))
}

queryItems.append(URLQueryItem(name: "zoom", value: "\(UIScreen.main.scale)"))
components.queryItems = queryItems

guard let urlString = components.url?.absoluteString else {
return screenshotUrl
}

return urlString
}

fileprivate func refreshScreenshotImage(_ imageUrl: String) {
// Themes not hosted on WP.com have an incorrect screenshotUrl and do not correctly support the w param
let imageUrlForWidth = imageUrl.hasPrefix("http") ? imageUrl + "?w=\(presenter!.screenshotWidth)" :
String(format: "http:%@", imageUrl)
let screenshotUrl = URL(string: imageUrlForWidth)
let imageUrlWithWidth = imageUrlForWidth( imageUrl: imageUrl )
let screenshotUrl = URL(string: imageUrlWithWidth)

imageView.backgroundColor = Styles.placeholderColor
activityView.startAnimating()
Expand Down