Skip to content

Commit

Permalink
add option to add photo to message
Browse files Browse the repository at this point in the history
  • Loading branch information
Topener committed Mar 15, 2019
1 parent 249df01 commit ac9cb31
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
85 changes: 76 additions & 9 deletions controllers/widget.js
Expand Up @@ -5,6 +5,8 @@
* - Display user images
* - Configuration for header titles
* - dynamic box width
* - fix image aspect ratio android
* - fix toolbar android
*/

var calcs = require(WPATH('calcs'));
Expand All @@ -13,7 +15,9 @@ var moment = require('/alloy/moment');

var sections = [];
var messages = {};
var listviewBottom = 60;
var listviewBottom = 50;
var chatWrapperHeight = 50;
var messageImage;

var config = {
meMessageBox: {
Expand Down Expand Up @@ -97,7 +101,6 @@ function onAddMessage(model) {
function onChangeMessage(model){
var sectionIndex = findOrCreateSection(model.attributes.dateTime);
var messagesIndex = messages[model.attributes.day].indexOf(model.attributes.timestamp);
console.warn('indexes', sectionIndex, messagesIndex);
$.listView.sections[sectionIndex].updateItemAt(messagesIndex, generateListItem(model),{animated: true});
}

Expand All @@ -114,6 +117,54 @@ function onRemoveMessage (model) {
$.listView.sections[sectionIndex].deleteItemsAt(messageIndex, 1, {animated: true});
}

function handleGalleryButton() {
if (Ti.Media.hasPhotoGalleryPermissions()) {
selectGalleryImage();
} else {
$.trigger('requestPermission', {
type: 'gallery',
success: selectGalleryImage
});
}
}

function selectGalleryImage() {
Ti.Media.openPhotoGallery({
success: function(e) {
addPhotoToMessage(e.media);
}
});
}


function handleCameraButton() {
if (Ti.Media.hasCameraPermissions()) {
takePhoto();
} else {
$.trigger('requestPermission', {
type: 'camera',
success: takePhoto
});
}
}

function takePhoto() {
Ti.Media.showCamera({
success: function(e) {
addPhotoToMessage(e.media);
}
});
}

function addPhotoToMessage(media) {
$.imageWrapper.height = 100;
chatWrapperHeight += $.imageWrapper.height;
$.chatMessageWrapper.height = chatWrapperHeight + 'dp';
$.messageImage.image = media;
messageImage = media;
setTimeout($.chatBox.focus,1000);
}

function generateListItem(model) {
var textObject = model.attributes.me ? config.meMessageText : config.otherMessageText;
textObject.text = model.attributes.message;
Expand All @@ -123,16 +174,16 @@ function generateListItem(model) {

var template = model.attributes.me ? 'me' : 'other';


var imageObject = {};

if (model.attributes.image) {
template += 'Image';

var localImage = images.getLocalImage(model);


// TODO: move to lib
if (localImage) {
console.warn(localImage.height, localImage.width);

var boxWidth = Ti.Platform.displayCaps.platformWidth;

Expand Down Expand Up @@ -212,8 +263,10 @@ function createSection(dateTime) {
function handleKeyboardChange(e){

var platformHeight = Ti.Platform.displayCaps.platformHeight;
chatWrapperHeight = platformHeight - e.keyboardFrame.y + listviewBottom - (platformHeight !== e.keyboardFrame.y ? window.safeAreaPadding.bottom : 0);
chatWrapperHeight += $.imageWrapper.height;
$.chatMessageWrapper.animate({
height: platformHeight - e.keyboardFrame.y + listviewBottom - (platformHeight !== e.keyboardFrame.y ? window.safeAreaPadding.bottom : 0),
height: chatWrapperHeight,
duration: e.animationDuration * 1000
});

Expand All @@ -223,6 +276,8 @@ function handleKeyboardChange(e){
}, scrollToLastItem);
}



function scrollToLastItem() {
var lastSection = $.listView.sections[$.listView.sections.length - 1];
$.listView.scrollToItem($.listView.sections.length - 1, lastSection.items.length -1, {animated: false});
Expand All @@ -231,7 +286,7 @@ function scrollToLastItem() {
function sendMessage() {
var text = $.chatBox.value;

if (text.length === 0) {
if (text.length === 0 && !$.messageImage.image) {
return false;
}

Expand All @@ -244,8 +299,18 @@ function sendMessage() {
me: true,
message: text,
dateTime: moment(new Date()).format(),
id: new Date().getTime()
id: new Date().getTime(),
};

if (messageImage) {
message.image = messageImage;
messageImage = false;
$.messageImage.image = null;
chatWrapperHeight -= $.imageWrapper.height;
$.imageWrapper.height = 0;
$.chatMessageWrapper.height = chatWrapperHeight + 'dp';

}

exports.addMessage(message);

Expand Down Expand Up @@ -299,8 +364,10 @@ function handleFocus() {
function init() {
window.removeEventListener('postlayout', init);

listviewBottom = 60 + window.safeAreaPadding.bottom;
$.chatMessageWrapper.height = listviewBottom + 'dp';
listviewBottom = 50 + window.safeAreaPadding.bottom;
chatWrapperHeight = listviewBottom;

$.chatMessageWrapper.height = chatWrapperHeight + 'dp';
$.listView.bottom = listviewBottom + 'dp';

if (OS_IOS) Ti.App.addEventListener('keyboardframechanged', handleKeyboardChange);
Expand Down
1 change: 1 addition & 0 deletions lib/images.js
@@ -1,4 +1,5 @@
function getLocalImage(model) {
console.log(model.toJSON());
if (model.attributes.localImage) {
return model.attributes.localImage;
}
Expand Down
34 changes: 27 additions & 7 deletions styles/widget.tss
Expand Up @@ -51,14 +51,15 @@

"#chatMessageWrapper": {
bottom: 0,
height: '60dp',
backgroundColor: "#ddd"
height: '50dp',
backgroundColor: "#ddd",
layout: 'vertical'
}

"#chatBox": {
left: 10,
right: 10,
height: '50dp',
height: '40dp',
top: 5,
backgroundColor: "#fff",
padding: {
Expand All @@ -84,14 +85,33 @@
"#sendMessage": {
width: Ti.UI.SIZE,
touchEnabled: false,
font: {
fontFamily: 'FontAwesome5Free-Solid',
fontSize: 20
},
color: "#fff",
text: '' // fa arrow-circle-up
}

".messageImage": {
top: 0,
}

".fa": {
font: {
fontFamily: 'FontAwesome5Free-Solid',
fontSize: 20
}
}

".toolbarButton": {
color: "#224567",
font: {
fontSize: 24
}
}


"#cameraButton": {
text: "",
}

"#imageButton": {
text: ""
}
24 changes: 21 additions & 3 deletions views/widget.xml
Expand Up @@ -40,9 +40,27 @@
</ListView>

<View id="chatMessageWrapper">
<TextField id="chatBox" onReturn="sendMessage" onChange="handleMessageChange" onFocus="handleFocus" />
<View id="sendMessageWrapper" onClick="sendMessage">
<Label id="sendMessage" />
<View id="imageWrapper" height="0" backgroundColor="#fff" top="2">
<ImageView id="messageImage" height="95" top="3" left="10" />
</View>

<View>
<TextField id="chatBox" onReturn="sendMessage" onChange="handleMessageChange" onFocus="handleFocus">
<KeyboardToolbar>
<Toolbar id="toolbar">
<Items>
<Label class="fa toolbarButton" id="cameraButton" onClick="handleCameraButton" />
<Button type="FIXED_SPACE" width="20" />
<Label class="fa toolbarButton" id="imageButton" onClick="handleGalleryButton" />
<Button type="FLEXIBLE_SPACE" />
</Items>
</Toolbar>
</KeyboardToolbar>
</TextField>

<View id="sendMessageWrapper" onClick="sendMessage">
<Label id="sendMessage" class="fa" />
</View>
</View>
</View>
</Alloy>

0 comments on commit ac9cb31

Please sign in to comment.