Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created September 27, 2014 19:47
Show Gist options
  • Save mfikes/d80e8d84191a99ff864c to your computer and use it in GitHub Desktop.
Save mfikes/d80e8d84191a99ff864c to your computer and use it in GitHub Desktop.
(ns classroom-checkout.books-master-view-controller
(:require [fikesfarm.ios.interop :refer [TableViewDataSource TableViewDelegate FetchedResultsControllerDelegate
fetched-results-change-type' table-view-row-animations
user-interface-idiom-ipad? table-view-cell-editing-styles]]
[classroom-checkout.book-detail-view-controller]
[classroom-checkout.database :refer [database-manager create-book! delete-book!]]
[classroom-checkout.utils :refer [full-title]])
(:require-macros [fikesfarm.ios.util :refer [reify export-view-did-load]]))
(def view-controller (atom nil))
(def table-view (atom nil))
(def logout-bar-button-item (atom nil))
(def add-bar-button-item (atom nil))
(def fetched-results-controller (atom nil))
(defn setup-fetched-results-controller-delegate []
(set! (.-cljsDelegate @fetched-results-controller)
(reify FetchedResultsControllerDelegate
(did-change-object-at-index-path-for-change-type-new-index-path
[_ _ section row change-type new-section new-row]
(case (fetched-results-change-type' change-type)
:fetched-results-change-insert (.insertRowInSectionWithRowAnimation @table-view new-row new-section (:table-view-row-animation-fade table-view-row-animations))
:fetched-results-change-delete (.deleteRowInSectionWithRowAnimation @table-view row section (:table-view-row-animation-fade table-view-row-animations))
:fetched-results-change-update (.reloadData @table-view)
:fetched-results-change-move (do
(.deleteRowInSectionWithRowAnimation @table-view row section (:table-view-row-animation-fade table-view-row-animations))
(.insertRowInSectionWithRowAnimation @table-view new-row new-section (:table-view-row-animation-fade table-view-row-animations)))))
(did-change-section-at-index-for-change-type [_ _ section-index change-type]
(case (fetched-results-change-type' change-type)
:fetched-results-change-insert (.insertSectionWithRowAnimation @table-view section-index (:table-view-row-animation-fade table-view-row-animations))
:fetched-results-change-delete (.deleteSectionWithRowAnimation @table-view section-index (:table-view-row-animation-fade table-view-row-animations))))
(will-change-content [_]
(.beginUpdates @table-view))
(did-change-content [_]
(.endUpdates @table-view)))))
(defn setup-table-view-data-source []
(set! (.-dataSource @table-view)
(reify TableViewDataSource
(number-of-rows-in-section [_ section]
(.numberOfObjectsInSection @fetched-results-controller section))
(init-cell-for-row-at-index-path [_ cell section row]
(let [book (.objectAtSectionRow @fetched-results-controller section row)]
(set! (-> cell .-titleLabelWrapper .-text)
(full-title (.-titlePrefix book) (.-title book)))
(.displayCoverForIsbn cell (.-code (first (filter #(.-primaryFlag %) (.eanList book)))))))
(number-of-sections [_]
(.sectionCount @fetched-results-controller))
(title-for-header-in-section [_ section]
(if (zero? section) "Books" "Books"))
(can-edit-row-at-index-path? [_ section _]
true)
(commit-editing-style-for-row-at-index-path [_ editing-style section row]
(when (= (:table-view-cell-editing-style-delete table-view-cell-editing-styles) editing-style)
(let [book (.objectAtSectionRow @fetched-results-controller section row)]
(delete-book! book)
(when (user-interface-idiom-ipad?)
(when (= book @classroom-checkout.book-detail-view-controller/book)
(reset! classroom-checkout.book-detail-view-controller/book nil)
(classroom-checkout.book-detail-view-controller/configure-view!)))))))))
(defn setup-table-view-delegate []
(set! (.-delegate @table-view)
(reify TableViewDelegate
(did-select-row-at-index-path [_ section row]
(reset! classroom-checkout.book-detail-view-controller/book (.objectAtSectionRow @fetched-results-controller section row))
(when (user-interface-idiom-ipad?)
(classroom-checkout.book-detail-view-controller/configure-view!))))))
(defn do-logout-bar-button-item-action! []
(.dismissViewControllerAnimated @view-controller true))
(defn- handle-view-did-load! []
(when @fetched-results-controller
(set! (.-cljsDelegate @fetched-results-controller) nil))
(reset! fetched-results-controller (.createBooksFetchedResultsController @database-manager))
(setup-fetched-results-controller-delegate)
(.performFetch @fetched-results-controller)
(setup-table-view-data-source)
(setup-table-view-delegate)
(.setActionCallback @logout-bar-button-item do-logout-bar-button-item-action!))
(export-view-did-load table-view logout-bar-button-item add-bar-button-item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment