From f433f9ca322315687fbeb5da7dd2041e9a167234 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 2 Nov 2016 15:10:21 +0000 Subject: [PATCH] Move platform-specific functionality into Platform Platform classes are provided by the application via PlatformPeg.set(). --- package.json | 1 - src/BasePlatform.js | 41 ++++++++++++++++++++ src/PlatformPeg.js | 50 +++++++++++++++++++++++++ src/components/structures/MatrixChat.js | 27 ++++--------- 4 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 src/BasePlatform.js create mode 100644 src/PlatformPeg.js diff --git a/package.json b/package.json index 47cc3c2272..e976cc5285 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "draft-js-export-html": "^0.4.0", "draft-js-export-markdown": "^0.2.0", "emojione": "2.2.3", - "favico.js": "^0.3.10", "filesize": "^3.1.2", "flux": "^2.0.3", "fuse.js": "^2.2.0", diff --git a/src/BasePlatform.js b/src/BasePlatform.js new file mode 100644 index 0000000000..2b5e89942e --- /dev/null +++ b/src/BasePlatform.js @@ -0,0 +1,41 @@ +// @flow + +/* +Copyright 2016 Aviral Dasgupta and OpenMarket Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/** + * Base class for classes that provide platform-specific functionality + * eg. Setting an application badge or displaying notifications + * + * Instances of this class are provided by the application. + */ +export default class BasePlatform { + constructor() { + this.notificationCount = 0; + this.errorDidOccur = false; + } + + setNotificationCount(count: number) { + this.notificationCount = count; + } + + setErrorStatus(errorDidOccur: boolean) { + this.errorDidOccur = errorDidOccur; + } + + displayNotification(title: string, msg: string, avatarUrl: string) { + } +} diff --git a/src/PlatformPeg.js b/src/PlatformPeg.js new file mode 100644 index 0000000000..7ebf4db689 --- /dev/null +++ b/src/PlatformPeg.js @@ -0,0 +1,50 @@ +/* +Copyright 2016 OpenMarket Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + * Holds the current Platform object used by the code to do anything + * specific to the platform we're running on (eg. web, electron) + * Platforms are provided by the app layer. + * This allows the app layer to set a Platform without necessarily + * having to have a MatrixChat object + */ +class PlatformPeg { + constructor() { + this.platform = null; + } + + /** + * Returns the current Platform object fir the application. + * This should be an instance of a class extending BasePlatform. + */ + get() { + return this.platform; + } + + /** + * Sets the current platform handler object to use for the + * application. + * This should be an instance of a class extending BasePlatform. + */ + set(plaf) { + this.platform = plaf; + } +} + +if (!global.mxPlatformPeg) { + global.mxPlatformPeg = new PlatformPeg(); +} +module.exports = global.mxPlatformPeg; diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 0f8f5e5de6..9b37871ba9 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -18,9 +18,9 @@ import q from 'q'; var React = require('react'); var Matrix = require("matrix-js-sdk"); -var Favico = require('favico.js'); var MatrixClientPeg = require("../../MatrixClientPeg"); +var PlatformPeg = require("../../PlatformPeg"); var SdkConfig = require("../../SdkConfig"); var Notifier = require("../../Notifier"); var ContextualMenu = require("./ContextualMenu"); @@ -172,7 +172,6 @@ module.exports = React.createClass({ componentWillMount: function() { SdkConfig.put(this.props.config); - this.favicon = new Favico({animation: 'none'}); // Stashed guest credentials if the user logs out // whilst logged in as a guest user (so they can change @@ -639,7 +638,7 @@ module.exports = React.createClass({ var self = this; cli.on('sync', function(state, prevState) { - self.updateFavicon(state, prevState); + self.updateStatusIndicator(state, prevState); if (state === "SYNCING" && prevState === "SYNCING") { return; } @@ -970,7 +969,7 @@ module.exports = React.createClass({ }); }, - updateFavicon: function(state, prevState) { + updateStatusIndicator: function(state, prevState) { var notifCount = 0; var rooms = MatrixClientPeg.get().getRooms(); @@ -984,24 +983,12 @@ module.exports = React.createClass({ notifCount++; } } - try { - // This needs to be in in a try block as it will throw - // if there are more than 100 badge count changes in - // its internal queue - var bgColor = "#d00", - notif = notifCount; - if(state === "ERROR") { - notif = notif || "×"; - bgColor = "#f00"; - } - - this.favicon.badge(notif, { - bgColor: bgColor - }); - } catch (e) { - console.warn("Failed to set badge count: "+e.message); + if (PlatformPeg.get()) { + PlatformPeg.get().setErrorStatus(state === 'ERROR'); + PlatformPeg.get().setNotificationCount(notifCount); } + document.title = `Riot ${state === "ERROR" ? " [offline]" : ""}${notifCount > 0 ? ` [${notifCount}]` : ""}`; },