123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // BBSSubjectListViewController.swift
- // O2Platform
- //
- // Created by 刘振兴 on 2016/11/4.
- // Copyright © 2016年 zoneland. All rights reserved.
- //
- import UIKit
- import Alamofire
- import AlamofireImage
- import AlamofireObjectMapper
- import SwiftyJSON
- import ObjectMapper
- import CocoaLumberjack
- class BBSSubjectListViewController: UIViewController {
-
- @IBOutlet weak var subjectTableView: ZLBaseTableView!
-
- var pageModel:SubjectPageModel = SubjectPageModel()
-
- var sectionData:BBSectionListData?
-
- var sectionUrl:String?
-
- var subjects:[BBSSubjectData] = []
-
- var window:UIWindow?
-
- var button:UIButton?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- title = sectionData?.sectionName
- subjectTableView.dataSource = self
- subjectTableView.delegate = self
- subjectTableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
- self.pageModel = SubjectPageModel()
- self.loadFirstData()
- })
-
- subjectTableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {
- //先生成下一页的页号
- if self.pageModel.isLast() == false {
- self.pageModel.nextPage()
- self.loadNextPageData()
- }else{
- self.showSuccess(title: "最后一页了,没有帖子了")
- }
- })
- self.loadFirstData()
- self.createButton()
-
- }
-
- override func viewWillAppear(_ animated: Bool) {
- self.window?.isHidden = false
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- self.window?.isHidden = true
- }
-
- override var preferredStatusBarStyle : UIStatusBarStyle {
- return .lightContent
- }
-
-
-
- func createButton(){
- let width = SCREEN_WIDTH
- let height = SCREEN_HEIGHT
- self.button = UIButton(frame: CGRect(x: 0,y: 0,width: 40,height: 40))
- self.button?.setImage(UIImage(named: "icon_bbs_publish"), for: UIControl.State())
- self.button?.addTarget(self, action: #selector(createAction), for: .touchUpInside)
- self.window = UIWindow(frame: CGRect(x: width - 60, y: height - 60, width: 40, height: 40))
- self.window?.windowLevel = UIWindow.Level.alert + 1
- self.window?.backgroundColor = base_color
- self.window?.layer.cornerRadius = 20
- self.window?.layer.masksToBounds = true
- self.window?.addSubview(self.button!)
- self.window?.makeKeyAndVisible()
- }
-
- @objc func createAction(sender:Any?){
- self.performSegue(withIdentifier: "showCreateSubjectSegue", sender: nil)
- }
-
- func loadFirstData(){
- self.subjects.removeAll()
- let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.subjectFromSectionByPageQuery, parameter: ["##pageNumber##":self.pageModel.pageNumber.toString as AnyObject,"##pageSize##":self.pageModel.pageSize.toString as AnyObject])
- AF.request(url!, method: .put, parameters: ["sectionId":(sectionData?.id)!,"withTopSubject":true], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
- debugPrint("sectionId = \(String(describing: self.sectionData?.id))!")
- debugPrint(response)
- switch response.result {
- case .success(let val):
- let type = JSON(val)["type"]
- let count = JSON(val)["count"]
- //第一次设置总数
- self.pageModel.setPageTotal(count.int!)
- if type == "success" && count > 0 {
- let subjectArray:[BBSSubjectData] = Mapper<BBSSubjectData>().mapArray(JSONString: JSON(val)["data"].description)!
- self.subjects.append(contentsOf: subjectArray)
- }else{
- DDLogError(JSON(val).description)
- }
- case .failure(let err):
- DDLogError(err.localizedDescription)
- }
- self.subjectTableView.reloadData()
- if self.subjectTableView.mj_header.isRefreshing(){
- self.subjectTableView.mj_header.endRefreshing()
- }
- }
- }
-
- func loadNextPageData(){
- let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.subjectFromSectionByPageQuery, parameter: ["##pageNumber##":self.pageModel.pageNumber.toString as AnyObject,"##pageSize##":self.pageModel.pageSize.toString as AnyObject])
- AF.request(url!, method: .put, parameters: ["sectionId":(sectionData?.id)!,"withTopSubject":true], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
- switch response.result {
- case .success(let val):
- let type = JSON(val)["type"]
- let count = JSON(val)["count"]
- if type == "success" && count > 0 {
- let subjectArray:[BBSSubjectData] = Mapper<BBSSubjectData>().mapArray(JSONString: JSON(val)["data"].description)!
- self.subjects.append(contentsOf: subjectArray)
- }else{
- DDLogError(JSON(val).description)
- }
- case .failure(let err):
- DDLogError(err.localizedDescription)
- }
- self.subjectTableView.reloadData()
- if self.subjectTableView.mj_footer.isRefreshing(){
- self.subjectTableView.mj_footer.endRefreshing()
- }
- }
-
- }
-
- @IBAction func UnBackPublishSubjectSuccess(_ segue:UIStoryboardSegue){
- self.loadFirstData()
- }
-
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "showSubjectDetailSegue" {
- let destVC = segue.destination as! BBSSubjectDetailViewController
- destVC.subject = sender as? BBSSubjectData
- }else if segue.identifier == "showCreateSubjectSegue" {
- let destVC = segue.destination as! BBSSubjectCreateTableViewController
- destVC.sectionData = self.sectionData
- }
- }
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
- extension BBSSubjectListViewController:UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return subjects.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell:SubjectTableViewCell = tableView.dequeueReusableCell(withIdentifier: "SubjectTableViewCell", for: indexPath) as! SubjectTableViewCell
- let subject = self.subjects[indexPath.row]
- cell.bbsSubjectData = subject
- return cell
- }
-
- }
- extension BBSSubjectListViewController:UITableViewDelegate {
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: false)
- let subject = subjects[indexPath.row]
- self.performSegue(withIdentifier: "showSubjectDetailSegue", sender: subject)
- }
- }
|