This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitleBar.js
133 lines (117 loc) · 3.48 KB
/
TitleBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2018 DoubleDutch, Inc.
*
* 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.
*/
import React, { Component } from 'react'
import { Platform, StyleSheet, Text, View } from 'react-native'
import Color from './Color'
const topSpaceHeight = 21
const barHeight = 43
let isEmulated
export function setEmulated() { isEmulated = true }
export class TitleBar extends Component {
constructor(props) {
super(props)
this.state = {
isSignedIn: false,
didSigninFail: false,
showStatus: !!props.signin,
title: ''
}
}
setTitle(title) {
if (this.props.client) { // This only works if `client` is specified.
const fullTitle = title + this.statusLightText()
this.props.client.setTitle(fullTitle) // Pass to native title
this.setState({title: fullTitle})
}
}
componentDidUpdate(prevProps) {
if (prevProps.title !== this.props.title) this.setTitle(this.props.title)
}
componentDidMount() {
const { signin, title } = this.props
this.setTitle(title)
if (signin) {
signin
.then(() => {
this.setState({ isSignedIn: true })
this.setTitle(this.props.title)
this.hideStatusTimer = setTimeout(() => {
this.setState( {showStatus: false })
this.setTitle(this.props.title)
}, 5000)
})
.catch(() => {
this.setState({ didSigninFail: true })
this.setTitle(this.props.title)
})
}
}
componentWillUnmount() {
if (this.hideStatusTimer) clearInterval(this.hideStatusTimer)
}
render() {
const { title } = this.state
const { client } = this.props
// This adjustment can be removed entirely when 8.2 has been out for one year (approx January 2020)
const adjustForNavBar = isEmulated || Platform.select({
ios: !client || !(client.clientVersion.major > 8 || (client.clientVersion.major === 8 && client.clientVersion.minor >= 2)),
android: false,
})
return (
<View style={isEmulated ? [s.wholeBarEmulator, this.props.style] : s.wholeBar}>
{ adjustForNavBar && <View style={s.topSpace} /> }
<View style={[s.titleContainer, adjustForNavBar ? s.titleSpace : null]}>
{ isEmulated && <Text style={s.emulatorTitle}>{title}</Text> }
</View>
</View>
)
}
statusLightText () {
const { showStatus, isSignedIn, didSigninFail } = this.state
if (showStatus) {
if (isSignedIn) return ' 🔵'
if (didSigninFail) return ' 🔴'
return ' ⚫'
}
return ''
}
}
const s = StyleSheet.create({
wholeBarEmulator: {
backgroundColor: new Color().rgbString(),
opacity: 0.9,
top: 0,
width: '100%',
zIndex: 1000000
},
wholeBar: {
},
topSpace: {
height: Platform.select({ios: topSpaceHeight, android: 0}),
},
titleSpace: {
height: barHeight,
},
titleContainer: {
justifyContent: 'center',
alignItems: 'center'
},
emulatorTitle: {
textAlign: 'center',
color: 'white',
fontSize: 17
}
})