[tvOS]Scala + PlayでtvOSアプリを作ってみる覚書


## Xcode側

File>New>ProjectでtvOSのSingle View Applicationを新規作成。
以下のファイルを修正。

```swift:AppDelegate.swift
import UIKit
import TVMLKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate {
    var window: UIWindow?
    var appController: TVApplicationController?
    
    static let TVBaseURL = "http://localhost:9000/"
    static let TVBootURL = "\(AppDelegate.TVBaseURL)assets/javascripts/application.js"
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        let appControllerContext = TVApplicationControllerContext()
        
        if let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) {
            appControllerContext.javaScriptApplicationURL = javaScriptURL
        }
        
        appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL
        
        if let launchOptions = launchOptions as? [String: AnyObject] {
            for (kind, value) in launchOptions {
                appControllerContext.launchOptions[kind] = value
            }
        }
        
        appController = TVApplicationController(context: appControllerContext, window: window, delegate: self)
        
        return true
    }
    
    // 略
}
```

## サーバー側

```play new client play-scala```でPlayアプリを新規作成。

![スクリーンショット 2015-11-28 16.21.49.png](https://qiita-image-store.s3.amazonaws.com/0/20722/38a72b2f-90b8-59e9-87af-64c255ea87f5.png "スクリーンショット 2015-11-28 16.21.49.png")

以下のファイルを修正＆追加。

```javascript:public/javascripts/application.js
function getDocument(url) {
    var templateXHR = new XMLHttpRequest();
    templateXHR.responseType = "document";
    templateXHR.addEventListener("load", function() {pushDoc(templateXHR.responseXML);}, false);
    templateXHR.open("GET", url, true);
    templateXHR.send();
    return templateXHR;
}

function pushDoc(document) {
    navigationDocument.pushDocument(document);
}

App.onLaunch = function(options) {
	var url = `${options.BASEURL}alert.tvml`; // 最初に表示するURLを設定。
    getDocument(url);
}
```

```scala:app/controllers/Application.scala
package controllers

import play.api._
import play.api.mvc._

class Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

  def alert = Action {
    Ok(views.html.alert("Alert Test."))
  }

}
```

```xml:app/views/alertTemplate.scala.html
@(title: String)(xml: Html)

<document>
    <alertTemplate>
        <title>@title</title>
        @xml
    </alertTemplate>
</document>
```

```xml:app/views/alert.scala.html
@(message: String)

@alertTemplate("Welcome to Play") {
    <description>@message</description>
    <button>
        <text>OK</text>
    </button>
}
```

```text:conf/routes
# Home page
GET     /                           controllers.Application.index

# Tvml
GET     /alert.tvml                 controllers.Application.alert

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
```

## デバッグ
```play run```した後に、
http://localhost:9000/alert.tvml にブラウザでアクセスすることで、サーバー側のデバッグが割と楽に出来る。