Skip to content

Commit

Permalink
Merge pull request #4 from WebEngage/develop
Browse files Browse the repository at this point in the history
Merge into master
  • Loading branch information
ShubhamN123 authored Nov 20, 2024
2 parents 3e8acc0 + 10201cb commit 297229a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 37 deletions.
65 changes: 39 additions & 26 deletions Sources/WEContentExtension/Common/CoreApi/WEXCoreUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,22 @@ struct WEXCoreUtils {
guard let message = message else {
return nil
}

guard let attributedString = viewController?.getHtmlParsedString(message, isTitle: false, bckColor: colorHex) else {
return nil
}

let finalAttributedString = NSMutableAttributedString(attributedString: attributedString)
let rawString = attributedString.string
let lines = rawString.components(separatedBy: "\n")
let finalAttributedString = NSMutableAttributedString()

for line in lines {
if !line.isEmpty {
guard let alignment = viewController?.naturalTextAlignmentForText(line, forDescription: true) else {
continue
}

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = alignment
let paragraphRanges = rawString.paragraphRanges()

let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle
]

let attributedLine = NSAttributedString(string: line, attributes: attributes)
finalAttributedString.append(attributedLine)
if line != lines.last {
finalAttributedString.append(NSAttributedString(string: "\n"))
}
for range in paragraphRanges {
let paragraphText = (rawString as NSString).substring(with: range)
guard let alignment = viewController?.naturalTextAlignmentForText(paragraphText, forDescription: true) else {
continue
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = alignment
finalAttributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
}

return finalAttributedString
}

Expand All @@ -69,13 +55,18 @@ struct WEXCoreUtils {
let bundleIdentifier = bundle.object(forInfoDictionaryKey: WEConstants.CFBUNDLEIDENTIFIER) as? String
appGroup = "\(WEConstants.GROUP).\(bundleIdentifier ?? "").\(WEConstants.WENOTIFICATIONGROUP)"
}


if let appGroupUrl = appGroup, let _ = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupUrl) {
print("WebEngage App Group configured in Content Extension")
} else {
ALog("WebEngage App Group not configured in Content Extension")
}

if let defaults = UserDefaults(suiteName: appGroup) {
return defaults
} else {
print("Shared User Defaults could not be initialized. Ensure Shared App Groups have been enabled on Main App & Notification Service Extension Targets.")
fatalError("Shared User Defaults initialization failed.")
}
return nil
}

// Checks if a given string contains HTML tags using regular expressions.
Expand All @@ -90,6 +81,9 @@ struct WEXCoreUtils {
sharedDefaults.synchronize()
}
}
static func ALog(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
NSLog("%@ [Line %d] ERROR: %@", (function as NSString).lastPathComponent, line, message)
}
}


Expand All @@ -115,3 +109,22 @@ extension Character {
return unicodeScalars.count == 1 && emojiRange.contains(unicodeScalars.first!)
}
}

extension String {
func paragraphRanges() -> [NSRange] {
let nsString = self as NSString
var ranges: [NSRange] = []
var paragraphStart = 0
var paragraphEnd = 0
var contentsEnd = 0

let length = nsString.length
while paragraphStart < length {
nsString.getParagraphStart(&paragraphStart, end: &paragraphEnd, contentsEnd: &contentsEnd, for: NSMakeRange(paragraphStart, 0))
ranges.append(NSMakeRange(paragraphStart, contentsEnd - paragraphStart))
paragraphStart = paragraphEnd
}

return ranges
}
}
4 changes: 2 additions & 2 deletions Sources/WEContentExtension/Common/WEConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct WEConstants{
static let BIG_PICTURE = "BIG_PICTURE"
static let BIG_TEXT = "BIG_TEXT"
static let OVERLAY = "OVERLAY"
static let BLACKCOLOR = "bckColor"
static let BACKCOLOR = "bckColor"
static let CAROUSEL = "CAROUSEL_V1"
static let COLLAPSED = "collapsed"
static let CONTENT_PADDING: CGFloat = 10.0
Expand Down Expand Up @@ -44,7 +44,7 @@ struct WEConstants{
static let WENOTIFICATIONGROUP = "WEGNotificationGroup"
static let WEBENGAGE = "webengage"
static let WEX_APP_GROUP = "WEX_APP_GROUP"
static let WEX_CONTENT_EXTENSION_VERSION = "1.1.1"
static let WEX_CONTENT_EXTENSION_VERSION = "1.1.2"
static let WEX_CONTENT_EXTENSION_VERSION_STRING = "WEContentExtension_version"
static let WHITECOLOR = "#FFFFFF"
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension WEXBannerPushNotificationViewController{
/// Sets up the container for rich content labels based on the expandable details of the notification.
func setupLabelsContainer() {
if let superViewWrapper = view?.subviews.first,
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any], let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String{
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any], let colorHex = expandableDetails[WEConstants.BACKCOLOR] as? String{
let richContentView = UIView()
if #available(iOS 13.0, *) {
richContentView.backgroundColor = UIColor.colorFromHexString(colorHex, defaultColor: UIColor.WEXWhiteColor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension WEXCarouselPushNotificationViewController{
var superViewHeight = viewHeight + 2 * verticalMargins

if let expandableDetails = notification.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String : Any]{
if let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String{
if let colorHex = expandableDetails[WEConstants.BACKCOLOR] as? String{
if #available(iOS 13.0, *) {
self.view?.backgroundColor = UIColor.colorFromHexString(colorHex, defaultColor: UIColor.WEXWhiteColor())
}
Expand Down Expand Up @@ -85,7 +85,7 @@ extension WEXCarouselPushNotificationViewController{
}

let bottomSeparator = UIView(frame: CGRect(x: 0.0, y: CGFloat(superViewHeight) - 0.5, width: superViewWidth, height: 0.5))
if let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String{
if let colorHex = expandableDetails[WEConstants.BACKCOLOR] as? String{
if #available(iOS 13.0, *) {
bottomSeparator.backgroundColor = UIColor.colorFromHexString(colorHex, defaultColor: UIColor.WEXGreyColor())
}}
Expand Down Expand Up @@ -124,7 +124,7 @@ extension WEXCarouselPushNotificationViewController{
richMessage = self.notification?.request.content.body
}

let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String
let colorHex = expandableDetails[WEConstants.BACKCOLOR] as? String

let richTitleLabel = UILabel()
if let richTitle = richTitle{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class WEXCarouselPushNotificationViewController: WEXRichPushLayout {
viewHeight = viewWidth * WEConstants.LANDSCAPE_ASPECT
}
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any]
let colorHex = expandableDetails?[WEConstants.BLACKCOLOR] as? String ?? ""
let colorHex = expandableDetails?[WEConstants.BACKCOLOR] as? String ?? ""
if #available(iOS 13.0, *) {
viewToReturn.backgroundColor = UIColor.colorFromHexString(colorHex, defaultColor: UIColor.WEXGreyColor())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension WEXOverlayPushNotificationViewController{
/// Sets up the container for rich content labels based on the expandable details of the notification.
func setupLabelsContainer() {
if let superViewWrapper = view?.subviews.first,
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any], let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String{
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any], let colorHex = expandableDetails[WEConstants.BACKCOLOR] as? String{
let richContentView = UIView()
if #available(iOS 13.0, *) {
richContentView.backgroundColor = UIColor.clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ extension WEXRatingPushNotificationViewController{
richMessage = self.notification?.request.content.body
}

let colorHex = expandableDetails?[WEConstants.BLACKCOLOR] as? String
let colorHex = expandableDetails?[WEConstants.BACKCOLOR] as? String

// Add a notification content view for displaying title and body.
let richContentView = UIView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ extension WEXTextPushNotificationViewController {
/// Sets up the container for rich content labels based on the expandable details of the notification.
func setupLabelsContainer() {
if let superViewWrapper = view?.subviews.first,
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any], let colorHex = expandableDetails[WEConstants.BLACKCOLOR] as? String{
let expandableDetails = notification?.request.content.userInfo[WEConstants.EXPANDABLEDETAILS] as? [String: Any]{
let richContentView = UIView()
let colorHex = (expandableDetails[WEConstants.EXPANDABLEDETAILS] as? String) ?? ""
if #available(iOS 13.0, *) {
richContentView.backgroundColor = UIColor.colorFromHexString(colorHex, defaultColor: UIColor.WEXWhiteColor())
}
Expand Down
2 changes: 1 addition & 1 deletion WEContentExtension.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'WEContentExtension'
s.version = '1.1.1'
s.version = '1.1.2'
s.summary = 'Extension Target SDK for adding WebEngage Rich Push Notifications support'

s.description = <<-DESC
Expand Down

0 comments on commit 297229a

Please sign in to comment.