feat(watchos): emit app context

(cherry picked from commit 6ad851b)
This commit is contained in:
paweldomas 2017-09-27 20:24:46 -05:00
parent 0ef4be6e9e
commit ea4cd420fa
6 changed files with 152 additions and 6 deletions

View File

@ -70,4 +70,27 @@ class ExtensionDelegate: NSObject, WCSessionDelegate, WKExtensionDelegate {
print("WC Session activated with state: \(activationState.rawValue)")
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("WC SESSION DID RECEIVE APP CONTEXT");
let conferenceURL = applicationContext["conferenceURL"] as? NSString ?? "NULL";
print("CONFERENCE URL \(conferenceURL)");
let micMuted = applicationContext["micMuted"] as? NSNumber ?? -1;
print("MIC MUTED \(micMuted)");
let recentURLs = applicationContext["recentURLs"];
if let recentURLsArray = recentURLs as? NSArray {
for entry in recentURLsArray {
// FIXME possible runtime exception
let entryDict = entry as! NSDictionary;
let roomURL = entryDict["roomURL"] as? NSString ?? "NULL";
let timestamp = entryDict["timestamp"] as? NSNumber ?? -1;
print("roomURL: \(roomURL) ts: \(timestamp)");
//print("entry \(type(of: entry)) \(type(of: roomURL)) \(type(of: timestamp))");
}
}
}
}

View File

@ -0,0 +1,23 @@
/**
* {
* type: ADD_RECENT_URL,
* recentURLs: Array
* }
*/
export const SET_RECENT_URLS = Symbol('SET_RECENT_URLS');
/**
* {
* type: SET_MIC_MUTED,
* micMuted: boolean
* }
*/
export const SET_MIC_MUTED = Symbol('SET_MIC_MUTED');
/**
* {
* type: SET_CONFERENCE_URL,
* conferenceURL: String?
* }
*/
export const SET_CONFERENCE_URL = Symbol('SET_CONFERENCE_URL');

View File

@ -1 +1,2 @@
import './middleware';
import './reducer';

View File

@ -3,16 +3,30 @@
import { Platform } from 'react-native';
import * as watch from 'react-native-watch-connectivity';
import {
SET_CONFERENCE_URL,
SET_MIC_MUTED,
SET_RECENT_URLS
} from './actionTypes';
import { ADD_RECENT_URL, LOADED_RECENT_URLS } from '../../recent';
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT, appNavigate } from '../../app';
import {
CONFERENCE_FAILED,
CONFERENCE_LEFT,
CONFERENCE_WILL_JOIN
CONFERENCE_JOINED,
CONFERENCE_LEFT
} from '../../base/conference';
import {
MEDIA_TYPE as MediaType,
toggleAudioMuted
} from '../../base/media';
import { MiddlewareRegistry } from '../../base/redux';
import { getInviteURL } from '../../base/connection/functions';
import {
isLocalTrackMuted,
TRACK_ADDED,
TRACK_REMOVED,
TRACK_UPDATED
} from '../../base/tracks';
/**
@ -35,6 +49,11 @@ MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
watch.subscribeToWatchState((err, watchState) => {
if (!err) {
console.log('watchState', watchState);
// FIXME that does not seem to help with the initial sync up
// if (watchState === 'Activated') {
// _updateApplicationContext(getState);
// }
} else {
console.log('ERROR getting watchState');
}
@ -60,14 +79,61 @@ MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
break;
}
case APP_WILL_UNMOUNT:
case ADD_RECENT_URL:
case LOADED_RECENT_URLS: {
dispatch({
type: SET_RECENT_URLS,
recentURLs: getState()['features/recent'].entries
});
break;
}
case TRACK_ADDED:
case TRACK_REMOVED:
case TRACK_UPDATED: {
// FIXME Note sure how this will be accurate before the tracks are
// created. If no tracks I guess we should use /base/media state.
const tracks = getState()['features/base/tracks'];
const micMuted = isLocalTrackMuted(tracks, MediaType.AUDIO);
dispatch({
type: SET_MIC_MUTED,
micMuted
});
break;
}
case CONFERENCE_JOINED:
case CONFERENCE_FAILED:
case CONFERENCE_LEFT:
case CONFERENCE_LEFT: {
const { conference } = getState()['features/base/conference'];
// NOTE for some reason 'null' does not update context - must be string
const conferenceURL = conference ? getInviteURL(getState) : 'NULL';
dispatch({
type: SET_CONFERENCE_URL,
conferenceURL
});
break;
case CONFERENCE_WILL_JOIN:
}
// Here list all actions that affect the watch OS application context.
// The reducer should form all those actions into our context structure.
case SET_CONFERENCE_URL:
case SET_MIC_MUTED:
case SET_RECENT_URLS: {
_updateApplicationContext(getState);
break;
}
case APP_WILL_UNMOUNT:
break;
}
return result;
});
function _updateApplicationContext(getState) {
const context = getState()['features/mobile/watchos'];
console.info('UPDATING WATCH CONTEXT', context);
watch.updateApplicationContext(context);
}

View File

@ -0,0 +1,32 @@
import { ReducerRegistry, set } from '../../base/redux';
import {
SET_CONFERENCE_URL, SET_MIC_MUTED,
SET_RECENT_URLS
} from './actionTypes';
const INITIAL_STATE = {
// NOTE for some reason 'null' does not update context
conferenceURL: 'NULL',
micMuted: false,
recentURLs: []
};
/**
* Reduces the Redux actions of the feature features/recording.
*/
ReducerRegistry.register(
'features/mobile/watchos', (state = INITIAL_STATE, action) => {
switch (action.type) {
case SET_CONFERENCE_URL: {
return set(state, 'conferenceURL', action.conferenceURL);
}
case SET_MIC_MUTED: {
return set(state, 'micMuted', action.micMuted);
}
case SET_RECENT_URLS: {
return set(state, 'recentURLs', action.recentURLs);
}
default:
return state;
}
});

View File

@ -20,6 +20,7 @@ MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
switch (action.type) {
case LIB_LOAD_STORAGE_DONE: {
// FIXME this appears to be called when a conference is left
let entries = [];
const recentURLs = window.localStorage.getItem('recentURLs');