-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
265 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
Examples | ||
Examples | ||
screenshots | ||
.travis.yml | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {Image} from 'react-native'; | ||
|
||
const ImageAvatar = (props) => { | ||
const { | ||
src, | ||
size, | ||
imageStyle, | ||
borderRadius, | ||
} = props; | ||
|
||
const imageDefaultStyle = { | ||
borderRadius: borderRadius ? borderRadius : (size * 0.5), | ||
width: size, | ||
height: size, | ||
}; | ||
|
||
const newProps = { | ||
style: [imageDefaultStyle, imageStyle], | ||
source: {uri: src}, | ||
}; | ||
|
||
return React.createElement(Image, newProps); | ||
}; | ||
|
||
ImageAvatar.propTypes = { | ||
src: PropTypes.string, | ||
size: PropTypes.number, | ||
imageStyle: PropTypes.object, | ||
borderRadius: PropTypes.number, | ||
}; | ||
|
||
export default ImageAvatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import {View, Text} from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import {abbr} from '../helpers'; | ||
|
||
const TextAvatar = (props) => { | ||
const { | ||
name, | ||
size, | ||
textColor, | ||
} = props; | ||
|
||
const textContainerStyle = { | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
marginTop: -(size / 20), | ||
height: size, | ||
width: size, | ||
}; | ||
|
||
return ( | ||
<View style={textContainerStyle}> | ||
<Text | ||
style={{ | ||
color: textColor, | ||
fontSize: size / 2.5, | ||
}} | ||
adjustsFontSizeToFit={true} | ||
> | ||
{abbr(name)} | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
TextAvatar.propTypes = { | ||
name: PropTypes.string, | ||
size: PropTypes.number, | ||
textColor: PropTypes.string, | ||
}; | ||
|
||
export default TextAvatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import ImageAvatar from './ImageAvatar'; | ||
import TextAvatar from './TextAvatar'; | ||
|
||
export {ImageAvatar, TextAvatar}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import initials from 'initials'; | ||
|
||
export function abbr(name) { | ||
let abbr = initials(name); | ||
if (name.startsWith('+')) { | ||
abbr = `+${ abbr }`; | ||
} | ||
if (!abbr) abbr = name; | ||
return abbr; | ||
} | ||
|
||
export function sumChars(str) { | ||
let sum = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
sum += str.charCodeAt(i); | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
export async function fetchImage(src) { | ||
try { | ||
const fetchCall = await fetch(src); | ||
if (fetchCall.headers.map['content-type'].startsWith('image/')) { | ||
return true; | ||
} else { | ||
console.warn('Online fetched source is not a supported image'); | ||
return false; | ||
} | ||
} catch (err) { | ||
console.warn('Error fetching source, falling back to initials', err); | ||
return false; | ||
} | ||
} | ||
|
||
export function generateBackgroundStyle(name, bgColor, bgColors) { | ||
let background; | ||
if (bgColor) { | ||
background = bgColor; | ||
} else { | ||
// Pick a deterministic color from the list | ||
const i = sumChars(name) % bgColors.length; | ||
background = bgColors[i]; | ||
} | ||
return {backgroundColor: background}; | ||
} | ||
|
||
export function getContainerStyle(size, src, borderRadius) { | ||
return { | ||
borderRadius: borderRadius ? borderRadius : (size * 0.5), | ||
borderWidth: src ? 0 : 1, | ||
borderColor: 'transparent', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.