예제 #1
0
        public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
        {
//			Console.WriteLine("[{0}] OnCreateView Called: {1}", TAG, DateTime.Now.ToLongTimeString());
            View v = inflater.Inflate(Resource.Layout.fragment_photo_gallery, container, false);

            mGridView = v.FindViewById <GridView>(Resource.Id.gridView);

            mGridView.Scroll += async(object sender, AbsListView.ScrollEventArgs e) => {
                if (e.FirstVisibleItem + e.VisibleItemCount == e.TotalItemCount && !endReached && e.TotalItemCount > 0)
                {
                    endReached = true;
//					Console.WriteLine("[{0}] Scroll Ended", TAG);
                    currentPage++;

                    List <GalleryItem> newItems;
                    if (query != null)
                    {
                        newItems = await new FlickrFetchr().Search(query, currentPage.ToString());
                    }
                    else
                    {
                        newItems = await new FlickrFetchr().Fetchitems(currentPage.ToString());
                    }

                    galleryItems = galleryItems.Concat(newItems).ToList();

                    var adapter = (ArrayAdapter)mGridView.Adapter;
                    adapter.AddAll(newItems);
                    //adapter.NotifyDataSetChanged();

                    endReached = false;
                }
            };

            mGridView.ScrollStateChanged += (object sender, AbsListView.ScrollStateChangedEventArgs e) => {
                if (e.ScrollState == ScrollState.Idle)
                {
                    if (idlePreloadTokenSource != null)
                    {
                        idlePreloadTokenSource.Cancel();
//						Console.WriteLine("[{0}] IdlePreloadTaskState: {1}", TAG, idlePreloadTask.IsCompleted);
                    }
                    idlePreloadTokenSource = new CancellationTokenSource();
                    idlePreloadTask        = Task.Run(async() => {
                        await new FlickrFetchr().PreloadImages(mGridView.FirstVisiblePosition, mGridView.LastVisiblePosition, galleryItems).ConfigureAwait(false);
                    }, idlePreloadTokenSource.Token);
                }
            };

            mGridView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
                GalleryItem item         = galleryItems[e.Position];
                var         photoPageUri = Android.Net.Uri.Parse(item.PhotoPageUrl);

                // Implicit intent
//				Intent i = new Intent(Intent.ActionView, photoPageUri);
                // Explicit intent
                Intent i = new Intent(Activity, typeof(PhotoPageActivity));
                i.SetData(photoPageUri);

                StartActivity(i);
            };

            mGridView.ItemLongClick += (object sender, AdapterView.ItemLongClickEventArgs e) => {
                if (galleryItems[e.Position].Url != null && galleryItems[e.Position].Url != String.Empty)
                {
                    Intent intent = new Intent(Activity, typeof(PhotoActivity));
                    intent.PutExtra(PHOTO_URL_EXTRA, galleryItems[e.Position].Url);
                    Activity.StartActivity(intent);
                }
            };

            SetupAdapter();
//			Console.WriteLine("[{0}] OnCreateView Done: {1}", TAG, DateTime.Now.ToLongTimeString());
            return(v);
        }
예제 #2
0
		public async Task<List<GalleryItem>> DownloadGalleryItems(string url)
		{
//			Console.WriteLine("[{0}] Start DownloadGalleryItems: {1}", TAG, DateTime.Now.ToLongTimeString());
			List<GalleryItem> items = new List<GalleryItem>();

			try {

//				Console.WriteLine("[{0}] DownloadGalleryItems Start GetUrlAsync: {1}", TAG, DateTime.Now.ToLongTimeString());
				string xmlString = await GetUrlAsync(url).ConfigureAwait(false);
//				Console.WriteLine("[{0}] DownloadGalleryItems End GetUrlAsync: {1}", TAG, DateTime.Now.ToLongTimeString());
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString);
//				Console.WriteLine("[{0}] Received xml from Url: {1}", TAG, url);
				//https://api.flickr.com:443/services/rest/?method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s

				XmlSerializer serializer = new XmlSerializer(typeof(rsp));

//				Console.WriteLine("[{0}] Fetchitems Start Deserialzer: {1}", TAG, DateTime.Now.ToLongTimeString());
				using (var reader = new StringReader(xmlString)) {
					var rsp = (rsp)serializer.Deserialize(reader);
					NumberOfHits = rsp.photos.total;
					var photos = rsp.photos.photo;
					foreach (rspPhotosPhoto photo in photos) {
						//Console.WriteLine("[{0}]\nPhoto Id: {1}\nCaption: {2}\nUrl: {3}", TAG, photo.id, photo.title, photo.url_s);
						GalleryItem item = new GalleryItem(){
							Caption = photo.title,
							Id = photo.id.ToString(),
							Url = photo.url_s,
							Owner = photo.owner,
							Filename = GetFilenameFromUrl(photo.url_s)
						};
						items.Add(item);
					}
				}
//				Console.WriteLine("[{0}] Fetchitems End Deserialzer: {1}", TAG, DateTime.Now.ToLongTimeString());
			}
			catch (IOException ioex) {
				Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ioex.Message);
			}
			catch (Exception ex){
				Console.WriteLine("[{0}] Failed to parse items: {1}", TAG, ex.Message);
			}

			//Console.WriteLine("[{0}] End DownloadGalleryItems: {1}", TAG, DateTime.Now.ToLongTimeString());
			return items;
			// Using httpUtility to make query string... no ? added automatically
