Permalink
Browse files

Introducing session-scoped Episode_Manager to handle all episode-spec…

…ific CometActors.
  • Loading branch information...
1 parent f352c49 commit 0fde783a27bbed26b50478fb3ae876567ab0403d Paweł Krupinski committed Nov 1, 2011
@@ -1,14 +1,15 @@
package net.pawel.comet
+import net.pawel.snippet.Season_Link
import net.pawel.model.Episode
+import net.liftweb.http.js.JsCmds
+import net.pawel.lib.{Mark_Episode_Watched, Episode_Manager}
+import xml.{Elem, Text}
import net.liftweb.util.Helpers._
import net.liftweb.http.SHtml._
-import net.liftweb.http.js.JsCmds
-import xml._
-import net.liftweb.http.CometActor
-import net.pawel.snippet.Season_Link
+import net.liftweb.http.{CometListener, CometActor}
-trait Episode_Binding_Comet extends CometActor with Season_Link {
+trait Episode_Binding_Comet extends Season_Link with CometListener {
def bindEpisodesCss(episodes: List[Episode]) = episodes.map(episode =>
".season *" #> episode.season
@@ -20,15 +21,11 @@ trait Episode_Binding_Comet extends CometActor with Season_Link {
& ".series_name *" #> season_link(episode.series, episode.season, episode.series.name)
& ".overview *" #> episode.overview
& ".watched *" #> ajaxCheckbox(episode.watched,
- watched => this ! Mark_Episode_Watched(episode)))
+ watched => Episode_Manager.is ! Mark_Episode_Watched(episode)))
def attachOverview(elem: Elem, overview: String) =
(elem % ("onmouseover" -> ("tooltip.show('" + overview.replace("'", "\\'") + "')"))
% ("onmouseout" -> "tooltip.hide()"))
- case class Mark_Episode_Watched(episode: Episode)
-
- override def lowPriority = {
- case Mark_Episode_Watched(episode) => episode.mark_watched(); reRender()
- }
+ protected def registerWith = Episode_Manager.is
}
@@ -1,11 +1,13 @@
package net.pawel.comet
import net.pawel.model.Series
-import net.liftweb.http.CometActor
import net.pawel.snippet.Series_Link
+import net.liftweb.http.CometActor
+import net.pawel.lib.Updated_Watched
+import net.liftweb.common.Logger
-class List_Episodes extends CometActor with Series_Link with Episode_Binding_Comet {
+class List_Episodes extends CometActor with Series_Link with Episode_Binding_Comet with Logger {
def render = {
val params: Array[String] = name.open_!.split(':')
@@ -17,4 +19,11 @@ class List_Episodes extends CometActor with Series_Link with Episode_Binding_Com
".season" #> season_number &
".episodes *" #> bindEpisodesCss(episodes)
}
+
+ override def lowPriority = {
+ case Updated_Watched(from, to) => {
+ debug("Updated watched received.")
+ reRender()
+ };
+ }
}
@@ -1,10 +1,20 @@
package net.pawel.comet
import net.pawel.snippet.main_page.Episode_Fetching
+import net.liftweb.http.CometActor
+import net.pawel.lib.Updated_Watched
+import net.liftweb.common.Logger
-class Recently_Aired extends Episode_Fetching with Episode_Binding_Comet {
+class Recently_Aired extends CometActor with Episode_Fetching with Episode_Binding_Comet with Logger {
override protected def dontCacheRendering = true
def render = ".episodes *" #> bindEpisodesCss(recently_aired_episodes)
+
+ override def lowPriority = {
+ case Updated_Watched(from, to) => {
+ debug("Updated watched received.")
+ reRender()
+ };
+ }
}
@@ -0,0 +1,30 @@
+package net.pawel.lib
+
+import net.liftweb.http.{ListenerManager, SessionVar}
+import net.pawel.model.Episode
+import net.liftweb.actor.LiftActor
+import net.liftweb.common.Logger
+
+class Episode_Manager extends ListenerManager with LiftActor with Logger {
+ override protected def lowPriority ={
+ case Mark_Episode_Watched(episode) => {
+ val from: Option[Episode] = episode.series.last_watched_episode
+ val to: Option[Episode] = episode.mark_watched()
+ debug("Updating watched episode from " + from + " to " + to)
+ updateListeners(Updated_Watched.from(from).to(to))
+ };
+ }
+
+ protected def createUpdate = None
+}
+
+object Episode_Manager extends SessionVar(new Episode_Manager())
+
+case class Mark_Episode_Watched(episode: Episode)
+
+case class Updated_Watched(from: Option[Episode], to: Option[Episode])
+object Updated_Watched {
+ def from(fromEpisode: Option[Episode]) = new {
+ def to(toEpisode: Option[Episode]) = new Updated_Watched(fromEpisode, toEpisode)
+ }
+}
@@ -16,7 +16,7 @@ class Series extends LongKeyedMapper[Series] with IdPK {
def season(season: Int): List[Episode] = Episode.find_by_series_id_and_season(series_id, season)
def last_watched_episode: Option[Episode] = Episode.findByKey(last_watched_episode_id)
def unmark_last_watched = { last_watched_episode_id(Empty); save(); }
- def mark_last_watched(episode: Episode) = { last_watched_episode_id(episode.id); save(); }
+ def mark_last_watched(episode: Episode) = { last_watched_episode_id(episode.id); save(); episode; }
def delete {
episodes.foreach(_.delete_!)
@@ -62,15 +62,15 @@ class Episode extends LongKeyedMapper[Episode] with IdPK with Ordered[Episode] w
def key:(Long, Int, Int) = (series_id.get, season.get, number.get)
- def mark_watched() {mark_watched(!watched)}
+ def mark_watched(): Option[Episode] = mark_watched(!watched)
- def mark_watched(watched_state: Boolean) {
+ def mark_watched(watched_state: Boolean): Option[Episode] = {
debug("Marking " + this + " watched: " + watched_state)
series.unmark_last_watched
if (watched_state == true) {
- series.mark_last_watched(this)
+ Some(series.mark_last_watched(this))
} else {
- series.episodes.sorted.reverse.find(_ < this).foreach(episode => series.mark_last_watched(episode))
+ series.episodes.sorted.reverse.find(_ < this).map(episode => series.mark_last_watched(episode))
}
}

0 comments on commit 0fde783

Please sign in to comment.