public static void WriteAsync(string Container, string DocumentType, string DocumentID, string Contents) { //BackgroundTask.Start(0, () => //{ // FoodJournal.Model.Data.FoodJournalBackup.Log("Write",Container,DocumentType + "." + DocumentID); try { ThreadSync.WaitOne(); // Wait till we have a sync signal, and clear the signal SessionLog.StartPerformance("Write"); WriteDocument(Container, DocumentType, DocumentID, Contents); } catch (Exception ex) { LittleWatson.ReportException(ex); } finally { SessionLog.EndPerformance("Write"); ThreadSync.Set(); // Set the signal for the next thread to go if it comes to it } SyncQueue.Post(Container, DocumentType, DocumentID, Contents); //}); }
public static void LoadItems(bool Wait, String Xml) { SessionLog.StartPerformance("LoadItems"); try { if (Loading) { if (Wait) { while (Loading) { Thread.Sleep(100); } } else { return; } } //FoodJournalBackup.Log("Load Items",null,null); Loading = true; if (Xml == null) { Xml = LocalDB.Read("Items", "Items", "All"); } if (Xml != null) { Serializer s = Serializer.FromXML(Xml); foreach (var i in s.Select("Item")) { FoodItem item = new FoodItem(i.Key, false); item.LastAmountDB = i.Read("LastAmount"); item.NutritionDB = i.Read("Nutrition"); item.ServingSizesDB = i.Read("ServingSizes"); item.SourceID = i.Read("SourceID"); Cache.MergeItem(item); } } Cache.AllItemsLoaded = true; } catch (Exception ex) { LittleWatson.ReportException(ex); } finally { Loading = false; SessionLog.EndPerformance("LoadItems"); } }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { try { var view = inflater.Inflate(Resource.Layout.fragment_report, container, false); Activity.FindViewById <TabLayout>(Resource.Id.tabs).Visibility = ViewStates.Gone; Activity.FindViewById <FloatingActionButton>(Resource.Id.fab_list).Visibility = ViewStates.Gone; advertisement = view.FindViewById <LinearLayout>(Resource.Id.advertisement); Task.Run(() => { Task.Delay(50).Wait(); Platform.RunSafeOnUIThread("ReportFragment.OnCreateView", () => { items = view.FindViewById <LinearLayout>(Resource.Id.reportitems); int z = 0; foreach (Property p in UserSettings.Current.SelectedProperties) { var chart = inflater.Inflate(Resource.Layout.item_chart, container, false); var titleTextView = chart.FindViewById <TextView>(Resource.Id.chart_title); var propertyView = chart.FindViewById <WeekGraph>(Resource.Id.chart1); p.Color = AndroidUI.GetPropertyColor(Activity, z, p); titleTextView.SetTextColor(p.Color); titleTextView.Text = p.TextOnly; propertyView.property = p; propertyView.BarClick += (sender, e) => { var date = e; Intent i = new Intent(Activity, typeof(FoodJournal.Android15.Activities.ViewDayActivity)); i.PutExtra("date", date.ToStorageStringDate()); i.PutExtra("dateText", (date == DateTime.Now.Date) ? AppResources.Today : date.ToString("dddd")); i.PutExtra("period", (int)Period.Breakfast); StartActivity(i); }; items.AddView(chart); z++; } SessionLog.EndPerformance("Navigate"); }); }); return(view); } catch (Exception ex) { LittleWatson.ReportException(ex); return(null); } }
public static void StartSave() { SessionLog.StartPerformance("Save"); lock (SaveQueue) { if (Saving) { return; } Saving = true; } // save after 0 seconds BackgroundTask.Start(0, () => { try { List <DateTime> queue = new List <DateTime> (); lock (SaveQueue) { queue.AddRange(SaveQueue); SaveQueue.Clear(); } foreach (var dt in queue) { SaveDay(dt); } if (queue.Count > 0) { SaveRecent(); } if (!Cache.AllItemsLoaded) { LoadItems(true, null); } SaveItems(); } finally { lock (SaveQueue) { Saving = false; } SessionLog.EndPerformance("Save"); if (SaveQueue.Count > 0) { StartSave(); } } } ); }
// Threadsafe reading of entire document contents public static string Read(string Container, string DocumentType, string DocumentID) { try { ThreadSync.WaitOne(); // Wait till we have a sync signal, and clear the signal SessionLog.StartPerformance("Read"); return(ReadDocument(Container, DocumentType, DocumentID)); } catch (Exception ex) { LittleWatson.ReportException(ex); } finally { SessionLog.EndPerformance("Read"); ThreadSync.Set(); // Set the signal for the next thread to go if it comes to it } return(null); }
public static void LoadDay(DateTime date) { SessionLog.StartPerformance("LoadDay"); try { string Container = date.ToStorageStringMonth(); string DocumentID = date.ToStorageStringDate(); string Xml = LocalDB.Read(Container, "Day", DocumentID); LoadDay(date, Xml); //FoodJournalBackup.Log("Load Day",date.ToStorageStringFull(),null); //FoodJournalBackup.VerifyDate(date); } catch (Exception ex) { LittleWatson.ReportException(ex); } SessionLog.EndPerformance("LoadDay"); }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { base.OnCreateView(inflater, container, savedInstanceState); Instance = this; Title = Title ?? GetString(Resource.String.Today); var view = inflater.Inflate(Resource.Layout.fragment_pager, container, false); tabLayout = Activity.FindViewById <TabLayout>(Resource.Id.tabs); viewPager = view.FindViewById <ViewPager>(Resource.Id.viewpager); tabLayout.Visibility = ViewStates.Visible; Task.Run(() => { Task.Delay(100).Wait(); Platform.RunSafeOnUIThread("TodayViewFragment.OnCreateView", () => { periodDeleteVM = new PeriodDeleteVM(); periodDeleteVM.DeleteModeChanged += DeleteModeChanged; setupViewPager(DefaultPeriod); SessionLog.EndPerformance("Navigate"); }); }); FloatingActionButton fab = Activity.FindViewById <FloatingActionButton>(Resource.Id.fab_list); fab.SetRippleColor(Resource.Color.ripple_material_dark); fab.Visibility = ViewStates.Visible; fab.Click += (object sender, EventArgs ea) => { try { periodDeleteVM.InDeleteMode = false; actionMode = ((AppCompatActivity)Activity).StartSupportActionMode(this); actionMode.Finish(); // periodDeleteVM.InDeleteMode = false; //GrabAdIntertitials(); ShowNewDialog(); } catch (Exception ex) { LittleWatson.ReportException(ex); } }; return(view); }
public override void OnResume() { base.OnResume(); // contentView.RemoveAllViews(); vm = new GoalsVM(); Task.Run(() => { Task.Delay(70).Wait(); Platform.RunSafeOnUIThread("GoalViewFragment.OnResume", () => { var contentPanel = view.FindViewById <Android.Widget.LinearLayout>(Resource.Id.contentpanel); contentPanel.RequestFocus(); var binding = DataContext <GoalsVM> .FromView(contentPanel); binding.VM = vm; binding.Add(Resource.Id.editText_description, x => x.Description); binding.Add(Resource.Id.editDate, x => x.Date); var editDate = view.FindViewById <Android.Widget.EditText>(Resource.Id.editDate); editDate.Click += (sender, e) => { DateTime goal; if (!DateTime.TryParse(vm.Date, out goal)) { goal = DateTime.Now.Date; } Android.App.DatePickerDialog dpd = new Android.App.DatePickerDialog(Activity, (c, d) => { editDate.Text = d.Date.ToShortDateString(); vm.Date = d.Date.ToShortDateString(); }, goal.Year, goal.Month, goal.Day); dpd.Show(); }; var adapter = new VMListAdapter <GoalLineVM>(Activity, vm.Goals, Resource.Layout.item_goalline, null, (b, gvm) => { b.Add(Resource.Id.text, x => x.Text); b.Add(Resource.Id.value, x => x.Value); b.Add(Resource.Id.image_remove, (a) => { vm.DeleteGoal(a); }); } ); contentView = view.FindViewById <Android.Widget.LinearLayout>(Resource.Id.contentView); contentView.RemoveAllViews(); for (int i = 0; i < adapter.Count; i++) { contentView.AddView(adapter.GetView(i, null, contentView)); } vm.Goals.CollectionChanged += (sender, e) => { contentView.RemoveAllViews(); for (int i = 0; i < adapter.Count; i++) { contentView.AddView(adapter.GetView(i, null, contentView)); } }; var text_button = view.FindViewById <Android.Widget.TextView>(Resource.Id.text_button); text_button.Click += (sender, e) => { List <String> options = vm.NewPropertyOptions; AlertDialog.Builder builder = new AlertDialog.Builder(Activity); builder.SetTitle(Resource.String.add_target) .SetItems(options.ToArray(), (s, e2) => { Property result = StandardProperty.none; String clicked = options[e2.Which]; foreach (var value in Property.All()) { if (value.FullCapitalizedText == clicked) { result = value; } } if (result == StandardProperty.none) { return; } vm.AddGoal(result); }); builder.Create(); builder.Show(); }; SessionLog.EndPerformance("Navigate"); }); }); GrabAd(); }
private static void Pump(Platform.NetworkConnectionType connectionType) { try { if (connectionType == Platform.NetworkConnectionType.None) { return; } if (InHere) { PumpAgain = true; SessionLog.RecordTrace("Attempting to pump while already pumping"); return; } InHere = true; PumpAgain = false; var messages = new List <Message> (); var infomessages = new List <String> (); int totallength = 0; foreach (var item in MessageQueueDB.SelectWhere(m => m.Processed == null)) { var msg = new Message() { Key = item.Id.ToString(), MessageType = item.MessageType, Body = item.Message }; if (totallength + msg.Body.Length > TotalMessageSizeLimit) { if (messages.Count == 0) { messages.Add(msg); infomessages.Add("Msg " + msg.Key + " truncated from " + msg.Body.Length.ToString()); msg.Body = msg.Body.Substring(0, TotalMessageSizeLimit); } PumpAgain = true; break; } else { messages.Add(msg); totallength += msg.Body.Length; #if ANDROID try { // 3/25/15: no more retry for large messages // trying this for Android first, if successfull we may as well apply to WinPhone if (msg.Body.Length > LargeMessage) { item.Processed = DateTime.Now; MessageQueueDB.Update(item); } } catch (Exception ex) { ReportQueueException(ex); } #endif } } if (messages.Count > 0) { foreach (var info in infomessages) { messages.Add(new Message() { Key = "0", MessageType = "Info", Body = info }); } var svc = Services.NewServiceClient(); svc.PushCompleted += (x, y) => { InHere = false; // mark committed messages as processed in the database if (y.Error != null) { ReportQueueException(y.Error); } else { if (y.Result != null) { try { foreach (var result in y.Result) { int i; if (result.Key.Length > 8) { ParseLongResult(result.Key); } else if (int.TryParse(result.Key, out i) && i > 0) { foreach (var m in MessageQueueDB.SelectWhere(m => m.Id == i)) { m.Processed = DateTime.Now; MessageQueueDB.Update(m); } } } } catch (Exception ex) { LittleWatson.ReportException(ex); } } } InHere = false; if (PumpAgain) { StartPump(); } else { SessionLog.EndPerformance("Push"); } }; var upd = AppStats.Current.CultureSettingsLastUpdated.ToString("yyyy/MM/dd"); svc.PushAsync(AppStats.Current.AppInstance, AppStats.Current.Culture, AppStats.Current.Version + "?" + upd, messages); return; } } catch (Exception ex) { ReportQueueException(ex); } InHere = false; return; }
private void setupViewPager() { mainPagerAdapter = new MainPagerAdapter(ChildFragmentManager); int current = 0; List <Property> properties = UserSettings.Current.SelectedProperties; if (properties.Count == 0) { properties.Add(StandardProperty.none); } var props = UserSettings.Current.SelectedProperties; if (props.Count == 1 && props.FirstOrDefault().ID == "00") { var prop = props.FirstOrDefault(); var fragment = new JournalFragment(); Bundle bundle = new Bundle(); bundle.PutString("date", Navigate.selectedDate.ToStorageStringDate()); bundle.PutString("property", prop.ID); fragment.Arguments = bundle; current = mainPagerAdapter.Count; mainPagerAdapter.addFragment(fragment, GetString(Resource.String.NoGoalsTitle)); } else { foreach (var property in props.Where(a => a.ID != "00").ToList()) { var fragment = new JournalFragment(); Bundle bundle = new Bundle(); bundle.PutString("date", Navigate.selectedDate.ToStorageStringDate()); bundle.PutString("property", property.ID); fragment.Arguments = bundle; if (UserSettings.Current.CurrentProperty == property) { current = mainPagerAdapter.Count; } mainPagerAdapter.addFragment(fragment, property.TextOnly); } } try { if (viewPager.Adapter == null) { viewPager.AddOnPageChangeListener(this); } viewPager.Adapter = mainPagerAdapter; tabLayout.SetupWithViewPager(viewPager); tabLayout.TabMode = TabLayout.ModeScrollable; } catch (Exception ex) { throw; } viewPager.SetCurrentItem(current, false); InputMethodManager imm = (InputMethodManager)Activity.GetSystemService(Context.InputMethodService); imm.HideSoftInputFromWindow(viewPager.WindowToken, 0); SessionLog.EndPerformance("Navigate"); }