Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// AlertManager.swift
// uikitsimple
//
// Copyright © 2020 SendBird, Inc. All rights reserved.
//
import UIKit
// This function handles alertController to be used in the sample app.
class AlertManager: NSObject {
static func show(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let closeAction = UIAlertAction(title: "Close", style: .cancel, handler: nil)
alert.addAction(closeAction)
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(alert, animated: true, completion: nil)
}
}
static func showCustomInfo(_ function: String) {
self.show(title: "Custom", message: "\(function) function can be customized.")
}
}
//
// AppDelegate.swift
// uikitsimple
//
// Copyright © 2020 SendBird, Inc. All rights reserved.
//
import UIKit
import SendBirdUIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let APP_ID = "YOUR_APP_ID" // The ID of the Sendbird application which UIKit sample app uses.
SBUMain.initialize(applicationId: APP_ID)
SBUMain.setLogLevel(.error)
SBUGlobals.CurrentUser = SBUUser(userId: "USER_ID")
SBUGlobals.AccessToken = "ACCESS_OR_SESSION_TOKEN" //Optional
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
//
// ChannelListViewController.swift
// uikitsimple
//
// Created by Jason.Allshorn on 2021/02/17.
// Copyright © 2020 SendBird, Inc. All rights reserved.
//
import UIKit
import SendBirdUIKit
class ChannelListVC: SBUChannelListViewController {
override init(channelListQuery: SBDGroupChannelListQuery? = nil) {
super.init(channelListQuery: channelListQuery)
self.titleView = self.createCustomTitleLabel()
self.leftBarButton = self.createHighlightedBackButton()
self.rightBarButton?.isEnabled = false
// Change empty view
#if swift(>=5.2)
let emptyView = CustomEmptyView()
#else
let emptyView = CustomEmptyView(frame: .zero)
#endif
self.emptyView = emptyView
// CUSTOM CHANNEL CELL
// This part changes the default channel cell to a custom cell.
#if swift(>=5.2)
self.register(channelCell: CustomChannelListCell())
#else
self.register(channelCell: CustomChannelListCell(
style: .default,
reuseIdentifier: CustomChannelListCell.sbu_className)
)
#endif
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let channel = self.channelList[indexPath.row].channelUrl
print(channel)
}
func createCustomTitleLabel() -> UILabel {
let titleLabel = UILabel()
titleLabel.text = "Your Chat List"
return titleLabel
}
@objc func onClickBack() {
print("clicked")
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}
func createHighlightedBackButton() -> UIBarButtonItem {
return UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(onClickBack))
}
}
class ViewController: UIViewController {
@IBAction func openChat(_ sender: UIButton) {
let listQuery = SBDGroupChannel.createMyGroupChannelListQuery()
listQuery?.includeEmptyChannel = true
listQuery?.includeFrozenChannel = true
// ... You can set more query options
let channelListVC = ChannelListVC(channelListQuery: listQuery)
let naviVC = UINavigationController(rootViewController: channelListVC)
naviVC.modalPresentationStyle = .fullScreen
present(naviVC, animated: true)
}
}
//
// CustomChannelListCell.swift
// SendBirdUIKit-Sample
//
// Copyright © 2020 SendBird, Inc. All rights reserved.
//
import UIKit
import SendBirdUIKit
class CustomChannelListCell: SBUBaseChannelCell {
// MARK: - Properties
@SBUAutoLayout var coverImage = UIImageView()
@SBUAutoLayout var separatorLine = UIView()
@SBUAutoLayout var titleLabel = UILabel()
@SBUAutoLayout var memberLabel = UILabel()
@SBUAutoLayout var titleStackView: UIStackView = {
let titleStackView = UIStackView()
titleStackView.alignment = .center
titleStackView.spacing = 4.0
titleStackView.axis = .horizontal
return titleStackView
}()
let kCoverImageSize: CGFloat = 40
// MARK: -
override func setupViews() {
super.setupViews()
self.coverImage.clipsToBounds = true
self.coverImage.frame = CGRect(x: 0, y: 0, width: kCoverImageSize, height: kCoverImageSize)
self.contentView.addSubview(self.coverImage)
self.titleStackView.addArrangedSubview(self.titleLabel)
// Not yet available
self.titleStackView.addArrangedSubview(self.memberLabel)
self.contentView.addSubview(self.titleStackView)
self.contentView.addSubview(self.separatorLine)
self.titleLabel.font = SBUTheme.channelCellTheme.titleFont
self.titleLabel.textColor = SBUTheme.channelCellTheme.titleTextColor
self.separatorLine.backgroundColor = SBUTheme.channelCellTheme.separatorLineColor
self.memberLabel.textColor = .gray
}
override func setupAutolayout() {
super.setupAutolayout()
NSLayoutConstraint.activate([
self.coverImage.topAnchor.constraint(
equalTo: self.contentView.topAnchor,
constant: 10
),
self.coverImage.bottomAnchor.constraint(
equalTo: self.contentView.bottomAnchor,
constant: -10
),
self.coverImage.leadingAnchor.constraint(
equalTo: self.contentView.leadingAnchor,
constant: 16
),
self.coverImage.widthAnchor.constraint(equalToConstant: kCoverImageSize),
self.coverImage.heightAnchor.constraint(equalToConstant: kCoverImageSize),
])
NSLayoutConstraint.activate([
self.titleStackView.topAnchor.constraint(
equalTo: self.contentView.topAnchor,
constant: 10
),
self.titleStackView.bottomAnchor.constraint(
equalTo: self.contentView.bottomAnchor,
constant: -10
),
self.titleStackView.leadingAnchor.constraint(
equalTo: self.coverImage.trailingAnchor,
constant: 16
),
self.titleStackView.rightAnchor.constraint(
equalTo: self.contentView.rightAnchor,
constant: -16),
])
self.titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
self.memberLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
NSLayoutConstraint.activate([
// From bottom of cell
self.separatorLine.topAnchor.constraint(
equalTo: self.coverImage.bottomAnchor,
constant: -0.5
),
// From left side
self.separatorLine.leadingAnchor.constraint(
equalTo: self.titleStackView.leadingAnchor,
constant: 0
),
// From right side
self.separatorLine.trailingAnchor.constraint(
equalTo: self.contentView.trailingAnchor,
constant: -16
),
//Line Width
self.separatorLine.heightAnchor.constraint(equalToConstant: 10.0)
])
}
override func configure(channel: SBDBaseChannel) {
super.configure(channel: channel)
self.titleLabel.text = channel.name.count > 0 ? channel.name : "Empty channel"
let ch = channel as! SBDGroupChannel
if let message = ch.lastMessage?.message {
self.memberLabel.text = "'\(message)'"
}
self.coverImage.image = UIImage(named: "kitten")
self.coverImage.layer.cornerRadius = kCoverImageSize/2
self.coverImage.layer.masksToBounds = true
}
}
//
// ChannelListViewController.swift
// uikitsimple
//
// Copyright © 2020 SendBird, Inc. All rights reserved.
//
import UIKit
import SendBirdUIKit
class ChannelListVC: SBUChannelListViewController {
override init(channelListQuery: SBDGroupChannelListQuery? = nil) {
super.init(channelListQuery: channelListQuery)
self.titleView = self.createCustomTitleLabel()
self.leftBarButton = self.createHighlightedBackButton()
self.rightBarButton?.isEnabled = false
// Change empty view
#if swift(>=5.2)
let emptyView = CustomEmptyView()
#else
let emptyView = CustomEmptyView(frame: .zero)
#endif
self.emptyView = emptyView
// CUSTOM CHANNEL CELL
// This part changes the default channel cell to a custom cell.
#if swift(>=5.2)
self.register(channelCell: CustomChannelListCell())
#else
self.register(channelCell: CustomChannelListCell(
style: .default,
reuseIdentifier: CustomChannelListCell.sbu_className)
)
#endif
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let channel = self.channelList[indexPath.row].channelUrl
print(channel)
}
func createCustomTitleLabel() -> UILabel {
let titleLabel = UILabel()
titleLabel.text = "Your Chat List"
return titleLabel
}
@objc func onClickBack() {
print("clicked")
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}
func createHighlightedBackButton() -> UIBarButtonItem {
return UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(onClickBack))
}
}
class ChannelListViewController: UIViewController {
@IBAction func openChat(_ sender: UIButton) {
let listQuery = SBDGroupChannel.createMyGroupChannelListQuery()
listQuery?.includeEmptyChannel = true
listQuery?.includeFrozenChannel = true
// ... You can set more query options
let channelListVC = ChannelListVC(channelListQuery: listQuery)
let naviVC = UINavigationController(rootViewController: channelListVC)
naviVC.modalPresentationStyle = .fullScreen
present(naviVC, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment