/// <summary>Convert from DataReader to Task object</summary>
		TodoItem FromReader (SqliteDataReader r) {
			var t = new TodoItem();
			t.ID = Convert.ToInt32 (r ["ID"]);
			t.Name = r ["Name"].ToString ();
			t.Notes = r ["Notes"].ToString ();
			t.Done = Convert.ToInt32 (r ["Done"]) == 1 ? true : false;
			return t;
		}
		public int SaveItem (TodoItem item) 
		{
			lock (locker) {
				if (item.ID != 0) {
					database.Update(item);
					return item.ID;
				} else {
					return database.Insert(item);
				}
			}
		}
		public override bool ShouldOverrideUrlLoading (WebView webView, string url) {
			var scheme = "hybrid:";
			// If the URL is not our own custom scheme, just let the webView load the URL as usual
			if (!url.StartsWith (scheme)) 
				return false;

			// This handler will treat everything between the protocol and "?"
			// as the method name.  The querystring has all of the parameters.
			var resources = url.Substring(scheme.Length).Split('?');
			var method = resources [0];
			var parameters = System.Web.HttpUtility.ParseQueryString(resources[1]);


			if (method == "ListAll") {
				var model = App.Database.GetItems ().ToList();
				var template = new TodoList () { Model = model };
				var page = template.GenerateString ();
				webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
			}
			else if (method == "AddTask") {
				var template = new TodoView () { Model = new TodoItem() };
				var page = template.GenerateString ();
				webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
			}
			else if (method == "ViewTask") {
				var id = parameters ["todoid"];
				var model = App.Database.GetItem (Convert.ToInt32 (id));
				var template = new TodoView () { Model = model };
				var page = template.GenerateString ();
				webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
			} 
			else if (method == "SpeakAll") {
				var todos = App.Database.GetItemsNotDone ();
				var tospeak = "";
				foreach (var t in todos)
					tospeak += t.Name + " ";
				if (tospeak == "")
					tospeak = "there are no tasks to do";
				if (speech == null)
					speech = new Speech ();
				speech.Speak (context, tospeak);
			} else if (method == "TextAll" || method == "TweetAll") {

				var todos = App.Database.GetItemsNotDone ();
				var totext = "";
				foreach (var t in todos)
					totext += t.Name + ",";
				if (totext == "")
					totext = "there are no tasks to share";

				try {
					var intent = new Intent(Intent.ActionSend);
					intent.PutExtra(Intent.ExtraText,totext);
					intent.SetType("text/plain");
					context.StartActivity(Intent.CreateChooser(intent, "Undone Todos"));
				} catch(Exception ex) {
					System.Diagnostics.Debug.WriteLine (ex);
				}

			} else if (method == "TodoView") {
				// the editing form
				var button = parameters ["Button"];
				if (button == "Save") {
					var id = parameters ["id"];
					var name = parameters ["name"];
					var notes = parameters ["notes"];
					var done = parameters ["done"];

					var todo = new TodoItem {
						ID = Convert.ToInt32 (id),
						Name = name,
						Notes = notes,
						Done = (done == "on")
					};

					App.Database.SaveItem (todo);

					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
				} else if (button == "Delete") {
					var id = Convert.ToInt32 (parameters ["id"]);

					App.Database.DeleteItem (new TodoItem {ID = id});

					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
				} else if (button == "Cancel") {
					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
				} else if (button == "Speak") {
					var name = parameters ["name"];
					var notes = parameters ["notes"];
					if (speech == null)
						speech = new Speech ();
					speech.Speak (context, name + " " + notes);
				}
			}


			return true;
		}
		bool HandleShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType) {
			var scheme = "hybrid:";
			// If the URL is not our own custom scheme, just let the webView load the URL as usual
			if (request.Url.Scheme != scheme.Replace(":", ""))
				return true;

			// This handler will treat everything between the protocol and "?"
			// as the method name.  The querystring has all of the parameters.
			var resources = request.Url.ResourceSpecifier.Split('?');
			var method = resources [0];
			var parameters = System.Web.HttpUtility.ParseQueryString(resources[1]); // breaks if ? not present (ie no params)

			if (method == "ListAll") {
				var model = App.Database.GetItems ().ToList();
				var template = new TodoList () { Model = model };
				var page = template.GenerateString ();
				webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
			}
			else if (method == "AddTask") {
				var template = new TodoView () { Model = new TodoItem() };
				var page = template.GenerateString ();
				webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
			}
			else if (method == "ViewTask") {
				var id = parameters ["todoid"];
				var model = App.Database.GetItem (Convert.ToInt32 (id));
				var template = new TodoView () { Model = model };
				var page = template.GenerateString ();
				webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
			} 
			else if (method == "SpeakAll") {
				var todos = App.Database.GetItemsNotDone ();
				var tospeak = "";
				foreach (var t in todos)
					tospeak += t.Name + " ";
				if (tospeak == "")
					tospeak = "there are no tasks to do";
				Speech.Speak (tospeak);
			} else if (method == "TweetAll") {
				var todos = App.Database.GetItemsNotDone ();
				var totweet = "";
				foreach (var t in todos)
					totweet += t.Name + ",";
				if (totweet == "")
					totweet = "there are no tasks to tweet";
				else 
					totweet = "Still do to:" + totweet;
				var tweetController = new TWTweetComposeViewController ();
				tweetController.SetInitialText (totweet); 
				PresentModalViewController (tweetController, true);
			} else if (method == "TextAll") {
				if (MFMessageComposeViewController.CanSendText) {

					var todos = App.Database.GetItemsNotDone ();
					var totext = "";
					foreach (var t in todos)
						totext += t.Name + ",";
					if (totext == "")
						totext = "there are no tasks to text";

					MFMessageComposeViewController message =
						new MFMessageComposeViewController ();
					message.Finished += (sender, e) => {
						e.Controller.DismissViewController (true, null);
					};
					//message.Recipients = new string[] { receiver };
					message.Body = totext;
					PresentModalViewController (message, true);
				} else {
					new UIAlertView ("Sorry", "Cannot text from this device", null, "OK", null).Show ();
				}
			} else if (method == "TodoView") {
				// the editing form
				var button = parameters ["Button"];
				if (button == "Save") {
					var id = parameters ["id"];
					var name = parameters ["name"];
					var notes = parameters ["notes"];
					var done = parameters ["done"];

					var todo = new TodoItem {
						ID = Convert.ToInt32 (id),
						Name = name,
						Notes = notes,
						Done = (done == "on")
					};

					App.Database.SaveItem (todo);

					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
				} else if (button == "Delete") {
					var id = parameters ["id"];

					App.Database.DeleteItem (Convert.ToInt32 (id));

					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
				} else if (button == "Cancel") {
					var model = App.Database.GetItems ().ToList ();
					var template = new TodoList () { Model = model };
					var page = template.GenerateString ();
					webView.LoadHtmlString (page, NSBundle.MainBundle.BundleUrl);
				} else if (button == "Speak") {
					var name = parameters ["name"];
					var notes = parameters ["notes"];
					Speech.Speak (name + " " + notes);
				}
			}
			return false;
		}
        bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
        {
            var scheme = "hybrid:";

            // If the URL is not our own custom scheme, just let the webView load the URL as usual
            if (request.Url.Scheme != scheme.Replace(":", ""))
            {
                return(true);
            }

            // This handler will treat everything between the protocol and "?"
            // as the method name.  The querystring has all of the parameters.
            var resources  = request.Url.ResourceSpecifier.Split('?');
            var method     = resources [0];
            var parameters = System.Web.HttpUtility.ParseQueryString(resources[1]);             // breaks if ? not present (ie no params)

            if (method == "ListAll")
            {
                var model    = App.Database.GetItems().ToList();
                var template = new TodoList()
                {
                    Model = model
                };
                var page = template.GenerateString();
                webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
            }
            else if (method == "AddTask")
            {
                var template = new TodoView()
                {
                    Model = new TodoItem()
                };
                var page = template.GenerateString();
                webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
            }
            else if (method == "ViewTask")
            {
                var id       = parameters ["todoid"];
                var model    = App.Database.GetItem(Convert.ToInt32(id));
                var template = new TodoView()
                {
                    Model = model
                };
                var page = template.GenerateString();
                webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
            }
            else if (method == "SpeakAll")
            {
                var todos   = App.Database.GetItemsNotDone();
                var tospeak = "";
                foreach (var t in todos)
                {
                    tospeak += t.Name + " ";
                }
                if (tospeak == "")
                {
                    tospeak = "there are no tasks to do";
                }
                Speech.Speak(tospeak);
            }
            else if (method == "TweetAll")
            {
                var todos   = App.Database.GetItemsNotDone();
                var totweet = "";
                foreach (var t in todos)
                {
                    totweet += t.Name + ",";
                }
                if (totweet == "")
                {
                    totweet = "there are no tasks to tweet";
                }
                else
                {
                    totweet = "Still do to:" + totweet;
                }
                var tweetController = new TWTweetComposeViewController();
                tweetController.SetInitialText(totweet);
                PresentModalViewController(tweetController, true);
            }
            else if (method == "TextAll")
            {
                if (MFMessageComposeViewController.CanSendText)
                {
                    var todos  = App.Database.GetItemsNotDone();
                    var totext = "";
                    foreach (var t in todos)
                    {
                        totext += t.Name + ",";
                    }
                    if (totext == "")
                    {
                        totext = "there are no tasks to text";
                    }

                    MFMessageComposeViewController message =
                        new MFMessageComposeViewController();
                    message.Finished += (sender, e) => {
                        e.Controller.DismissViewController(true, null);
                    };
                    //message.Recipients = new string[] { receiver };
                    message.Body = totext;
                    PresentModalViewController(message, true);
                }
                else
                {
                    new UIAlertView("Sorry", "Cannot text from this device", null, "OK", null).Show();
                }
            }
            else if (method == "TodoView")
            {
                // the editing form
                var button = parameters ["Button"];
                if (button == "Save")
                {
                    var id    = parameters ["id"];
                    var name  = parameters ["name"];
                    var notes = parameters ["notes"];
                    var done  = parameters ["done"];

                    var todo = new TodoItem {
                        ID    = Convert.ToInt32(id),
                        Name  = name,
                        Notes = notes,
                        Done  = (done == "on")
                    };

                    App.Database.SaveItem(todo);

                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
                }
                else if (button == "Delete")
                {
                    var id = Convert.ToInt32(parameters ["id"]);

                    App.Database.DeleteItem(new TodoItem {
                        ID = id
                    });

                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
                }
                else if (button == "Cancel")
                {
                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadHtmlString(page, NSBundle.MainBundle.BundleUrl);
                }
                else if (button == "Speak")
                {
                    var name  = parameters ["name"];
                    var notes = parameters ["notes"];
                    Speech.Speak(name + " " + notes);
                }
            }
            return(false);
        }
		public TodoItem GetItem(int id) 
		{
			var t = new TodoItem();
			lock (locker) {
				connection = new SqliteConnection ("Data Source=" + path);
				connection.Open ();
				using (var command = connection.CreateCommand ()) {
					command.CommandText = "SELECT [ID], [Name], [Notes], [Done] from [TodoItem] WHERE [ID] = ?";
					command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = id });
					var r = command.ExecuteReader ();
					while (r.Read ()) {
						t = FromReader (r);
						break;
					}
				}
				connection.Close ();
			}
			return t;
		}
		public int SaveItem(TodoItem item) 
		{
			int r;
			lock (locker) {
				if (item.ID != 0) {
					connection = new SqliteConnection ("Data Source=" + path);
					connection.Open ();
					using (var command = connection.CreateCommand ()) {
						command.CommandText = "UPDATE [TodoItem] SET [Name] = ?, [Notes] = ?, [Done] = ? WHERE [ID] = ?;";
						command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Name });
						command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Notes });
						command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.Done });
						command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.ID });
						r = command.ExecuteNonQuery ();
					}
					connection.Close ();
					return r;
				} else {
					connection = new SqliteConnection ("Data Source=" + path);
					connection.Open ();
					using (var command = connection.CreateCommand ()) {
						command.CommandText = "INSERT INTO [TodoItem] ([Name], [Notes], [Done]) VALUES (? ,?, ?)";
						command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Name });
						command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Notes });
						command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.Done });
						r = command.ExecuteNonQuery ();
					}
					connection.Close ();
					return r;
				}

			}
		}
