Skip to content

Instantly share code, notes, and snippets.

@rethab
Last active August 29, 2015 14:15
Show Gist options
  • Save rethab/bdd6e8d38e54e4376d54 to your computer and use it in GitHub Desktop.
Save rethab/bdd6e8d38e54e4376d54 to your computer and use it in GitHub Desktop.
simple android todo app in php
<?php
// droid is coming from testnow.php
// all this app can do. corresponding functions are
// named do_XXX where XXX is the action
$actions = array('create', 'list', 'done', 'exit');
// database as array. read when started, saved on exit.
$data = null;
// where the database-file is stored
if (is_phone()) {
$filename = '/sdcard/todo.db';
} else {
$filename = '/tmp/todo.db';
}
init_app();
main_loop();
function init_app() {
global $filename;
global $data;
if (file_exists($filename)) {
$contents = file_get_contents($filename);
$lines = explode("\n", $contents);
$data = array_map(function ($line) { return explode(';', $line); }, $lines);
} else {
// file is created on save if inexistent
$data = array();
}
}
function main_loop() {
global $actions;
do {
$action = input('What do you want to do?', format_actions($actions));
$continue = run_action($action);
} while ($continue);
}
function format_actions(array $actions) {
return implode("\n", array_map('ucfirst', $actions));
}
function output($title, $msg) {
if (is_phone()) {
global $droid;
$droid->makeToast($title, $msg);
}
echo "$title\n\n$msg\n";
}
function input($title, $msg) {
if (is_phone()) {
global $droid;
$res = $droid->dialogGetInput($title, $msg);
return $res['result'] == null ? 'exit' : $res['result'];
} else {
return readline("$title\n\n$msg\n");
}
}
function run_action($input) {
global $actions;
$input = trim($input);
$input = strtolower($input);
if (!in_array($input, $actions)) {
output('Error', 'Unknown action \''.$input.'\'');
return true;
} else {
$action = 'do_'.$input;
return $action();
}
}
function do_create(){
global $data;
$title = input('Create', 'Enter a title');
$data[] = array($title, 'F');
return true;
}
function do_list(){
global $data;
if (empty($data)) {
output('List', 'Nothing here..');
} else {
output('List', data_to_string($data));
}
return true;
}
function do_done(){
global $data;
$title = input('Done', 'Which one shall be marked done?');
foreach ($data as $key => $item) {
if ($item[0] == $title) {
$data[$key][1] = 'T';
}
}
return true;
}
function do_exit(){
global $data;
global $filename;
if (!empty($data)) {
$handle = fopen($filename, 'w+');
if (!$handle) die ("failed to save database\n");
$ret = fwrite($handle, data_to_string($data));
if (!$ret) die ("failed to save database\n");
}
output('Exit', 'Saved. Farewell!');
return false;
}
function data_to_string($data) {
$lines = array_map(function ($line) { return implode(';', $line); }, $data);
return implode("\n", $lines);
}
function is_phone() {
return file_exists('/sdcard');
}
//$n = $droid->dialogGetInput('Fuck!', 'How many times have I told you to stay away?');
//echo "after\n";
//$droid->makeToast('Fuck! You\'re right! ' . $n['result'] . ' times!');
//echo "after dem after\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment