protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SQLiteSleepSessionCommands.CreateDatabase(dbPath); mSleepSessions = SQLiteSleepSessionCommands.FindAllSessions(dbPath); mSleepSessions.Reverse(); mAdapter = new SleepSessionAdapter(mSleepSessions, this); SetContentView(Resource.Layout.Main); mRecyclerView = FindViewById <RecyclerView>(Resource.Id.recyclerView); FindViewById <Button>(Resource.Id.addSessionButton).Click += delegate { var activity = new Intent(this, typeof(AddSessionActivity)); activity.PutExtra("dbPath", dbPath); StartActivityForResult(activity, addSessionRequestCode); }; mAdapter.OnRecyclerViewItemClickAction += ((position) => { var activity = new Intent(this, typeof(ChangeSessionActivity)); activity.PutExtra("id", mSleepSessions[position].ID); StartActivityForResult(activity, changeSessionRequestCode); }); mAdapter.OnRecyclerViewItemLongClickAction += ((position) => { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetTitle("Внимание"); builder.SetMessage("Вы точно хотите удалить эту сессию?"); builder.SetNegativeButton("Отмена", delegate { Toast.MakeText(this, "Действие отменено", ToastLength.Short).Show(); }); builder.SetPositiveButton("Подтвердить", delegate { SQLiteSleepSessionCommands.DeleteSession(dbPath, mSleepSessions[position]); Toast.MakeText(this, "Удалено", ToastLength.Short).Show(); this.Recreate(); }); Dialog dialog = builder.Create(); dialog.Show(); }); mRecyclerView.SetAdapter(mAdapter); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.SetLayoutManager(mLayoutManager); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.ChangeSession); sleepSession = SQLiteSleepSessionCommands.FindSessionById( dbPath, this.Intent.GetIntExtra("id", 0)); FindViewById <TextView>(Resource.Id.startSleepTextView).Text = $"Начало сна:\n{this.sleepSession.StartSleepTime.ToString("G")}"; FindViewById <TextView>(Resource.Id.endSleepTextView).Text = $"Конец сна:\n{this.sleepSession.EndSleepTime.ToString("G")}"; FindViewById <EditText>(Resource.Id.descriptionEditText).Text = this.sleepSession.Caption; FindViewById <Switch>(Resource.Id.isTaskDone).Checked = this.sleepSession.IsTaskDone; var cancelSessionButton = FindViewById <Button>(Resource.Id.cancelSessionButton); cancelSessionButton.Click += delegate { this.SetResult(Result.Canceled); this.Finish(); }; var addSessionButton = FindViewById <Button>(Resource.Id.saveSessionButton); addSessionButton.Click += delegate { sleepSession.Caption = FindViewById <EditText>( Resource.Id.descriptionEditText).Text; sleepSession.IsTaskDone = FindViewById <Switch>( Resource.Id.isTaskDone).Checked; SQLiteSleepSessionCommands.ReplaceData(sleepSession, dbPath); this.Finish(); }; }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.AddSession); var startSessionButton = FindViewById <ImageButton>(Resource.Id.startSleepChooseButton); startSessionButton.Click += delegate { timeSetButtonId = Resource.Id.startSleepChooseButton; TimePickerDialog timePickerDialog = new TimePickerDialog( this, this, DateTime.Now.Hour, DateTime.Now.Minute, true); timePickerDialog.Show(); }; var endSessionButton = FindViewById <ImageButton>(Resource.Id.endSleepChooseButton); endSessionButton.Click += delegate { timeSetButtonId = Resource.Id.endSleepChooseButton; TimePickerDialog timePickerDialog = new TimePickerDialog( this, this, DateTime.Now.Hour, DateTime.Now.Minute, true); timePickerDialog.Show(); }; var cancelSessionButton = FindViewById <Button>(Resource.Id.cancelSessionButton); cancelSessionButton.Click += delegate { this.SetResult(Result.Canceled); this.Finish(); }; string path = Path.Combine(System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal), "pn.dat"); if (File.Exists(path)) { numberStr = File.ReadAllText(path); } var smsSwith = FindViewById <Switch>(Resource.Id.smsRemindSwitch); if (numberStr is null) { smsSwith.Visibility = ViewStates.Invisible; } else { smsSwith.Visibility = ViewStates.Visible; } var addSessionButton = FindViewById <Button>(Resource.Id.saveSessionButton); addSessionButton.Click += delegate { if (sleepSession.StartSleepTime == DateTime.MinValue || sleepSession.EndSleepTime == DateTime.MinValue) { Toast.MakeText(ApplicationContext, "Для добавления требуется указать дату начала и конца сна", ToastLength.Long).Show(); } else { sleepSession.Caption = FindViewById <EditText>(Resource.Id.descriptionEditText).Text; SQLiteSleepSessionCommands.InsertUpdateData(sleepSession, this.Intent.GetStringExtra("dbPath")); var notificationSwitch = FindViewById <Switch>(Resource.Id.remindSwitch); if (notificationSwitch.Checked) { var alarmIntent = new Intent(this, typeof(AlarmReceiver)); alarmIntent.PutExtra("title", "Начало сна"); alarmIntent.PutExtra("message", "Ложитесь спать и не забудьте завести будильник"); var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); var alarmManager = GetSystemService(AlarmService).JavaCast <AlarmManager>(); Java.Util.Calendar calendar = Java.Util.Calendar.Instance; calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(sleepSession.StartSleepTime.Year, sleepSession.StartSleepTime.Month - 1, sleepSession.StartSleepTime.Day, sleepSession.StartSleepTime.Hour, sleepSession.StartSleepTime.Minute, 0); alarmManager.Set(AlarmType.RtcWakeup, calendar.TimeInMillis, pending); } var smsNotificationSwitch = FindViewById <Switch>(Resource.Id.smsRemindSwitch); if (smsNotificationSwitch.Checked) { var alarmIntent = new Intent(this, typeof(PhoneReceiver)); alarmIntent.PutExtra("number", numberStr); alarmIntent.PutExtra("message", "Доброе утро"); var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); var alarmManager = GetSystemService(AlarmService).JavaCast <AlarmManager>(); Java.Util.Calendar calendar = Java.Util.Calendar.Instance; calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(sleepSession.EndSleepTime.Year, sleepSession.EndSleepTime.Month - 1, sleepSession.EndSleepTime.Day, sleepSession.EndSleepTime.Hour, sleepSession.EndSleepTime.Minute, 0); alarmManager.Set(AlarmType.RtcWakeup, calendar.TimeInMillis, pending); } this.SetResult(Result.Ok); this.Finish(); } }; }
public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Resource.Id.action_Delete: { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetTitle("Внимание"); builder.SetMessage("Вы точно хотите удалить все сессии?"); builder.SetNegativeButton("Отмена", delegate { Toast.MakeText(this, "Действие отменено", ToastLength.Short).Show(); }); builder.SetPositiveButton("Подтвердить", delegate { SQLiteSleepSessionCommands.DeleteDatabase(dbPath); Toast.MakeText(this, "Все сессии удалены", ToastLength.Short).Show(); this.Recreate(); }); Dialog dialog = builder.Create(); dialog.Show(); break; } case Resource.Id.action_AddNumber: { Intent intent = new Intent(Intent.ActionPick, ContactsContract.CommonDataKinds.Phone.ContentUri); StartActivityForResult(intent, addPhoneNumberRequestCode); break; } case Resource.Id.action_Export: { string data = SQLiteSleepSessionCommands.ExportDataAsString(dbPath); string tmpFilePath = Path.Combine( this.ApplicationContext.ExternalCacheDir.AbsolutePath, "data.txt"); File.WriteAllText(tmpFilePath, data, Encoding.Default); LayoutInflater li = LayoutInflater.From(this); View exportView = li.Inflate(Resource.Layout.emailExport, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetView(exportView); EditText editText = exportView.FindViewById <EditText>( Resource.Id.emailAddress); builder .SetCancelable(true) .SetPositiveButton("Ок", delegate { string to = editText.Text; Intent i = new Intent(Intent.ActionSend); i.AddFlags(ActivityFlags.GrantReadUriPermission); i.SetType("message/rfc822"); i.PutExtra(Intent.ExtraEmail, new String[] { to }); i.PutExtra(Intent.ExtraSubject, "Экспортируемые данные"); Java.IO.File file = new Java.IO.File(tmpFilePath); file.SetReadable(true, false); Android.Net.Uri uri = Android.Net.Uri.FromFile(file); i.PutExtra(Intent.ExtraStream, uri); try { StartActivity(Intent.CreateChooser(i, "Выберите приложение...")); } catch (ActivityNotFoundException) { Toast.MakeText(this, "Почтовый клиент не установлен", ToastLength.Short).Show(); } }) .SetNegativeButton("Отмена", ((object sender, DialogClickEventArgs e) => { var dialog = sender as AlertDialog; dialog.Cancel(); })); AlertDialog alertDialog = builder.Create(); alertDialog.Show(); break; } default: break; } return(base.OnOptionsItemSelected(item)); }