//			try {
//				var query = HttpUtility.ParseQueryString(string.Empty);
//				query["method"] = methodGetRecent;
//				query["api_key"] = flickrAPIKey;
//				query[paramExtras] = extraSmallUrl;
//
//				string url = baseUrl + query;
//				string xmlString = await GetUrlAsync(url).ConfigureAwait(false);
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString); 
//				//https://api.flickr.com/services/rest/method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s
//			}
//			catch (Exception ex) {
//				Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ex.Message);
//			}

			// Using Android API to make query string
//			try {
//				string url = Android.Net.Uri.Parse(baseUrl).BuildUpon()
//					.AppendQueryParameter("method", methodGetRecent)
//					.AppendQueryParameter("api_key", flickrAPIKey)
//					.AppendQueryParameter(paramExtras, extraSmallUrl)
//					.Build().ToString();
//				string xmlString = await GetUrlAsync(url).ConfigureAwait(false);
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString); 
//				//https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s
//			}
//			catch (Exception ex) {
//				Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ex.Message);
//			}
		}
예제 #3
0
        public async Task <List <GalleryItem> > DownloadGalleryItems(string url)
        {
//			Console.WriteLine("[{0}] Start DownloadGalleryItems: {1}", TAG, DateTime.Now.ToLongTimeString());
            List <GalleryItem> items = new List <GalleryItem>();

            try {
//				Console.WriteLine("[{0}] DownloadGalleryItems Start GetUrlAsync: {1}", TAG, DateTime.Now.ToLongTimeString());
                string xmlString = await GetUrlAsync(url).ConfigureAwait(false);

//				Console.WriteLine("[{0}] DownloadGalleryItems End GetUrlAsync: {1}", TAG, DateTime.Now.ToLongTimeString());
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString);
//				Console.WriteLine("[{0}] Received xml from Url: {1}", TAG, url);
                //https://api.flickr.com:443/services/rest/?method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s

                XmlSerializer serializer = new XmlSerializer(typeof(rsp));

//				Console.WriteLine("[{0}] Fetchitems Start Deserialzer: {1}", TAG, DateTime.Now.ToLongTimeString());
                using (var reader = new StringReader(xmlString)) {
                    var rsp = (rsp)serializer.Deserialize(reader);
                    NumberOfHits = rsp.photos.total;
                    var photos = rsp.photos.photo;
                    foreach (rspPhotosPhoto photo in photos)
                    {
                        //Console.WriteLine("[{0}]\nPhoto Id: {1}\nCaption: {2}\nUrl: {3}", TAG, photo.id, photo.title, photo.url_s);
                        GalleryItem item = new GalleryItem()
                        {
                            Caption  = photo.title,
                            Id       = photo.id.ToString(),
                            Url      = photo.url_s,
                            Owner    = photo.owner,
                            Filename = GetFilenameFromUrl(photo.url_s)
                        };
                        items.Add(item);
                    }
                }
//				Console.WriteLine("[{0}] Fetchitems End Deserialzer: {1}", TAG, DateTime.Now.ToLongTimeString());
            }
            catch (IOException ioex) {
                Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ioex.Message);
            }
            catch (Exception ex) {
                Console.WriteLine("[{0}] Failed to parse items: {1}", TAG, ex.Message);
            }

            //Console.WriteLine("[{0}] End DownloadGalleryItems: {1}", TAG, DateTime.Now.ToLongTimeString());
            return(items);
            // Using httpUtility to make query string... no ? added automatically
//			try {
//				var query = HttpUtility.ParseQueryString(string.Empty);
//				query["method"] = methodGetRecent;
//				query["api_key"] = flickrAPIKey;
//				query[paramExtras] = extraSmallUrl;
//
//				string url = baseUrl + query;
//				string xmlString = await GetUrlAsync(url).ConfigureAwait(false);
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString);
//				//https://api.flickr.com/services/rest/method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s
//			}
//			catch (Exception ex) {
//				Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ex.Message);
//			}

            // Using Android API to make query string
//			try {
//				string url = Android.Net.Uri.Parse(baseUrl).BuildUpon()
//					.AppendQueryParameter("method", methodGetRecent)
//					.AppendQueryParameter("api_key", flickrAPIKey)
//					.AppendQueryParameter(paramExtras, extraSmallUrl)
//					.Build().ToString();
//				string xmlString = await GetUrlAsync(url).ConfigureAwait(false);
//				Console.WriteLine("[{0}] Received xml from Url: {1}\n{2}", TAG, url, xmlString);
//				//https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=ea248fbeac480b7c14fce5cece516ef0&extras=url_s
//			}
//			catch (Exception ex) {
//				Console.WriteLine("[{0}] Failed to fetch items: {1}", TAG, ex.Message);
//			}
        }