Skip to content

Commit

Permalink
Device refactoring (#98)
Browse files Browse the repository at this point in the history
* Start refactoring: use common object for all platforms

* Implement Device backend for web platform

* Implement device backend for android

* Move location implementation to considered backend

* Fix Location declaration

* Fix build

* Move 'location.js' to 'html' folder

* fixed typo
  • Loading branch information
comrat authored and whoozle committed Jan 15, 2018
1 parent fee64b8 commit 4d303eb
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 88 deletions.
25 changes: 25 additions & 0 deletions core/Device.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Object {
signal propertyUpdated;
property string macAddess;
property string modelName;
property string deviceId;
property string firmware;
property string sdk;
property bool supportingUhd;
property bool supporting3d;

constructor: {
var backend = _globals.core.__deviceBackend
if (!backend)
throw new Error('no backend found')
backend().createDevice(this)
}

onSdkChanged,
onDeviceIdChanged,
onFirmwareChanged,
onMacAddessChanged,
onModelNameChanged,
onSupporting3dChanged,
onSupportingUhdChanged: { this.propertyUpdated() }
}
49 changes: 49 additions & 0 deletions core/Location.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// Window location object
Object {
property string hash; ///< contains current hash value (after '#' charachter)
property string host; ///< current host with port number
property string href; ///< whole current URL
property string port; ///< current port number
property string origin; ///< current protocol, hostname and port number of a URL
property string hostname; ///< current host name
property string pathname; ///< path name of the current URL
property string protocol; ///< current protocol
property string search; ///< query string of the URL
property Object state; ///< current history state

///@private
constructor: {
this.impl = null
this._createLocation()
}

///@private
function _getLocation() {
if (this.impl === null)
this._createPlayer()
return this.impl
}

///@private
function _createLocation() {
if (this.impl)
return this.impl

var backend = _globals.core.__locationBackend
if (!backend)
throw new Error('no backend found')
return this.impl = backend().createLocation(this)
}

pushState(state, title, url): {
var location = this._getLocation()
if (location)
player.pushState(state, title, url)
}

changeHref(href): {
var location = this._getLocation()
if (location)
player.changeHref(href)
}
}
2 changes: 2 additions & 0 deletions platform/android/.core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if (navigator.userAgent.indexOf('Android') >= 0) {
_globals.core.__deviceBackend = function() { return _globals.android.device }

log = console.log.bind(console)

log("Android detected")
Expand Down
10 changes: 0 additions & 10 deletions platform/android/Device.qml

This file was deleted.

9 changes: 9 additions & 0 deletions platform/android/device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var Device = function(ui) {
ui.deviceId = "android_" + Math.random().toString(36).substr(2, 9)
}

exports.createDevice = function(ui) {
return new Device(ui)
}

exports.Device = Device
1 change: 1 addition & 0 deletions platform/html5/.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ else


_globals._backend = function() { return _globals.html5.html }
_globals.core.__locationBackend = function() { return _globals.html5.location }
54 changes: 0 additions & 54 deletions platform/html5/Location.qml

This file was deleted.

47 changes: 47 additions & 0 deletions platform/html5/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Location = function(ui) {
this._ui = ui
var location = window.location
this.updateActualValues()
var self = this
var context = ui._context
context.window.on("hashchange", function() { self._ui.hash = location.hash }.bind(this))
context.window.on("popstate", function() { self.updateActualValues() }.bind(this))
}

Location.prototype.updateActualValues = function() {
var ui = this._ui
var windowContext = ui._context.window.dom
ui.hash = windowContext.location.hash
ui.href = windowContext.location.href
ui.port = windowContext.location.port
ui.host = windowContext.location.host
ui.origin = windowContext.location.origin
ui.hostname = windowContext.location.hostname
ui.pathname = windowContext.location.pathname
ui.protocol = windowContext.location.protocol
ui.search = windowContext.location.search
ui.state = windowContext.history.state
}

Location.prototype.changeHref = function(href) {
this._ui._context.window.dom.location.href = href
this.updateActualValues()
}

Location.prototype.pushState = function(state, title, url) {
var ui = this._ui
var windowContext = ui._context.window.dom
if (windowContext.location.hostname) {
windowContext.history.pushState(state, title, url)
this.updateActualValues()
} else {
ui._context.document.title = title
this.state = state
}
}

exports.createLocation = function(ui) {
return new Location(ui)
}

exports.Location = Location
13 changes: 0 additions & 13 deletions platform/pure/Location.qml

This file was deleted.

2 changes: 2 additions & 0 deletions platform/web/.core.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_globals.core.__deviceBackend = function() { return _globals.web.device }

exports.core.keyCodes = {
13: 'Select',
16: 'Shift',
Expand Down
11 changes: 0 additions & 11 deletions platform/web/Device.qml

This file was deleted.

12 changes: 12 additions & 0 deletions platform/web/device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var Device = function(ui) {
var context = ui._context
var deviceString = context.system.os + "_" + context.system.browser
deviceString = deviceString.replace(/\s/g, '')
ui.deviceId = deviceString + "_" + Math.random().toString(36).substr(2, 9)
}

exports.createDevice = function(ui) {
return new Device(ui)
}

exports.Device = Device

0 comments on commit 4d303eb

Please sign in to comment.