Documente Academic
Documente Profesional
Documente Cultură
// CommentCollectionView.swift
import UIKit
import RealmSwift
// MARK: - Variables
var comments : Results<PublicationComment>? {
didSet {
reloadData()
}
}
//
let cellId = "cellId"
var searchDelegate : SearchResultSize?
//
var isSearChing = false
var searchKey = "" {
didSet {
if StringUtils.isStringEmpty(str: searchKey) {
comments = PublicationCommentDAO.getAll(by: (publicationId))
} else {
let predicate = NSPredicate(format: "comment CONTAINS[cd] %@", searchKey)
comments = PublicationCommentDAO.getAll(by: (publicationId))?.filter(predicate)
}
registerForRealmNotification()
}
}
self.delegate = self
self.dataSource = self
self.translatesAutoresizingMaskIntoConstraints = false
backgroundColor = ColorConstant.lighGrayBackground
self.separatorStyle = .none
self.showsVerticalScrollIndicator = false
self.register(CommentCell.self, forCellReuseIdentifier: cellId)
}
return cell
}
return height
}
return defaultHeight
}
if comment.creatorComment?.idUser == ServiceBaseUrl.currentUser.id {
let action = UIAlertController(title: "Voulez vous :", message: "", preferredStyle:
.actionSheet)
action.addAction(copy)
action.addAction(delete)
action.addAction(update)
action.addAction(cancel)
} else {
//
let action = UIAlertController(title: "Voulez vous !", message: "", preferredStyle:
.actionSheet)
//
let report = UIAlertAction(title: "Signaler", style: .default) { (deleteAction) in
}
//
let cancel = UIAlertAction(title: "Annuler", style: .cancel, handler: nil)
//
let copy = UIAlertAction(title: "Copier", style: .default) { (updateAction) in
UIPasteboard.general.string = comment.comment
}
//
if publicationOwner == ServiceBaseUrl.currentUser.id {
}
action.addAction(delete)
action.addAction(block)
}
//
action.addAction(report)
action.addAction(copy)
action.addAction(cancel)
//
NavigationManager.shared.present(viewController: action, animated: true)
}
}
// MARK: - RealmNotification
var commentResultNotificationToken: NotificationToken? = nil
//
// MARK: - RealmNotification
func registerForRealmNotification(){
commentResultNotificationToken = comments?.addNotificationBlock { [weak self]
(changes: RealmCollectionChange) in
break
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
break
}
}
//
deinit {
commentResultNotificationToken?.stop()
}
//
func deregisterFromRealmNotification() {
commentResultNotificationToken?.stop()
}
//
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate:
Bool) {
//Bottom Refresh
if scrollView == self {
} else {
PublicationCommentsService.loadMore(for: publicationId)
}
}
}
}
}
//
// CommentListViewController.swift
//
//
import UIKit
import RealmSwift
protocol SearchResultSize {
func did(get number : Int)
}
//
lazy var cancelSearchButton: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setImage(#imageLiteral(resourceName: "ic_video_close"), for: .normal)
btn.isHidden = true
btn.addTarget(self, action: #selector(cancelSearch), for: .touchUpInside)
return btn
}()
//
let commentTableView : CommentTableView = {
let tableView = CommentTableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.withAppShadow()
tableView.layer.cornerRadius = 5.0
return tableView
}()
// MARK: - Variables
//
override func viewDidLoad() {
super.viewDidLoad()
//
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName:
ColorConstant.yellowAppColor]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String
: AnyObject], for: .normal)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title =
"Annuler"
//
setupViews()
commentTableView.searchDelegate = self
}
func setupViews(){
view.backgroundColor = ColorConstant.lighGrayBackground
view.addSubview(inputViewHolder)
inputViewHolder.heightAnchor.constraint(equalToConstant: 50).isActive = true
inputViewHolder.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive =
true
inputViewHolder.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
bottomInputConstrain = inputViewHolder.bottomAnchor.constraint(equalTo:
view.bottomAnchor)
bottomInputConstrain?.isActive = true
view.addSubview(collectionViewShadowHolder)
collectionViewShadowHolder.topAnchor.constraint(equalTo: view.topAnchor, constant :
16).isActive = true
collectionViewShadowHolder.bottomAnchor.constraint(equalTo:
inputViewHolder.topAnchor , constant : -8).isActive = true
collectionViewShadowHolder.widthAnchor.constraint(equalTo: view.widthAnchor,
constant : -16).isActive = true
collectionViewShadowHolder.centerXAnchor.constraint(equalTo:
view.centerXAnchor).isActive = true
view.addSubview(searchBar)
searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant : 16).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
searchBar.widthAnchor.constraint(equalTo: view.widthAnchor, constant : -16).isActive =
true
searchBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
//
view.addSubview(commentTableView)
commentTableView.topAnchor.constraint(equalTo: searchBar.bottomAnchor, constant :
20).isActive = true
commentTableView.bottomAnchor.constraint(equalTo: inputViewHolder.topAnchor ,
constant : -8).isActive = true
commentTableView.widthAnchor.constraint(equalTo: view.widthAnchor, constant :
-16).isActive = true
commentTableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive =
true
view.addSubview(result)
result.topAnchor.constraint(equalTo: searchBar.bottomAnchor, constant : 4).isActive =
true
result.heightAnchor.constraint(equalToConstant: 20).isActive = true
result.widthAnchor.constraint(equalTo: commentTableView.widthAnchor).isActive = true
result.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
func registerForKeyboardNotifications(){
NotificationCenter.default
.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name:
NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications(){
NotificationCenter.default
.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.inputViewHolder.isUserInteractionEnabled = true
self.inputViewHolder.isUserInteractionEnabled = false
cancelSearchButton.fadeIn()
}
//
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
view.endEditing(true)
}
//
override func handleBackBtnAction(sender: UIBarButtonItem) {
if !self.inputViewHolder.isUserInteractionEnabled {
return
}
super.handleBackBtnAction(sender: sender)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
cancelSearch()
}
func cancelSearch() {
cancelSearchButton.fadeOut()
if !StringUtils.isStringEmpty(str: searchBar.text) {
PublicationCommentsService.load(for: publicationId)
}
searchBar.text = ""
view.endEditing(true)
//
commentTableView.searchKey = ""
isSearching = false
}
import UIKit
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
if textView.text.isEmpty {
textView.text = " Votre commentaire..."
textView.textColor = UIColor.lightGray
}
if text == "\n" {
textView.resignFirstResponder()
return false
}
if text == "@" {
return true
}
}
https://stackoverflow.com/questions/4754392/uiview-with-rounded-corners-and-drop-shadow