protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            BindService ();

            SetContentView(Resource.Layout.SendMessage);
            ActionBar.SetDisplayHomeAsUpEnabled (true);

            var groupId = Intent.GetIntExtra("groupId", 0);
            _smsGroupRepo = new SmsGroupRepository();
            _smsGroup = _smsGroupRepo.Get (groupId);

            _contactRepo = new ContactRepository(this);
            _recipients = _contactRepo.GetMembersForSmsGroup (groupId);

            FindViewById <TextView>(Resource.Id.recipientGroup).Text = _smsGroup.Name;
            FindViewById <TextView>(Resource.Id.recipients).Text = string.Join (", ", _recipients.Select (c => c.Name));
            FindViewById <ImageButton>(Resource.Id.cmdSend).Click += (sender, e) =>
                {
                    _progressDialog = new ProgressDialog(this);
                    _progressDialog.SetTitle ("Sending Messages");
                    _progressDialog.SetMessage ("Please wait.  Sending message to recipients in group...");

                    Task.Factory
                        .StartNew(() =>
                            QueueMessage ())
                        .ContinueWith(task =>
                            RunOnUiThread(() => {
                                if (task.Result)
                                {
                                    new AlertDialog.Builder(this)
                                        .SetTitle ("Messages Queued")
                                        .SetMessage (string.Format ("Your message was queued to be sent to each recipient of the '{0}' group",
                                                                    _smsGroup.Name))
                                        .SetPositiveButton ("Ok", (o, args) => {
                                                    var homeIntent = new Intent();
                                                    homeIntent.PutExtra ("defaultTab", 0);
                                                    homeIntent.AddFlags (ActivityFlags.ClearTop);
                                                    homeIntent.SetClass (this, typeof(MainActivity));
                                                    StartActivity(homeIntent);
                                                })
                                        .Show ();
                                }
                                else
                                {
                                    new AlertDialog.Builder(this)
                                        .SetTitle ("Message Error")
                                        .SetMessage (string.Format ("Doh!  Your message could not be sent to the '{0}' group.",
                                                                    _smsGroup.Name))
                                        .Show ();
                                }
                                _progressDialog.Dismiss ();
                            }));
                };
        }
        private List<Contact> GetContacts(int groupId)
        {
            _contactRepo = new ContactRepository(this);
            var selectedContacts = _contactRepo.GetMembersForSmsGroup(groupId);
            var contacts = _contactRepo.GetAllMobile ();

            foreach (var selectedContact in selectedContacts)
            {
                var contact = contacts.First(c => c.AddressBookId == selectedContact.AddressBookId);
                contact.Selected = true;
                contact.Id = selectedContact.Id;
            }

            return contacts;
        }
        private void SaveGroup()
        {
            SmsGroup smsGroup;

            //get the selected contacts
            var selectedContacts = _contacts.Where (c => c.Selected);

            _smsRepo = new SmsGroupRepository();
            _contactRepo = new ContactRepository(this);

            var enumerable = selectedContacts as IList<Contact> ?? selectedContacts.ToList();
            if (string.IsNullOrEmpty (GroupName))  //if updating an existing group
            {
                smsGroup = _smsRepo.Get (GroupId);
                smsGroup.MemberCount = enumerable.Count();
                smsGroup.ModifiedDate = DateTime.Now;
                _smsRepo.Save (smsGroup);

                //reset previously selected contacts
                _contactRepo.GetMembersForSmsGroup (GroupId).ForEach (c => {
                    c.Selected = false;
                    c.ModifiedDate = DateTime.Now;
                    _contactRepo.Save (c);
                });
            }
            else  //if new group
            {
                smsGroup = new SmsGroup
                {
                    Name = GroupName,
                    CreatedDate = DateTime.Now,
                    UUID = Guid.NewGuid().ToString(),
                    MemberCount = enumerable.Count()
                };
                _smsRepo.Save (smsGroup);
            }

            foreach (var contact in enumerable)
            {
                if (contact.Id == 0)  //if new contact
                {
                    contact.Selected = true;
                    contact.CreatedDate = DateTime.Now;
                    contact.UUID = Guid.NewGuid ().ToString ();
                    contact.SmsGroupId = smsGroup.Id;
                    _contactRepo.Save(contact);
                }
                else  //if update existing
                {
                    contact.Selected = true;
                    contact.SmsGroupId = smsGroup.Id;
                    contact.ModifiedDate = DateTime.Now;
                    _contactRepo.Save (contact);
                }
            }
        }
Beispiel #4
0
        public override bool OnContextItemSelected(IMenuItem item)
        {
            if (_position < 0) return false;

            switch (item.ItemId)
            {
                case Resource.Id.editSMS:
                    using (var editSmsIntent = new Intent())
                    {
                        editSmsIntent.PutExtra ("groupId", _smsGroups[_position].Id);
                        editSmsIntent.SetClass (Activity, typeof(EditSmsGroupActivity));
                        StartActivity (editSmsIntent);
                    }
                    break;
                case Resource.Id.sendSMS:
                    using (var sendMessage = new Intent())
                    {
                        sendMessage.PutExtra ("groupId", _smsGroups[_position].Id);
                        sendMessage.SetClass (Activity, typeof(SendMessageActivity));
                        StartActivity (sendMessage);
                    }
                    break;
                case Resource.Id.deleteSMS:
                    new AlertDialog.Builder(Activity)
                        .SetTitle ("Delete SMS Group")
                        .SetMessage (string.Format ("Are you sure you want to delete the following group: {0}",
                                                    _smsGroups[_position]))
                        .SetPositiveButton ("Ok", (o, e) => {
                            _progressDialog = new ProgressDialog(Activity);
                            _progressDialog.SetTitle ("Delete SMS Group");
                            _progressDialog.SetMessage ("Please wait.  Deleting SMS Group...");
                            _progressDialog.Show ();

                            Task.Factory
                                .StartNew(() => {
                                    var smsGroup = _smsGroups[_position];

                                    //Delete all messages, group memebers, and then delete sms group
                                    var messageRepo = new SmsMessageRepository();
                                    var messages = messageRepo.GetAllForGroup (smsGroup.Id);
                                    messages.ForEach (messageRepo.Delete);

                                    _contactRepo = new ContactRepository(Activity);
                                    var contacts = _contactRepo.GetMembersForSmsGroup(smsGroup.Id);
                                    contacts.ForEach (c => _contactRepo.Delete (c));

                                    _smsGroupRepo.Delete (smsGroup);
                                })
                                .ContinueWith(task =>
                                    Activity.RunOnUiThread(() => {
                                        _smsGroups.RemoveAt (_position);
                                        ListAdapter = new GroupListAdapter(Activity, _smsGroups);
                                        ((BaseAdapter)ListAdapter).NotifyDataSetChanged ();
                                        _progressDialog.Dismiss ();
                                    }));
                        })
                        .SetNegativeButton ("Cancel", (o, e) => { })
                        .Show ();
                    break;
            }

            return base.OnContextItemSelected (item);
        }