Skip to content

Commit 3d0f3b4

Browse files
author
aforge
committed
Branding config remaining bits.
1 parent 3d98ab4 commit 3d0f3b4

File tree

6 files changed

+187
-81
lines changed

6 files changed

+187
-81
lines changed

Tinodios/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
8484
if SharedUtils.isFirstLaunch {
8585
Cache.log.info("First time launch. Setting up...")
8686
SharedUtils.isFirstLaunch = false
87-
// TODO: uncomment when branding setup is complete.
88-
// SharedUtils.setUpBranding()
87+
SharedUtils.identifyAndConfigureBranding()
8988
}
9089
SharedUtils.registerUserDefaults()
9190
let baseDb = BaseDb.sharedInstance

Tinodios/Base.lproj/Main.storyboard

Lines changed: 112 additions & 77 deletions
Large diffs are not rendered by default.

Tinodios/ChatListViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import UIKit
99
import TinodeSDK
10+
import TinodiosDB
1011

1112
protocol ChatListDisplayLogic: AnyObject {
1213
func displayChats(_ topics: [DefaultComTopic], archivedTopics: [DefaultComTopic]?)
@@ -57,6 +58,10 @@ class ChatListViewController: UITableViewController, ChatListDisplayLogic {
5758
button.addTarget(self, action: #selector(navigateToArchive), for: .touchUpInside)
5859
archivedChatsFooter!.addSubview(button)
5960
tableView.tableFooterView = archivedChatsFooter
61+
// Customize the title if needed.
62+
if let serviceName = SharedUtils.serviceName {
63+
self.title = serviceName
64+
}
6065
}
6166

6267
private func toggleFooter(visible: Bool) {

Tinodios/SettingsHelpViewController.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import TinodiosDB
1111
import UIKit
1212

1313
class SettingsHelpViewController: UITableViewController {
14+
// 44px is the default UITableView row height.
15+
// TODO: may be 88px for retina display. Handle it.
16+
private static let kDefaultRowHeight: CGFloat = 44
17+
1418
@IBOutlet weak var contactUs: UITableViewCell!
1519
@IBOutlet weak var termsOfUse: UITableViewCell!
1620
@IBOutlet weak var privacyPolicy: UITableViewCell!
@@ -19,9 +23,12 @@ class SettingsHelpViewController: UITableViewController {
1923
@IBOutlet weak var serviceNameLabel: UILabel!
2024
@IBOutlet weak var serviceLinkLabel: UILabel!
2125
@IBOutlet weak var serverAddressLabel: UILabel!
26+
@IBOutlet weak var poweredByView: UIView!
2227

2328
private var tosUrl: URL!
2429
private var privacyUrl: URL!
30+
private var isUsingCustomBranding = false
31+
private var contentHeight: CGFloat = 0
2532

2633
override func viewDidLoad() {
2734
super.viewDidLoad()
@@ -52,8 +59,6 @@ class SettingsHelpViewController: UITableViewController {
5259
// Logo.
5360
if let logo = SharedUtils.largeIcon {
5461
logoView.image = logo
55-
} else {
56-
logoView.image = UIImage(named: "logo-ios")
5762
}
5863
// Service name.
5964
if let serviceName = SharedUtils.serviceName {
@@ -67,6 +72,39 @@ class SettingsHelpViewController: UITableViewController {
6772
// Server address.
6873
let (host, tls) = Tinode.getConnectionParams()
6974
serverAddressLabel.text = (tls ? "https://" : "http://") + host
75+
76+
// Precompute content height.
77+
// Table is confitured as static cells. Can use simple loop.
78+
self.contentHeight = 0
79+
for i in 0..<tableView.numberOfSections {
80+
let numRows = tableView.numberOfRows(inSection: i)
81+
for j in 0..<numRows {
82+
let h = tableView(self.tableView, heightForRowAt: IndexPath(row: j, section: i))
83+
self.contentHeight += h > 0 ? h : SettingsHelpViewController.kDefaultRowHeight
84+
}
85+
}
86+
87+
if SharedUtils.appId != nil {
88+
self.isUsingCustomBranding = true
89+
}
90+
self.poweredByView.isHidden = !isUsingCustomBranding
91+
}
92+
93+
override func viewDidLayoutSubviews() {
94+
super.viewDidLayoutSubviews()
95+
96+
if isUsingCustomBranding {
97+
// Adjust "Powered by" view position.
98+
let topPadding = self.tableView.safeAreaInsets.top
99+
let bottomPadding = self.tableView.safeAreaInsets.bottom
100+
// Total space available below table content and the bottom.
101+
let height = tableView.frame.height - topPadding - bottomPadding - self.contentHeight
102+
// height < 0 means "Powered By" isn't visible.
103+
let h = height > 0 ? height : SettingsHelpViewController.kDefaultRowHeight
104+
if h >= SettingsHelpViewController.kDefaultRowHeight && poweredByView.frame.size.height != h {
105+
poweredByView.frame.size.height = h
106+
}
107+
}
70108
}
71109

72110
@objc func termsOfUseClicked(sender: UITapGestureRecognizer) {

Tinodios/UiUtils.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class UiUtils {
268268
DispatchQueue.main.async {
269269
let storyboard = UIStoryboard(name: "Main", bundle: nil)
270270
let initialViewController = storyboard.instantiateViewController(withIdentifier: "ChatsNavigator") as! UINavigationController
271+
if let serviceName = SharedUtils.serviceName {
272+
initialViewController.title = serviceName
273+
}
271274
if let window = (UIApplication.shared.delegate as! AppDelegate).window {
272275
window.rootViewController = initialViewController
273276
}
@@ -326,6 +329,9 @@ class UiUtils {
326329
} else {
327330
rootVC = storyboard.instantiateViewController(
328331
withIdentifier: "ChatsNavigator") as! UINavigationController
332+
if let serviceName = SharedUtils.serviceName {
333+
rootVC.title = serviceName
334+
}
329335
}
330336
let messageVC =
331337
storyboard.instantiateViewController(

TinodiosDB/SharedUtils.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,29 @@ public class SharedUtils {
364364
task.resume()
365365
}
366366

367+
// Identifies device with Tinode server and fetches branding configuration code.
368+
public static func identifyAndConfigureBranding() {
369+
let url = URL(string: "https://hosts.tinode.co/whoami")!
370+
print("Self-identifying with the server. Endpoint: ", url.absoluteString)
371+
let task = URLSession.shared.dataTask(with: URLRequest(url: url)) { data, response, error in
372+
guard let data = data, error == nil else {
373+
print("Branding config response error: " + (error?.localizedDescription ?? "Failed to self-identify"))
374+
return
375+
}
376+
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
377+
if let responseJSON = responseJSON as? [String: Any] {
378+
print("Branding identity config: ", responseJSON)
379+
380+
if let code = responseJSON["code"] as? String {
381+
SharedUtils.setUpBranding(withConfigurationCode: code)
382+
} else {
383+
print("Branding config error: Missing configuration code in the response. Quitting.")
384+
}
385+
}
386+
}
387+
task.resume()
388+
}
389+
367390
// Configures application branding and connection settings.
368391
public static func setUpBranding(withConfigurationCode configCode: String) {
369392
guard !configCode.isEmpty else {

0 commit comments

Comments
 (0)