Exemple #8
0
        public override bool ShouldOverrideUrlLoading(WebView webView, string url)
        {
            var scheme = "hybrid:";

            // If the URL is not our own custom scheme, just let the webView load the URL as usual
            if (!url.StartsWith(scheme))
            {
                return(false);
            }

            // This handler will treat everything between the protocol and "?"
            // as the method name.  The querystring has all of the parameters.
            var resources  = url.Substring(scheme.Length).Split('?');
            var method     = resources [0];
            var parameters = System.Web.HttpUtility.ParseQueryString(resources[1]);


            if (method == "ListAll")
            {
                var model    = App.Database.GetItems().ToList();
                var template = new TodoList()
                {
                    Model = model
                };
                var page = template.GenerateString();
                webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
            }
            else if (method == "AddTask")
            {
                var template = new TodoView()
                {
                    Model = new TodoItem()
                };
                var page = template.GenerateString();
                webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
            }
            else if (method == "ViewTask")
            {
                var id       = parameters ["todoid"];
                var model    = App.Database.GetItem(Convert.ToInt32(id));
                var template = new TodoView()
                {
                    Model = model
                };
                var page = template.GenerateString();
                webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
            }
            else if (method == "SpeakAll")
            {
                var todos   = App.Database.GetItemsNotDone();
                var tospeak = "";
                foreach (var t in todos)
                {
                    tospeak += t.Name + " ";
                }
                if (tospeak == "")
                {
                    tospeak = "there are no tasks to do";
                }
                if (speech == null)
                {
                    speech = new Speech();
                }
                speech.Speak(context, tospeak);
            }
            else if (method == "TextAll" || method == "TweetAll")
            {
                var todos  = App.Database.GetItemsNotDone();
                var totext = "";
                foreach (var t in todos)
                {
                    totext += t.Name + ",";
                }
                if (totext == "")
                {
                    totext = "there are no tasks to share";
                }

                try {
                    var intent = new Intent(Intent.ActionSend);
                    intent.PutExtra(Intent.ExtraText, totext);
                    intent.SetType("text/plain");
                    context.StartActivity(Intent.CreateChooser(intent, "Undone Todos"));
                } catch (Exception ex) {
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
            else if (method == "TodoView")
            {
                // the editing form
                var button = parameters ["Button"];
                if (button == "Save")
                {
                    var id    = parameters ["id"];
                    var name  = parameters ["name"];
                    var notes = parameters ["notes"];
                    var done  = parameters ["done"];

                    var todo = new TodoItem {
                        ID    = Convert.ToInt32(id),
                        Name  = name,
                        Notes = notes,
                        Done  = (done == "on")
                    };

                    App.Database.SaveItem(todo);

                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
                }
                else if (button == "Delete")
                {
                    var id = Convert.ToInt32(parameters ["id"]);

                    App.Database.DeleteItem(new TodoItem {
                        ID = id
                    });

                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
                }
                else if (button == "Cancel")
                {
                    var model    = App.Database.GetItems().ToList();
                    var template = new TodoList()
                    {
                        Model = model
                    };
                    var page = template.GenerateString();
                    webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
                }
                else if (button == "Speak")
                {
                    var name  = parameters ["name"];
                    var notes = parameters ["notes"];
                    if (speech == null)
                    {
                        speech = new Speech();
                    }
                    speech.Speak(context, name + " " + notes);
                }
            }


            return(true);
        }
		public int DeleteItem(TodoItem item)
		{
			return database.DeleteItem(item.ID);
		}
		public int SaveItem (TodoItem item)
		{
			return database.SaveItem(item);
		}
Exemple #11
0
 public int DeleteItem(TodoItem item)
 {
     return(database.DeleteItem(item.ID));
 }
Exemple #12
0
 public int SaveItem(TodoItem item)
 {
     return(database.SaveItem(item));
 }