Advertisement
isotonicq

Untitled

Nov 3rd, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.57 KB | None | 0 0
  1. using Android.App;
  2. using Android.Content;
  3. using Android.Views;
  4. using Android.Widget;
  5. using Android.OS;
  6. using System.Collections.Generic;
  7. using Android.Support.V4.View;
  8. using FoodNavigator.Resources.database;
  9. using Android.Locations;
  10.  
  11. using FoodNavigator.Resources.near_you_base;
  12. using Android.Support.V4.App;
  13. using System.Threading.Tasks;
  14. using Newtonsoft.Json;
  15. using System.IO;
  16. using System.Json;
  17. using System.Net;
  18. using Plugin.Geolocator;
  19. using System;
  20. using System.Globalization;
  21. using Android.App.Usage;
  22.  
  23. namespace FoodNavigator
  24. {
  25.     [Activity(Label = "FoodNavigator", MainLauncher = true, Icon = "@drawable/icon")]
  26.  
  27.     public class MainActivity : FragmentActivity
  28.     {
  29.         private ViewPager mViewPager;
  30.         private SlidingTabLayout mScrollView;
  31.  
  32.         protected override void OnCreate(Bundle bundle)
  33.         {
  34.             base.OnCreate(bundle);
  35.  
  36.             // Setting . instead of , in deciminals
  37.             string currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
  38.             CultureInfo ci = new CultureInfo(currentCulture);
  39.             ci.NumberFormat.NumberDecimalSeparator = ".";
  40.             System.Threading.Thread.CurrentThread.CurrentCulture = ci;
  41.  
  42.             // Set our view from the "main" layout resource
  43.             SetContentView(Resource.Layout.Main);
  44.             mScrollView = FindViewById<SlidingTabLayout>(Resource.Id.sliding_tabs);
  45.             mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
  46.             mViewPager.Adapter = new SamplePagerAdapter(SupportFragmentManager);
  47.             mScrollView.ViewPager = mViewPager;    
  48.         }
  49.  
  50.         public override bool OnCreateOptionsMenu(IMenu menu)
  51.         {
  52.             MenuInflater.Inflate(Resource.Menu.actionbar_menu,menu);
  53.             return base.OnCreateOptionsMenu(menu);
  54.         }    
  55.     }
  56.  
  57.     public class SamplePagerAdapter : FragmentPagerAdapter
  58.     {
  59.         private List<Android.Support.V4.App.Fragment> mFragmentHolder;
  60.  
  61.         public SamplePagerAdapter(Android.Support.V4.App.FragmentManager fragManager) : base(fragManager)
  62.         {
  63.             mFragmentHolder = new List<Android.Support.V4.App.Fragment>();
  64.             mFragmentHolder.Add(new liked_food_class());
  65.             mFragmentHolder.Add(new near_restaurants_class());
  66.             mFragmentHolder.Add(new navigation_class());
  67.         }
  68.  
  69.         public override int Count
  70.         {
  71.             get { return mFragmentHolder.Count; }
  72.         }
  73.  
  74.         public override Android.Support.V4.App.Fragment GetItem(int position)
  75.         {
  76.             return mFragmentHolder[position];
  77.         }
  78.     }
  79.  
  80.     public class liked_food_class : Android.Support.V4.App.Fragment
  81.     {
  82.         #region variables
  83.         CheckBox[] table = new CheckBox[15];
  84.         List<string> List_of_food = new List<string>();
  85.         DataBase db;
  86.         list_of_food list_food = new list_of_food();
  87.         List<example> saved_data = new List<example>();
  88. #endregion
  89.  
  90.         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  91.         {
  92.  
  93.             var view = inflater.Inflate(Resource.Layout.liked_food_layout, container, false);
  94.  
  95.             List_of_food = list_food.get_food();
  96.             db = new DataBase();
  97.             db.createDataBase();
  98.  
  99.             load_data();
  100.  
  101.             #region checkboxes declarations
  102.             table[0] = view.FindViewById<CheckBox>(Resource.Id.c1);
  103.             table[1] = view.FindViewById<CheckBox>(Resource.Id.c2);
  104.             table[2] = view.FindViewById<CheckBox>(Resource.Id.c3);
  105.             table[3] = view.FindViewById<CheckBox>(Resource.Id.c4);
  106.             table[4] = view.FindViewById<CheckBox>(Resource.Id.c5);
  107.             table[5] = view.FindViewById<CheckBox>(Resource.Id.c6);
  108.             table[6] = view.FindViewById<CheckBox>(Resource.Id.c7);
  109.             table[7] = view.FindViewById<CheckBox>(Resource.Id.c8);
  110.             table[8] = view.FindViewById<CheckBox>(Resource.Id.c9);
  111.             table[9] = view.FindViewById<CheckBox>(Resource.Id.c10);
  112.             table[10] = view.FindViewById<CheckBox>(Resource.Id.c11);
  113.             table[11] = view.FindViewById<CheckBox>(Resource.Id.c12);
  114.             table[12] = view.FindViewById<CheckBox>(Resource.Id.c13);
  115.             table[13] = view.FindViewById<CheckBox>(Resource.Id.c14);
  116.             table[14] = view.FindViewById<CheckBox>(Resource.Id.c15);
  117.             #endregion
  118.             load_data();
  119.  
  120.             if (saved_data.Count == 0)
  121.             {
  122.                 fill_database();
  123.                 load_data();
  124.             }
  125.  
  126.             checkbox_load();
  127.             checkbox_database();
  128.  
  129.             return view;
  130.         }
  131.  
  132.         public void fill_database()
  133.         {
  134.             int x = 0;
  135.  
  136.             foreach (var item in List_of_food)
  137.             {
  138.                 example przyklad = new example()
  139.                 {
  140.                     id = x,
  141.                     name = List_of_food[x],
  142.                     is_checked = false,
  143.                 };
  144.  
  145.                 db.insertIntoTableLikedFood(przyklad);
  146.  
  147.                 x++;
  148.             }
  149.         }
  150.  
  151.         public void checkbox_load()
  152.         {
  153.             int x = 0;
  154.  
  155.             foreach (var item in table)
  156.             {
  157.                 table[x].Text = List_of_food[x];
  158.  
  159.                 item.Click += delegate
  160.                 {
  161.                     example przyklad = new example();
  162.  
  163.                     if (item.Checked == true)
  164.                     {
  165.                         przyklad.is_checked = true;
  166.                     }
  167.                     else
  168.                     {
  169.                         przyklad.is_checked = false;
  170.                     }
  171.  
  172.                     przyklad.name = item.Text;
  173.                     db.updateTableLikedFood(przyklad);
  174.                     load_data();
  175.                 };
  176.  
  177.                 x++;
  178.             }
  179.         }
  180.  
  181.         public void load_data()
  182.         {
  183.             saved_data = db.selectTableLikedFood();
  184.         }
  185.  
  186.         public void checkbox_database()
  187.         {
  188.             int x = 0;
  189.             foreach (var item in saved_data)
  190.             {
  191.                 if (item.is_checked == true)
  192.                 {
  193.                     table[x].Checked = true;
  194.                 }
  195.                 else
  196.                 {
  197.                     table[x].Checked = false;
  198.                 }
  199.                 x++;
  200.             }
  201.         }
  202.  
  203.         public override string ToString()
  204.         {
  205.             return "liked food";
  206.         }
  207.  
  208.     }
  209.  
  210.     public class near_restaurants_class : Android.Support.V4.App.Fragment
  211.     {
  212.         #region variables
  213.         Plugin.Geolocator.Abstractions.Position position = new Plugin.Geolocator.Abstractions.Position();
  214.         List<restaurant_template> restaurant_base = new List<restaurant_template>();
  215.         ListView list;
  216.         LocationManager lm;
  217.         DataBase db;
  218.         Button search;
  219.         SeekBar radius;
  220.         double? lat=null,lon=null;
  221.         bool gps_is_on = false;
  222.         #endregion
  223.  
  224.         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  225.         {
  226.             var view = inflater.Inflate(Resource.Layout.near_rest_layout, container, false);
  227.             list =  view.FindViewById<ListView>(Resource.Id.list_view);
  228.  
  229.             TextView search_radius_text = view.FindViewById<TextView>(Resource.Id.search_radius);
  230.             TextView search_radius_distance = view.FindViewById<TextView>(Resource.Id.search_distance);
  231.             search_radius_distance.Text = ("Distance: 10 km");
  232.  
  233.             radius = view.FindViewById<SeekBar>(Resource.Id.seekBar1);
  234.             radius.Max = 20;
  235.             radius.Progress = 1;
  236.             radius.Progress = 10;
  237.  
  238.             lm = (LocationManager)Context.GetSystemService(Context.LocationService);
  239.  
  240.             search = view.FindViewById<Button>(Resource.Id.button1);
  241.  
  242.             radius.ProgressChanged += delegate
  243.             {
  244.                 search_radius_distance.Text = string.Format("Distance: {0} km",radius.Progress.ToString());
  245.             };
  246.  
  247.             find_position();
  248.  
  249.             search.Click += delegate
  250.             {
  251.                 check_connection();
  252.  
  253.                 if (lat!=null && lon!=null && check_connection()==true)
  254.                 {
  255.                     find_locals();
  256.                     search.Visibility = ViewStates.Invisible;
  257.                     search_radius_text.Visibility = ViewStates.Invisible;
  258.                     radius.Visibility = ViewStates.Invisible;
  259.                     search_radius_distance.Visibility = ViewStates.Invisible;
  260.                 }
  261.                 else
  262.                 {
  263.                     find_position();
  264.                 }
  265.             };              
  266.            return view;
  267.         }
  268.  
  269.         bool check_connection()
  270.         {
  271.             try
  272.             {
  273.                 HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create("https://maps.googleapis.com");
  274.                 iNetRequest.Timeout = 5000;
  275.                 WebResponse iNetResponse = iNetRequest.GetResponse();
  276.                 iNetResponse.Close();
  277.                 return true;
  278.             }
  279.             catch
  280.             {
  281.                 Toast.MakeText(this.Activity, "Please check your internet connection", ToastLength.Short).Show();
  282.                 return false;
  283.             }
  284.         }
  285.  
  286.         async void find_position()
  287.         {
  288.             gps_is_on = lm.IsProviderEnabled(LocationManager.GpsProvider);
  289.  
  290.             if (gps_is_on==true)
  291.             {
  292.                 try
  293.                 {
  294.                     Toast.MakeText(this.Activity, "Searching your location", ToastLength.Short).Show();
  295.                     var locator = CrossGeolocator.Current;
  296.                     locator.DesiredAccuracy = 50;
  297.                     position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
  298.  
  299.                     if (lat == null && lon == null)
  300.                     {
  301.                         lat = position.Latitude;
  302.                         lon = position.Longitude;
  303.                         Toast.MakeText(this.Activity, "Found your location", ToastLength.Short).Show();
  304.                     }
  305.                 }
  306.                 catch
  307.                 {
  308.                     Toast.MakeText(this.Activity, "Problem with gps status", ToastLength.Short).Show();
  309.                     find_position();
  310.                 }
  311.             }
  312.             else
  313.             {
  314.                 Toast.MakeText(this.Activity, "Please turn on gps", ToastLength.Short).Show();
  315.             }
  316.         }
  317.  
  318.         async public void find_locals()
  319.         {
  320.             db = new DataBase();
  321.             List<example> nowa = db.selectTableLikedFood();
  322.             bool add_plus = false;
  323.  
  324.             string url = ("https://maps.googleapis.com/maps/api/place/textsearch/json?query=");
  325.        
  326.             foreach(var item in nowa)
  327.             {
  328.                 if(item.is_checked==true && add_plus==false)
  329.                 {
  330.                     url += item.name;
  331.                     add_plus = true;
  332.                 }
  333.                 else if (item.is_checked == true && add_plus == true)
  334.                 {
  335.                     url += ",";
  336.                     url += item.name;
  337.                 }
  338.             }
  339.  
  340.             url += ("&location=");
  341.             url += Math.Round(lat.Value, 7).ToString();
  342.             url += ",";
  343.             url += Math.Round(lon.Value, 7).ToString();
  344.             url += ("&radius=");
  345.             url += (radius.Progress*1000).ToString();
  346.             url +=("&key=AIzaSyDXwGerP7xjveXn2KgQLI_Cvpuk7s7vAGs");
  347.  
  348.             Console.WriteLine(url);
  349.             JsonValue the_list = await get_restaurants(url);
  350.             var result = JsonConvert.DeserializeObject<Restaurant.RootObject>(the_list.ToString());
  351.             List<Restaurant.Result> final_list = result.results;
  352.  
  353.             foreach (Restaurant.Result item in final_list)
  354.             {
  355.                 restaurant_template temp = new restaurant_template()
  356.                 {
  357.                     name = item.name.ToUpper(),
  358.                     is_open = "open: " + item.opening_hours.open_now.ToString(),
  359.                     rating = "rating: " + item.rating.ToString(),
  360.                     distance = "distance: to do",
  361.                     address = "address: " + item.formatted_address,
  362.                     type = "type: " + item.types[0]
  363.                    
  364.                 };
  365.                 restaurant_base.Add(temp);
  366.             }
  367.  
  368.             list_view_adapter adapter = new list_view_adapter(this.Activity, restaurant_base);
  369.             list.Adapter = adapter;
  370.             list.ItemLongClick += list_item_click;
  371.         }
  372.  
  373.         private void list_item_click(object sender, AdapterView.ItemLongClickEventArgs e)
  374.         {
  375.             Android.Support.V4.App.FragmentTransaction transaction = FragmentManager.BeginTransaction();
  376.             restaurant_dialog show_details = new restaurant_dialog();
  377.             show_details.add_to_restaurant(restaurant_base[e.Position].name);
  378.             show_details.add_to_restaurant(restaurant_base[e.Position].is_open);
  379.             show_details.add_to_restaurant(restaurant_base[e.Position].address);
  380.             show_details.add_to_restaurant(restaurant_base[e.Position].type);
  381.             show_details.add_to_restaurant(restaurant_base[e.Position].rating);
  382.  
  383.             show_details.Show(transaction,"details");
  384.         }
  385.  
  386.         private async Task<JsonValue> get_restaurants(string url)
  387.         {
  388.             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(url));
  389.             request.ContentType = "application/json";
  390.             request.Method = "GET";
  391.  
  392.             using (WebResponse response = await request.GetResponseAsync())
  393.             {
  394.                 using (Stream stream = response.GetResponseStream())
  395.                 {
  396.                     JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
  397.                     return jsonDoc;
  398.                 }
  399.             }
  400.         }
  401.  
  402.         public override string ToString()
  403.         {
  404.             return "nearest restaurants";
  405.         }
  406.  
  407.     }
  408.    
  409.     public class navigation_class : Android.Support.V4.App.Fragment
  410.     {
  411.         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  412.         {
  413.             var view = inflater.Inflate(Resource.Layout.navigate_layout, container, false);
  414.             return view;
  415.         }
  416.  
  417.         public override string ToString()
  418.         {
  419.             return "navigation tab";
  420.         }
  421.     }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement