<commit_before>import UIKit

class Device {
    fileprivate class DeviceView: UIView, UIKeyInput {
        var app: GameViewController!

        @objc var hasText : Bool {
            return true
        }

        @objc func insertText(_ text: String) {
            app.client.pushAction(OutAction.keyInput)
            app.client.pushString(text)
        }

        @objc func deleteBackward() {

        }

        override var canBecomeFirstResponder : Bool {
            return true
        }
    }

    var pixelRatio: CGFloat = 1
    var lastEvent: UIEvent!
    let onTouchEnded = Signal()
    fileprivate var view: DeviceView!

    class func register(){
        App.getApp().client.onAction(.deviceLog) {
            (reader: Reader) in
            App.getApp().renderer.device!.log(reader.getString())
        }

        App.getApp().client.onAction(.deviceShowKeyboard) {
            App.getApp().renderer.device!.showKeyboard()
        }
        
        App.getApp().client.onAction(.deviceHideKeyboard) {
            App.getApp().renderer.device!.hideKeyboard()
        }
    }

    init() {
        let app: GameViewController = App.getApp()
        self.view = DeviceView(frame: app.view.frame)
        view.app = app
        view.isHidden = true
        app.view.addSubview(view)

        // DEVICE_PIXEL_RATIO
        let pixelRatio = UIScreen.main.scale
        self.pixelRatio = pixelRatio
        app.client.pushAction(OutAction.devicePixelRatio)
        app.client.pushFloat(pixelRatio)

        // DEVICE_IS_PHONE
        let isPhone = UIDevice.current.userInterfaceIdiom == .phone
        app.client.pushAction(OutAction.deviceIsPhone)
        app.client.pushBoolean(isPhone)
    }

    func onEvent(_ event: UIEvent) {
        let app: GameViewController = App.getApp()
        
        let touches = event.allTouches
        guard touches != nil else { return }

        let touch = touches!.first
        guard touch != nil else { return }

        self.lastEvent = event

        switch touch!.phase {
        case .began:
            app.client.pushAction(OutAction.pointerPress)
        case .moved:
            app.client.pushAction(OutAction.pointerMove)
        case .ended, .cancelled:
            onTouchEnded.emit()
            app.client.pushAction(OutAction.pointerRelease)
        default: break
        }

        let location = touch!.location(in: self.view)
        app.client.pushFloat(location.x)
        app.client.pushFloat(location.y)
        app.client.sendData()
    }

    func log(_ val: String) {
        NSLog(val)
    }

    func showKeyboard() {
        let app: GameViewController = App.getApp()
        view.becomeFirstResponder()
        app.client.pushAction(OutAction.deviceKeyboardShow)
    }

    func hideKeyboard() {
        let app: GameViewController = App.getApp()
        app.view.endEditing(true)
        app.client.pushAction(OutAction.deviceKeyboardHide)
    }
}<commit_msg>Added iOS implementation for device keyboard events<commit_after>import UIKit

class Device {
    fileprivate class DeviceView: UIView, UIKeyInput {
        var app: GameViewController!

        @objc var hasText : Bool {
            return true
        }

        @objc func insertText(_ text: String) {
            app.client.pushAction(OutAction.keyInput)
            app.client.pushString(text)
        }

        @objc func deleteBackward() {

        }

        override var canBecomeFirstResponder : Bool {
            return true
        }
    }

    var pixelRatio: CGFloat = 1
    var lastEvent: UIEvent!
    var keyboardVisible = false {
        didSet {
            let app = App.getApp()
            if (keyboardVisible) {
                app.client.pushAction(OutAction.deviceKeyboardShow)
            } else {
                app.client.pushAction(OutAction.deviceKeyboardHide)
            }
        }
    }
    var keyboardHeight: CGFloat = 0 {
        didSet {
            let app = App.getApp()
            app.client.pushAction(OutAction.deviceKeyboardHeight, keyboardHeight)
        }
    }
    let onTouchEnded = Signal()
    fileprivate var view: DeviceView!

    class func register(){
        App.getApp().client.onAction(.deviceLog) {
            (reader: Reader) in
            App.getApp().renderer.device!.log(reader.getString())
        }

        App.getApp().client.onAction(.deviceShowKeyboard) {
            App.getApp().renderer.device!.showKeyboard()
        }

        App.getApp().client.onAction(.deviceHideKeyboard) {
            App.getApp().renderer.device!.hideKeyboard()
        }
    }

    init() {
        let app: GameViewController = App.getApp()
        self.view = DeviceView(frame: app.view.frame)
        view.app = app
        view.isHidden = true
        app.view.addSubview(view)

        // DEVICE_PIXEL_RATIO
        let pixelRatio = UIScreen.main.scale
        self.pixelRatio = pixelRatio
        app.client.pushAction(OutAction.devicePixelRatio)
        app.client.pushFloat(pixelRatio)

        // DEVICE_IS_PHONE
        let isPhone = UIDevice.current.userInterfaceIdiom == .phone
        app.client.pushAction(OutAction.deviceIsPhone)
        app.client.pushBoolean(isPhone)

        // notifications
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func onEvent(_ event: UIEvent) {
        let app: GameViewController = App.getApp()

        let touches = event.allTouches
        guard touches != nil else { return }

        let touch = touches!.first
        guard touch != nil else { return }

        let location = touch!.location(in: view)
        if keyboardVisible && location.y >= view.frame.height - keyboardHeight { return }

        self.lastEvent = event

        switch touch!.phase {
        case .began:
            app.client.pushAction(OutAction.pointerPress)
        case .moved:
            app.client.pushAction(OutAction.pointerMove)
        case .ended, .cancelled:
            onTouchEnded.emit()
            app.client.pushAction(OutAction.pointerRelease)
        default: break
        }

        app.client.pushFloat(location.x)
        app.client.pushFloat(location.y)
        app.client.sendData()
    }

    func log(_ val: String) {
        NSLog(val)
    }

    func showKeyboard() {
        view.becomeFirstResponder()
    }

    func hideKeyboard() {
        let app = App.getApp()
        app.view.endEditing(true)
    }

    @objc func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            if let size = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect {
                keyboardHeight = size.height
            }
        }
        keyboardVisible = true
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        keyboardVisible = false
    }
}
{'fraction_non_alphanumeric': 0.09572649572649573, 'fraction_numerical': 0.00042735042735042735, 'mean_word_length': 2.026293103448276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2951045', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1967, 'n_words': 380}