Move platform-specific functionality into Platform

Platform classes are provided by the application via
PlatformPeg.set().
This commit is contained in:
David Baker 2016-11-02 15:10:21 +00:00
parent f2e59f1bd3
commit f433f9ca32
4 changed files with 98 additions and 21 deletions

View file

@ -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",

41
src/BasePlatform.js Normal file
View file

@ -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) {
}
}

50
src/PlatformPeg.js Normal file
View file

@ -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;

View file

@ -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}]` : ""}`;
},