Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.Graphics;
- using Newtonsoft.Json.Linq;
- using Android.Content.PM;
- using System.Threading;
- using System.Collections.Specialized;
- using StaticHelper;
- using iSuiteMobile;
- using Android.Webkit;
- using Android.Graphics.Drawables;
- using System.Xml;
- using System.Xml.Linq;
- namespace PMGMobile
- {
- [Activity (Label = "Service", Icon="@drawable/pmgmark", WindowSoftInputMode = SoftInput.AdjustPan,
- ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation, Theme = "@style/ApplicationTheme")]
- public class ActivityServiceView : Activity
- {
- private String breadcrumb;
- String itemId;
- LinearLayout formLLO;
- Context context;
- static Dictionary<object, object> viewsToFormItem; // Holds the reference between the screen questions and the underlying form questions
- View activeView = null; // reference to view, the custom LLO for the current selected question during the dialog process
- List<SelectItem> selectListValuesHolder = null; // Holds selected items for active select list
- static Form form;
- Button submitButton;
- Dictionary<Object, Object> emptyRequiredFields;
- ScrollView formScrollView;
- private IValueCallback mUploadMessage;
- private static int FILECHOOSER_RESULTCODE=1;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- context = this;
- //TODO: Fix display for long default text on selects. Getting truncated. Test for long texts on all types.
- //TODO: don't default input to first text input. it's forcing scroll past service description
- viewsToFormItem = new Dictionary<object,object>(); // make sure initialized, since static
- itemId = Intent.GetStringExtra("itemId"); // get passed in category to load
- String itemName = Intent.GetStringExtra("itemName"); // get passed in category to load
- breadcrumb = Intent.GetStringExtra("breadcrumb");
- SetContentView(Resource.Layout.activity_service_view);
- View topView = this.FindViewById(Resource.Id.serviceLLO);
- var itemImageIV = this.FindViewById(Resource.Id.badgeImage) as ImageView;
- TextView itemNameTV = this.FindViewById(Resource.Id.itemName2) as TextView;
- itemNameTV.Text = itemName;
- formScrollView = (ScrollView) this.FindViewById(Resource.Id.formScrollView);
- //Load Service Data, layout base service view layout, and render the form.
- JArray itemData = DataUtility.GetProductData(itemId, false);
- formLLO = (LinearLayout)this.FindViewById(Resource.Id.formLLO);
- WebView descriptionWV = (WebView)this.FindViewById(Resource.Id.description);
- descriptionWV.LoadData(itemData [2].ToString(), "text/html", "utf-8");
- String itemImageUrl = null;
- try
- {
- itemImageUrl = GetFullImageUrl(itemData [3].ToString());
- } catch
- {
- // no image present
- }
- PMGUtilityAndroid.SetImageViewFromURL(this, itemImageIV, itemImageUrl);
- submitButton = (Button) this.FindViewById(Resource.Id.submitButton);
- submitButton.Click+=(sender, e) => {SubmitForm();};
- form = new Form((JArray)itemData [4]);
- form.ShowHideRulesAsXml = (string) itemData[5]; //
- RenderForm(form);
- ProcessShowHide();
- }
- public static void ProcessShowHide()
- {
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- if (formItem.visibilityControlled)
- {
- view.Visibility = ViewStates.Gone;
- formItem.visibility = Visibility.Invisible;
- }
- }
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- if (formItem.visibility == Visibility.Visible)
- {
- if (formItem.type == "radio_set" || formItem.type == "select")
- {
- string selectValue = formItem.SingleSelectValue();
- if (selectValue != null)
- {
- if (formItem.displayRules.ContainsKey(selectValue))
- {
- Dictionary<string, bool> rules = formItem.displayRules[selectValue];
- foreach (KeyValuePair<string, bool> rule in rules)
- {
- form.SetFormItemVisibility(rule.Key, rule.Value);
- }
- }
- }
- }
- }
- }
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- if (formItem.visibility == Visibility.Visible)
- view.Visibility = ViewStates.Visible;
- else
- view.Visibility = ViewStates.Gone;
- }
- }
- void RenderForm(Form form)
- {
- View section=null;
- TextView labelTV = null;
- TextView requiredTV = null;
- if (true)
- {
- // Experimental code to render webview vs. native form
- section = LayoutInflater.Inflate(Resource.Layout.form_instructionaltext, null);
- WebView wv2 = (WebView)section.FindViewById(Resource.Id.webView);
- //wv2.LoadData("Hi", "text/html", "utf-8");
- wv2.SetWebViewClient(new WebViewClient());
- wv2.SetWebChromeClient(new WebChromeClient(){
- // For Android 3.0+
- public void openFileChooser( Android.Webkit.IValueCallback uploadMsg, String acceptType ) {
- mUploadMessage = uploadMsg;
- Intent i = new Intent(Intent.ActionGetContent);
- i.AddCategory(Intent.CategoryOpenable);
- i.SetType("*/*");
- StartActivityForResult(Intent.CreateChooser(i, "File Browser"),
- FILECHOOSER_RESULTCODE);
- }
- //For Android 4.1
- public void openFileChooser(IValueCallback uploadMsg, String acceptType, String capture){
- mUploadMessage = uploadMsg;
- Intent i = new Intent(Intent.ActionGetContent);
- i.AddCategory(Intent.CategoryOpenable);
- i.SetType("image/*");
- StartActivityForResult( Intent.CreateChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
- }
- });
- wv2.Settings.JavaScriptEnabled = true;
- wv2.LoadUrl("http://hascdev7.pmg.net/sc/catalog.product.aspx?product_id=" + itemId + "&showminimal=true");
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = "Another Hi";
- formLLO.AddView(section);
- // viewsToFormItem.Add(section, "Last Hi");
- }
- if (false)
- {
- foreach (FormItem formItem in form.Questions)
- {
- // TODO: pricerule, service price
- // TODO: File
- labelTV = null;
- requiredTV = null;
- if (formItem.type == "instr_text")
- {
- section = LayoutInflater.Inflate(Resource.Layout.form_instructionaltext, null);
- WebView wv = (WebView)section.FindViewById(Resource.Id.webView);
- wv.LoadData(formItem.defaultValue, "text/html", "utf-8");
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = formItem.label;
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type == "text" || formItem.type == "textarea")
- {
- if (formItem.type == "text")
- section = LayoutInflater.Inflate(Resource.Layout.form_shorttext, null);
- else
- section = LayoutInflater.Inflate(Resource.Layout.form_textarea, null);
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = formItem.label;
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type == "section_head")
- {
- section = LayoutInflater.Inflate(Resource.Layout.form_section, null);
- TextView sectionLabel = (TextView)section.FindViewById(Resource.Id.label);
- sectionLabel.Text = formItem.label;
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type == "checkbox")
- {
- section = LayoutInflater.Inflate(Resource.Layout.form_checkbox, null);
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = formItem.label;
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type == "datepicker")
- {
- section = LayoutInflater.Inflate(Resource.Layout.form_date, null);
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = formItem.label;
- //View topParent = GetTopParent(dateButton); // Get the layout that holds this button so its properties can be set later. PMG: RWC built this because I couldn't find another way. Well, turns out this wasn't needed. 'section' can be passed to the click event below.
- section.Click += (sender, e) =>
- {
- ShowDatePicker(sender, e);
- };
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type == "multiselect" || formItem.type == "multicheckbox" || formItem.type == "radio_set" || formItem.type == "select")
- {
- section = LayoutInflater.Inflate(Resource.Layout.form_select, null);
- labelTV = (TextView)section.FindViewById(Resource.Id.label);
- labelTV.Text = formItem.label;
- TextView selectedText = (TextView)section.FindViewById(Resource.Id.selectedText);
- bool hasSelected = false;
- for (int i = 0; i < formItem.selectItems.Count; i++)
- {
- if (formItem.selectItems[i].selected)
- hasSelected = true;
- }
- if (!hasSelected)
- selectedText.Text = "Select...";
- section.Click += (sender, e) =>
- {
- SelectClickLocal(sender);
- };
- // Button selectB = (Button)section.FindViewById(Resource.Id.select_colours);
- // selectB.Click+= (sender,e)=> {selectClick(sender);};
- formLLO.AddView(section);
- viewsToFormItem.Add(section, formItem);
- }
- if (formItem.type != "hidden")
- {
- requiredTV = (TextView)section.FindViewById(Resource.Id.required);
- if (requiredTV != null)
- {
- if (formItem.required)
- requiredTV.Visibility = ViewStates.Visible;
- else
- requiredTV.Visibility = ViewStates.Gone;
- }
- }
- }
- ShowHide();
- } // end if false
- }
- public void ShowHide()
- {
- LoadRules();
- }
- public void LoadRules()
- {
- try
- {
- XDocument rulesXD = XDocument.Parse(form.ShowHideRulesAsXml);
- foreach (XElement q in rulesXD.Descendants("q"))
- {
- // This turns an xml rules document for an entire form into individual dictionary entries for each respective question/FormItem.
- // Each formItem has a dictionary of "values/answers" which each contain a dictionary of show/hide rules and the target question
- Console.WriteLine(q.Document.ToString());
- XAttribute qIDXA = q.Attribute("id");
- string qID = qIDXA.Value.Remove(0, 8); // remove ____dnp_ prefex
- FormItem fi = form.GetFormItemByMP(qID);
- foreach (XElement a in q.Descendants("a"))
- {
- XAttribute vXA = a.Attribute("v");
- string aValue = vXA.Value;
- foreach (XElement qr in a.Descendants("qr"))
- {
- XAttribute idXA = qr.Attribute("id");
- string controlledId = idXA.Value.Remove(0, 8); // remove ____dnp_ prefex;
- XAttribute showXA = qr.Attribute("show");
- string show = showXA.Value;
- bool showRule = (show == "1"); // convert 0 and 1 to false and true;
- if (!fi.displayRules.ContainsKey(aValue))
- fi.displayRules.Add(aValue, new Dictionary<string, bool>()); // if the value entry doesn't exist yet, add a new one
- fi.displayRules[aValue].Add(controlledId, showRule); // append the target question id,rule to the value entry
- form.SetFIVisibilityControlled(controlledId, true);
- Console.WriteLine("qID: " + qID + " aValue: " + aValue + " controlledId: " + controlledId + " show: " + show);
- }
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- // Nothing to do yet. if this errors out, most likely insignificant.
- }
- }
- public bool CheckRequired()
- {
- NameValueCollection parameters = new NameValueCollection();
- List<string> typesToSend = new List<string>() {"text", "textarea", "checkbox", "datepicker", "multiselect", "multicheckbox", "radio_set", "select"};
- emptyRequiredFields = new Dictionary<Object,Object>();
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- if (typesToSend.Contains(formItem.type) && formItem.required && (formItem.visibility != Visibility.Invisible))
- {
- if (formItem.type == "text" || formItem.type == "textarea")
- {
- EditText input = (EditText)view.FindViewById(Resource.Id.input);
- if (input.Text.Length == 0)
- {
- emptyRequiredFields.Add(view, formItem);
- }
- }
- if (formItem.type == "checkbox")
- {
- CheckBox cb = (CheckBox)view.FindViewById(Resource.Id.input);
- if (!cb.Checked)
- emptyRequiredFields.Add(view, formItem);
- }
- if (formItem.type == "datepicker")
- {
- if (formItem.date == null)
- emptyRequiredFields.Add(view, formItem);
- }
- if (formItem.type == "multiselect" || formItem.type == "multicheckbox" || formItem.type == "radio_set" || formItem.type == "select")
- {
- bool hasSelected = false;
- List<string> selectedTexts = new List<string>();
- for (int i = 0; i < formItem.selectItems.Count; i++)
- {
- if (formItem.selectItems[i].selected)
- {
- hasSelected = true;
- break;
- }
- }
- if (!hasSelected)
- emptyRequiredFields.Add(view, formItem);
- }
- }
- }
- if (emptyRequiredFields.Count == 0)
- return true;
- return false;
- }
- public override bool OnCreateOptionsMenu(IMenu menu)
- {
- return PMGDialogAndroid.StandardOptionsMenu(menu);
- }
- public override bool OnOptionsItemSelected(IMenuItem item)
- {
- return PMGDialogAndroid.StandardOptionsItemSelected(this, item);
- }
- private void SelectClickLocal(object o)
- {
- PMGDialogAndroid dialogHelper = new PMGDialogAndroid();
- dialogHelper.context = context;
- dialogHelper.viewsToFormItem = viewsToFormItem;
- dialogHelper.selectListValuesHolder = selectListValuesHolder;
- dialogHelper.selectClick(o);
- }
- public View GetTopParent(View v) // Find the highest level holding view for a view. Typically this is for getting the Layout that holds something like a button.
- {
- View parent = v;
- while (parent.Parent!=null)
- {
- parent = (View)parent.Parent;
- }
- return parent;
- }
- private void ShowDatePicker(object sender, EventArgs e)
- {
- activeView = (View)sender; // set activeView so the callback can find the form item.
- FormItem formItem = (FormItem)viewsToFormItem [activeView];
- DateTime dateToShow;
- if (formItem.date == DateTime.MinValue)
- dateToShow = DateTime.Today;
- else
- dateToShow = formItem.date;
- DatePickerDialog datePickerDialog = new DatePickerDialog(this, OnDateSet, dateToShow.Year, dateToShow.Month, dateToShow.Day);
- datePickerDialog.Show();
- }
- private void OnDateSet(object view, DatePickerDialog.DateSetEventArgs e)
- {
- FormItem formItem = (FormItem)viewsToFormItem [activeView];
- TextView dateDisplay = (TextView)activeView.FindViewById(Resource.Id.dateDisplay);
- DateTime setDate = new DateTime(e.Year, e.MonthOfYear, e.DayOfMonth);
- formItem.date = setDate;
- dateDisplay.Text = setDate.ToString("d");
- }
- // private void showSelectDialog()
- // {
- // DialogInterface.
- // AlertDialog.Builder builder = new AlertDialog.Builder(context);
- // builder.SetTitle("Select");
- // string[] choices = {"Red","Green","Blue"};
- // bool[] selected = {false, false, true};
- // builder.SetMultiChoiceItems(choices, selected, selectDialogListener);
- // AlertDialog dialog = builder.Create();
- // dialog.Show();
- // }
- void SubmitForm()
- {
- View backgroundLLO = null;
- //reset any prior required field indicators
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- backgroundLLO = view.FindViewById(Resource.Id.backgroundLLO);
- if (backgroundLLO != null)
- backgroundLLO.SetBackgroundColor(Color.ParseColor("#00000000"));
- }
- if (!CheckRequired())
- {
- //submitButton.Enabled = false;
- List<string> missingfields = new List<string>();
- foreach (KeyValuePair<object, object> pair in emptyRequiredFields)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- missingfields.Add(formItem.label);
- backgroundLLO = view.FindViewById(Resource.Id.backgroundLLO);
- if (backgroundLLO != null)
- backgroundLLO.SetBackgroundColor(Color.ParseColor("#99FF0000"));
- }
- Toast.MakeText(this, "Empty Required Fields Found: " + string.Join<string>(", ", missingfields), ToastLength.Long).Show();
- int yScrollPos = 0;
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- if (!emptyRequiredFields.Contains(pair))
- {
- View tempView = (View)pair.Key;
- yScrollPos += tempView.Height;
- }
- else
- break;
- }
- formScrollView.ScrollTo(0, yScrollPos);
- return;
- }
- Toast.MakeText(this, "Please wait...", ToastLength.Short).Show();
- NameValueCollection parameters = new NameValueCollection();
- List<string> typesToSend = new List<string>() {"text", "textarea", "checkbox", "datepicker", "multiselect", "multicheckbox", "radio_set", "select"};
- foreach (KeyValuePair<object, object> pair in viewsToFormItem)
- {
- View view = (View)pair.Key;
- FormItem formItem = (FormItem)pair.Value;
- if (!emptyRequiredFields.ContainsKey(view))
- view.SetBackgroundColor(Color.ParseColor("#00000000")); // reset any non-required field background colors
- if (typesToSend.Contains(formItem.type))
- {
- string key = "__dnp_" + formItem.mappingPath;
- string value = "";
- if (formItem.type == "text" || formItem.type == "textarea")
- {
- EditText input = (EditText)view.FindViewById(Resource.Id.input);
- value = input.Text;
- }
- if (formItem.type == "checkbox")
- {
- CheckBox cb = (CheckBox)view.FindViewById(Resource.Id.input);
- value = cb.Checked.ToString();
- }
- if (formItem.type == "datepicker")
- {
- value = formItem.date.ToString();
- }
- if (formItem.type == "multiselect" || formItem.type == "multicheckbox" || formItem.type == "radio_set" || formItem.type == "select")
- {
- value = "";
- bool hasSelected = false;
- List<string> selectedTexts = new List<string>();
- for (int i=0; i<formItem.selectItems.Count; i++)
- {
- if (formItem.selectItems [i].selected)
- {
- selectedTexts.Add(formItem.selectItems [i].display);
- hasSelected = true;
- }
- if (hasSelected)
- {
- value = String.Join(", ", selectedTexts);
- }
- }
- }
- parameters.Add(key, value);
- }
- }
- new Thread(new ThreadStart(() =>
- {
- string orderNumber = SubmitRequest(itemId, parameters);
- RunOnUiThread(() => onOrderSubmitted(orderNumber));
- }
- )
- ).Start();
- }
- // void SubmitForm ()
- // {
- // Toast.MakeText (this, "Please wait...", ToastLength.Short).Show ();
- // NameValueCollection parameters = new NameValueCollection();
- // foreach (var element in from section in root from element in section where element.Tag != null && element.Tag is string select element) {
- // string key = "__dnp_" + element.Tag.ToString();
- // string value = element.Summary();
- // parameters.Add(key, value);
- // }
- //
- // new Thread (new ThreadStart (() =>
- // {
- // string orderNumber = SubmitRequest(itemId, parameters);
- // RunOnUiThread (() => onOrderSubmitted (orderNumber));
- // }
- // )
- // ).Start ();
- //
- //
- //
- // }
- //
- string SubmitRequest(string itemId, NameValueCollection form)
- {
- string orderId = DataUtility.SubmitOrder(itemId, form);
- return orderId;
- }
- void onOrderSubmitted(string orderNumber)
- {
- Toast.MakeText(this, "Order number " + orderNumber, ToastLength.Short).Show();
- }
- string GetFullImageUrl(string sPartialPath)
- {
- if (sPartialPath.StartsWith("catalog_resources/"))
- {
- return Config.ServerBaseUrl + "/sc/" + sPartialPath;
- } else
- {
- return Config.ServerBaseUrl + sPartialPath;
- }
- }
- public delegate void itemClick();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement