Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Content;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using System.Collections.Generic;
- using Android.Support.V4.View;
- using FoodNavigator.Resources.database;
- using Android.Locations;
- using FoodNavigator.Resources.near_you_base;
- using Android.Support.V4.App;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.IO;
- using System.Json;
- using System.Net;
- using Plugin.Geolocator;
- using System;
- using System.Globalization;
- using Android.App.Usage;
- namespace FoodNavigator
- {
- [Activity(Label = "FoodNavigator", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : FragmentActivity
- {
- private ViewPager mViewPager;
- private SlidingTabLayout mScrollView;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Setting . instead of , in deciminals
- string currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
- CultureInfo ci = new CultureInfo(currentCulture);
- ci.NumberFormat.NumberDecimalSeparator = ".";
- System.Threading.Thread.CurrentThread.CurrentCulture = ci;
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- mScrollView = FindViewById<SlidingTabLayout>(Resource.Id.sliding_tabs);
- mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
- mViewPager.Adapter = new SamplePagerAdapter(SupportFragmentManager);
- mScrollView.ViewPager = mViewPager;
- }
- public override bool OnCreateOptionsMenu(IMenu menu)
- {
- MenuInflater.Inflate(Resource.Menu.actionbar_menu,menu);
- return base.OnCreateOptionsMenu(menu);
- }
- }
- public class SamplePagerAdapter : FragmentPagerAdapter
- {
- private List<Android.Support.V4.App.Fragment> mFragmentHolder;
- public SamplePagerAdapter(Android.Support.V4.App.FragmentManager fragManager) : base(fragManager)
- {
- mFragmentHolder = new List<Android.Support.V4.App.Fragment>();
- mFragmentHolder.Add(new liked_food_class());
- mFragmentHolder.Add(new near_restaurants_class());
- mFragmentHolder.Add(new navigation_class());
- }
- public override int Count
- {
- get { return mFragmentHolder.Count; }
- }
- public override Android.Support.V4.App.Fragment GetItem(int position)
- {
- return mFragmentHolder[position];
- }
- }
- public class liked_food_class : Android.Support.V4.App.Fragment
- {
- #region variables
- CheckBox[] table = new CheckBox[15];
- List<string> List_of_food = new List<string>();
- DataBase db;
- list_of_food list_food = new list_of_food();
- List<example> saved_data = new List<example>();
- #endregion
- public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- var view = inflater.Inflate(Resource.Layout.liked_food_layout, container, false);
- List_of_food = list_food.get_food();
- db = new DataBase();
- db.createDataBase();
- load_data();
- #region checkboxes declarations
- table[0] = view.FindViewById<CheckBox>(Resource.Id.c1);
- table[1] = view.FindViewById<CheckBox>(Resource.Id.c2);
- table[2] = view.FindViewById<CheckBox>(Resource.Id.c3);
- table[3] = view.FindViewById<CheckBox>(Resource.Id.c4);
- table[4] = view.FindViewById<CheckBox>(Resource.Id.c5);
- table[5] = view.FindViewById<CheckBox>(Resource.Id.c6);
- table[6] = view.FindViewById<CheckBox>(Resource.Id.c7);
- table[7] = view.FindViewById<CheckBox>(Resource.Id.c8);
- table[8] = view.FindViewById<CheckBox>(Resource.Id.c9);
- table[9] = view.FindViewById<CheckBox>(Resource.Id.c10);
- table[10] = view.FindViewById<CheckBox>(Resource.Id.c11);
- table[11] = view.FindViewById<CheckBox>(Resource.Id.c12);
- table[12] = view.FindViewById<CheckBox>(Resource.Id.c13);
- table[13] = view.FindViewById<CheckBox>(Resource.Id.c14);
- table[14] = view.FindViewById<CheckBox>(Resource.Id.c15);
- #endregion
- load_data();
- if (saved_data.Count == 0)
- {
- fill_database();
- load_data();
- }
- checkbox_load();
- checkbox_database();
- return view;
- }
- public void fill_database()
- {
- int x = 0;
- foreach (var item in List_of_food)
- {
- example przyklad = new example()
- {
- id = x,
- name = List_of_food[x],
- is_checked = false,
- };
- db.insertIntoTableLikedFood(przyklad);
- x++;
- }
- }
- public void checkbox_load()
- {
- int x = 0;
- foreach (var item in table)
- {
- table[x].Text = List_of_food[x];
- item.Click += delegate
- {
- example przyklad = new example();
- if (item.Checked == true)
- {
- przyklad.is_checked = true;
- }
- else
- {
- przyklad.is_checked = false;
- }
- przyklad.name = item.Text;
- db.updateTableLikedFood(przyklad);
- load_data();
- };
- x++;
- }
- }
- public void load_data()
- {
- saved_data = db.selectTableLikedFood();
- }
- public void checkbox_database()
- {
- int x = 0;
- foreach (var item in saved_data)
- {
- if (item.is_checked == true)
- {
- table[x].Checked = true;
- }
- else
- {
- table[x].Checked = false;
- }
- x++;
- }
- }
- public override string ToString()
- {
- return "liked food";
- }
- }
- public class near_restaurants_class : Android.Support.V4.App.Fragment
- {
- #region variables
- Plugin.Geolocator.Abstractions.Position position = new Plugin.Geolocator.Abstractions.Position();
- List<restaurant_template> restaurant_base = new List<restaurant_template>();
- ListView list;
- LocationManager lm;
- DataBase db;
- Button search;
- SeekBar radius;
- double? lat=null,lon=null;
- bool gps_is_on = false;
- #endregion
- public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- var view = inflater.Inflate(Resource.Layout.near_rest_layout, container, false);
- list = view.FindViewById<ListView>(Resource.Id.list_view);
- TextView search_radius_text = view.FindViewById<TextView>(Resource.Id.search_radius);
- TextView search_radius_distance = view.FindViewById<TextView>(Resource.Id.search_distance);
- search_radius_distance.Text = ("Distance: 10 km");
- radius = view.FindViewById<SeekBar>(Resource.Id.seekBar1);
- radius.Max = 20;
- radius.Progress = 1;
- radius.Progress = 10;
- lm = (LocationManager)Context.GetSystemService(Context.LocationService);
- search = view.FindViewById<Button>(Resource.Id.button1);
- radius.ProgressChanged += delegate
- {
- search_radius_distance.Text = string.Format("Distance: {0} km",radius.Progress.ToString());
- };
- find_position();
- search.Click += delegate
- {
- check_connection();
- if (lat!=null && lon!=null && check_connection()==true)
- {
- find_locals();
- search.Visibility = ViewStates.Invisible;
- search_radius_text.Visibility = ViewStates.Invisible;
- radius.Visibility = ViewStates.Invisible;
- search_radius_distance.Visibility = ViewStates.Invisible;
- }
- else
- {
- find_position();
- }
- };
- return view;
- }
- bool check_connection()
- {
- try
- {
- HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create("https://maps.googleapis.com");
- iNetRequest.Timeout = 5000;
- WebResponse iNetResponse = iNetRequest.GetResponse();
- iNetResponse.Close();
- return true;
- }
- catch
- {
- Toast.MakeText(this.Activity, "Please check your internet connection", ToastLength.Short).Show();
- return false;
- }
- }
- async void find_position()
- {
- gps_is_on = lm.IsProviderEnabled(LocationManager.GpsProvider);
- if (gps_is_on==true)
- {
- try
- {
- Toast.MakeText(this.Activity, "Searching your location", ToastLength.Short).Show();
- var locator = CrossGeolocator.Current;
- locator.DesiredAccuracy = 50;
- position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
- if (lat == null && lon == null)
- {
- lat = position.Latitude;
- lon = position.Longitude;
- Toast.MakeText(this.Activity, "Found your location", ToastLength.Short).Show();
- }
- }
- catch
- {
- Toast.MakeText(this.Activity, "Problem with gps status", ToastLength.Short).Show();
- find_position();
- }
- }
- else
- {
- Toast.MakeText(this.Activity, "Please turn on gps", ToastLength.Short).Show();
- }
- }
- async public void find_locals()
- {
- db = new DataBase();
- List<example> nowa = db.selectTableLikedFood();
- bool add_plus = false;
- string url = ("https://maps.googleapis.com/maps/api/place/textsearch/json?query=");
- foreach(var item in nowa)
- {
- if(item.is_checked==true && add_plus==false)
- {
- url += item.name;
- add_plus = true;
- }
- else if (item.is_checked == true && add_plus == true)
- {
- url += ",";
- url += item.name;
- }
- }
- url += ("&location=");
- url += Math.Round(lat.Value, 7).ToString();
- url += ",";
- url += Math.Round(lon.Value, 7).ToString();
- url += ("&radius=");
- url += (radius.Progress*1000).ToString();
- url +=("&key=AIzaSyDXwGerP7xjveXn2KgQLI_Cvpuk7s7vAGs");
- Console.WriteLine(url);
- JsonValue the_list = await get_restaurants(url);
- var result = JsonConvert.DeserializeObject<Restaurant.RootObject>(the_list.ToString());
- List<Restaurant.Result> final_list = result.results;
- foreach (Restaurant.Result item in final_list)
- {
- restaurant_template temp = new restaurant_template()
- {
- name = item.name.ToUpper(),
- is_open = "open: " + item.opening_hours.open_now.ToString(),
- rating = "rating: " + item.rating.ToString(),
- distance = "distance: to do",
- address = "address: " + item.formatted_address,
- type = "type: " + item.types[0]
- };
- restaurant_base.Add(temp);
- }
- list_view_adapter adapter = new list_view_adapter(this.Activity, restaurant_base);
- list.Adapter = adapter;
- list.ItemLongClick += list_item_click;
- }
- private void list_item_click(object sender, AdapterView.ItemLongClickEventArgs e)
- {
- Android.Support.V4.App.FragmentTransaction transaction = FragmentManager.BeginTransaction();
- restaurant_dialog show_details = new restaurant_dialog();
- show_details.add_to_restaurant(restaurant_base[e.Position].name);
- show_details.add_to_restaurant(restaurant_base[e.Position].is_open);
- show_details.add_to_restaurant(restaurant_base[e.Position].address);
- show_details.add_to_restaurant(restaurant_base[e.Position].type);
- show_details.add_to_restaurant(restaurant_base[e.Position].rating);
- show_details.Show(transaction,"details");
- }
- private async Task<JsonValue> get_restaurants(string url)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(url));
- request.ContentType = "application/json";
- request.Method = "GET";
- using (WebResponse response = await request.GetResponseAsync())
- {
- using (Stream stream = response.GetResponseStream())
- {
- JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
- return jsonDoc;
- }
- }
- }
- public override string ToString()
- {
- return "nearest restaurants";
- }
- }
- public class navigation_class : Android.Support.V4.App.Fragment
- {
- public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- var view = inflater.Inflate(Resource.Layout.navigate_layout, container, false);
- return view;
- }
- public override string ToString()
- {
- return "navigation tab";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement