Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active December 29, 2015 22:49
Show Gist options
  • Save MotiurRahman/7739047 to your computer and use it in GitHub Desktop.
Save MotiurRahman/7739047 to your computer and use it in GitHub Desktop.
Android and IOS: Read/write contents (100 objects) to file and generate Table view. CRUD operation of a file.
/* Here create a file you can perform here read,insert,update,delete that means CURD operation */
var file = require('ui/common/FileView');
var fileView = new file();
fileView.open();
// File create and delete operation here
exports.addValue = function(title, description) {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
f.write(title + '\n' + description + '\n');
};
exports.add = function(title, description) {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
f.write(title + '\n' + description + '\n', true);
};
exports.deleteData = function() {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
f.deleteFile();
};
exports.readData = function() {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
var contents = f.read();
Ti.API.info('Output as a blob: ' + contents);
if ( contents instanceof Object) {
return contents.text;
} else
return '';
};
//This is the main View that shows inserted data in a table view row.
function FileView() {
var win = Ti.UI.createWindow({
layout : 'vertical',
backgroundColor : '#000',
title : 'Local File System',
navBarHidden : false
});
// Create a Label.
var titlelabel = Ti.UI.createLabel({
text : 'Title',
color : '#FFF',
font : {
fontSize : 20
},
height : Ti.UI.SIZE,
width : Ti.UI.FILL,
top : 10,
left : 10,
});
// Add to the parent view.
win.add(titlelabel);
// Create a TextField.
var title = Ti.UI.createTextField({
height : Ti.UI.SIZE,
top : 10,
left : 10,
width : Ti.UI.FILL,
hintText : 'Enter title ',
keyboardType : Ti.UI.KEYBOARD_DEFAULT,
returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
// Add to the parent view.
win.add(title);
// Create a Label.
var descriptionLabel = Ti.UI.createLabel({
text : 'Description:',
color : '#FFF',
font : {
fontSize : 20
},
height : Ti.UI.SIZE,
width : Ti.UI.FILL,
top : 10,
left : 10,
});
// Add to the parent view.
win.add(descriptionLabel);
// Create a TextField.
var description = Ti.UI.createTextField({
height : Ti.UI.SIZE,
top : 10,
left : 10,
width : Ti.UI.FILL,
hintText : 'Enter Description ',
keyboardType : Ti.UI.KEYBOARD_DEFAULT,
returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
// Add to the parent view.
win.add(description);
var actionView = Ti.UI.createView({
height : 50,
width : Ti.UI.FILL,
top : 0
});
win.add(actionView);
// Create a Button.
var save = Ti.UI.createButton({
title : 'save',
height : 50,
width : Ti.UI.SIZE,
left : 10
});
//var data = [];
// Listen for click events.
save.addEventListener('click', function() {
var File = require('lib/file');
//File.add(title.value, description.value);
if (title.value == '' || description.value == '') {
alert('Please fillup Fields');
} else {
if (File.readData().length > 0) {
File.add(title.value, description.value);
} else {
File.addValue(title.value, description.value);
}
Ti.App.fireEvent('tableUpdate');
}
//alert(File.readData());
title.setValue('');
description.setValue('');
title.blur();
});
// Add to the parent view.
actionView.add(save);
// Create a Button.
var update = Ti.UI.createButton({
title : 'update',
height : 50,
width : Ti.UI.SIZE,
right : 10
});
// Listen for click events.
update.addEventListener('click', function() {
var titleVal = Ti.App.Properties.getString('title');
var details = Ti.App.Properties.getString('description');
//alert(title + '\n' + details);
var word = wordArray();
//alert(word.length);
var File = require('lib/file');
if (title.value == '' || description.value == '') {
alert('please update somethings');
} else {
for (var i = 0; i < word.length; i++) {
if (titleVal == word[i].title && details == word[i].description) {
var data = ( {
title : title.value,
description : description.value
});
word.splice(i, 1, data);
var File = require('lib/file');
File.deleteData();
for (var i = 0; i < word.length; i++) {
//alert(word[i].title + '\n' + word[i].description);
if (File.readData().length > 0) {
File.add(word[i].title, word[i].description);
} else {
File.addValue(word[i].title, word[i].description);
}
};
Ti.App.fireEvent('tableUpdate');
title.setValue('');
description.setValue('');
title.blur();
}
};
}
});
// Add to the parent view.
actionView.add(update);
// Create a TableView.
var tableView = Ti.UI.createTableView({
top : 0,
});
win.add(tableView);
var wordArray = function() {
var word = [];
var File = require('lib/file');
var text = File.readData().split('\n');
Ti.API.info('textLength=' + text.length);
for (var i = 0; i < text.length - 1; i = i + 2) {
//Ti.API.info('textValue=' + text[i]);
word.push({
title : text[i],
description : text[i + 1]
});
};
return word;
};
Ti.App.addEventListener('tableUpdate', refresh);
refresh();
function refresh() {
tableData = [];
var word = wordArray();
//alert('function ar modha=' + word.length);
for (var i = 0; i < word.length; i++) {
var row = Ti.UI.createTableViewRow({
className : 'data', // used to improve table performance
selectedBackgroundColor : 'white',
rowIndex : i, // custom property, useful for determining the row during events
height : 'auto',
titleValue : word[i].title,
details : word[i].description,
});
var titleValue = Ti.UI.createLabel({
color : 'red',
font : {
fontFamily : 'Arial',
fontSize : 25,
fontWeight : 'normal'
},
text : word[i].title,
top : 30,
left : 10,
right : 75
});
row.add(titleValue);
var discriptionText = Ti.UI.createLabel({
color : 'red',
font : {
fontFamily : 'Arial',
fontSize : 25,
fontWeight : 'normal'
},
text : word[i].description,
top : 60,
left : 10,
right : 60
});
row.add(discriptionText);
var del = Ti.UI.createButton({
title : 'Del',
right : 2,
height : 40,
id : "delrow",
width : 50,
myrow : row
});
row.add(del);
tableData.push(row);
}
tableView.setData(tableData);
}
tableView.addEventListener('click', function(e) {
//alert('title:' + e.rowData.title + '\nDescription:' + e.rowData.details);
if (e.source.id == "delrow") {
var word = wordArray();
//alert(word.length);
var File = require('lib/file');
for (var i = 0; i < word.length; i++) {
if (e.rowData.titleValue == word[i].title && e.rowData.details == word[i].description) {
word.splice(i, 1);
var File = require('lib/file');
File.deleteData();
for (var i = 0; i < word.length; i++) {
//alert(word[i].title + '\n' + word[i].description);
if (File.readData().length > 0) {
File.add(word[i].title, word[i].description);
} else {
File.addValue(word[i].title, word[i].description);
}
};
Ti.App.fireEvent('tableUpdate');
//alert(word.length);
}
};
} else {
title.setValue(e.rowData.titleValue);
description.setValue(e.rowData.details);
Ti.App.Properties.setString('title', e.rowData.titleValue);
Ti.App.Properties.setString('description', e.rowData.details);
}
});
// Add to the parent view.
return win;
}
module.exports = FileView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment