<commit_before>//
//  CCMikuView.swift
//  CocoaChinaPlus
//
//  Created by user on 16/1/24.
//  Copyright © 2016年 zixun. All rights reserved.
//

import UIKit
import JavaScriptCore
import Neon
import RxSwift
import RxCocoa


class CCMikuView: UIView {
    
    private let webview = CCMikuWebView()
    
    private let disposeBag = DisposeBag()
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clearColor()
        self.addSubview(self.webview);
        self.webview.userInteractionEnabled = false;
        
        let pan = UIPanGestureRecognizer()
        self.addGestureRecognizer(pan);
        pan.rx_event
            .subscribeNext {[unowned self] (pan:UIGestureRecognizer) -> Void in
                self.dragMe(pan as! UIPanGestureRecognizer)
            }
            .addDisposableTo(self.disposeBag)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.webview.fillSuperview()
    }
    
    func dragMe(ges:UIPanGestureRecognizer) {
        let dragPoint = ges.locationInView(self.superview)
        self.center = dragPoint;
    }
    
    
    
}


private class CCMikuWebView: UIWebView { //2：42
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.delegate = self
        self.backgroundColor = UIColor.clearColor()
        self.opaque = false;
        
        let url = "http://localhost:8989/miku-dancing.coding.io/index.html"
        self.loadRequest(NSURLRequest(URL: NSURL(string: url)!))
        
        let timer = NSTimer.scheduledTimerWithTimeInterval(2*60 + 42, target: self, selector: Selector("timerAction"), userInfo: nil, repeats: true)
        
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     播放
     */
    func play() {
        self.evaluateScript("control.play()")
    }
    
    /**
     暂停
     */
    func pause() {
        self.evaluateScript("control.pause()")
    }
    
    
    func timerAction() {
        self.play()
    }
    
    private func evaluateScript(string:String) {
        let context = self.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as! JSContext
        context.evaluateScript(string)
    }

}

extension CCMikuWebView: UIWebViewDelegate {
    <commit_msg>fix miku timer bug<commit_after>//
//  CCMikuView.swift
//  CocoaChinaPlus
//
//  Created by user on 16/1/24.
//  Copyright © 2016年 zixun. All rights reserved.
//

import UIKit
import JavaScriptCore
import Neon
import RxSwift
import RxCocoa


class CCMikuView: UIView {
    
    private let webview = CCMikuWebView()
    
    private let disposeBag = DisposeBag()
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clearColor()
        self.addSubview(self.webview);
        self.webview.userInteractionEnabled = false;
        
        let pan = UIPanGestureRecognizer()
        self.addGestureRecognizer(pan);
        pan.rx_event
            .subscribeNext {[unowned self] (pan:UIGestureRecognizer) -> Void in
                self.dragMe(pan as! UIPanGestureRecognizer)
            }
            .addDisposableTo(self.disposeBag)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.webview.fillSuperview()
    }
    
    func dragMe(ges:UIPanGestureRecognizer) {
        let dragPoint = ges.locationInView(self.superview)
        self.center = dragPoint;
    }
    
    
    
}


private class CCMikuWebView: UIWebView { //2：42
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.delegate = self
        self.backgroundColor = UIColor.clearColor()
        self.opaque = false;
        
        let url = "http://localhost:8989/miku-dancing.coding.io/index.html"
        self.loadRequest(NSURLRequest(URL: NSURL(string: url)!))
        let timer = NSTimer.scheduledTimerWithTimeInterval(2*60 + 42, target: self, selector: "timerAction", userInfo: nil, repeats: true)
        
    }
    
    //@objc的用处 Swift中的NSTimer不能调用私有方法(包括target是private这种情况)，加入@objc后则可以
    @objc func timerAction() {
        self.play()
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     播放
     */
    func play() {
        self.evaluateScript("control.play()")
    }
    
    /**
     暂停
     */
    func pause() {
        self.evaluateScript("control.pause()")
    }
    
    
    private func evaluateScript(string:String) {
        let context = self.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as! JSContext
        context.evaluateScript(string)
    }

}

extension CCMikuWebView: UIWebViewDelegate {
    
{'fraction_non_alphanumeric': 0.10353817504655494, 'fraction_numerical': 0.00782122905027933, 'mean_word_length': 2.17810650887574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3087114', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1711, 'n_words': 314}