예제 #1
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            if(ApplicationData.Current.LocalSettings.Values["ReDoPhotosView"] == null)
            {
                ApplicationData.Current.LocalSettings.Values["ReDoPhotosView"] = 0;
            }

            PhotoCollectionFolder rphotocollection = (PhotoCollectionFolder)e.Parameter;
            team_media_group_id = (rphotocollection.folder_id).ToString();
            upload_position = (rphotocollection.numOfPhotos);
            nameOfFolder.Text = rphotocollection.nameOfFolder;
            myProgressRing.IsActive = true;
            string teamMediaAPI = rphotocollection.photosAPI;
            string teamMediaAPIResponse = await apiLibrary.apiCall((string)ApplicationData.Current.LocalSettings.Values["Tokens"], teamMediaAPI);
            Debug.WriteLine(teamMediaAPIResponse);
            RootObjectTeamMedia rteammedia = TeamMediaClass.teamMediaDataDeserializer(teamMediaAPIResponse);
            photosCollection.Clear();
            if (rteammedia.collection.items != null)
            {
                NoPhotosFound.Visibility = Visibility.Collapsed;
                Hashtable rteammedia_ht = new Hashtable();
                Hashtable rteamMediaPhotoId_ht = new Hashtable();
                for (int i = 0; i < rteammedia.collection.items[0].data.Count; i++)
                {
                    rteammedia_ht.Add(rteammedia.collection.items[0].data[i].name, i);
                }


                if (rphotocollection.photos != null)
                {
                    rteamMediaPhotoId_ht.Clear();
                    for (int i = 0; i < rphotocollection.photos.Count; i++)
                    {
                        rteamMediaPhotoId_ht.Add(rphotocollection.photos[i].photoid, i);
                    }
                    // read teamFileandMediaFolder and deserialize it into rteamMediaGroup
                    for (int i = 0; i < rteammedia.collection.items.Count; i++)
                    {
                        //check if there is anything stored in rphotocollection.photos

                        // if the photoid exists in rphotocollection, compare last update date. 
                        // 1. If media has not been updated, skip
                        // 2. if media has been updated, update. 
                        // 3. If media doesn't exist, update.
                        // While the above method was right, I am removing Step 1 from because by using ImageEx I am not downloading the images again and again. They are being downloaded only when they are needed.
                        long apiPhotoId = (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                        if (rteamMediaPhotoId_ht.ContainsKey(apiPhotoId))
                        {
                            //if (DateTime.Compare((DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value, (DateTime)rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].updated_at) > 0)
                            //{
                               // Debug.WriteLine("returned updated_at was greater"); // changed it to update either ways.
                                
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;

                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].pathToTempDownloadsPhoto = "ms-appdata:///temp/ImageCache/" + methodLibrary.BuildFileName(new Uri(rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].urlToPhoto));
                              
                           // }
                            // if media has not been updated, do nothing.
                        }
                        else
                        {
                            // fetch and update rphotocollection if the photo doesn't exist
                            Debug.WriteLine("photo didn't exist in received collection");
                            Photos rphotos = new Photos();
                            rphotos.folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                            rphotos.is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                            rphotos.nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;

                            rphotos.photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                            rphotos.position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                            rphotos.updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                            rphotos.urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                            rphotos.pathToTempDownloadsPhoto = "ms-appdata:///temp/ImageCache/" + methodLibrary.BuildFileName(new Uri(rphotos.urlToPhoto));                         

                            rphotocollection.photos.Add(rphotos); rphotocollection.photos.Add(rphotos);
                        }
                    }
                }
                else
                {
                    rphotocollection.photos = new List<Photos>();
                    Debug.WriteLine("No photos in photocollection. Downloading them.");
                    for (int i = 0; i < rteammedia.collection.items.Count; i++)
                    {
                        Photos rphotos = new Photos();
                        rphotos.folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                        rphotos.is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                        rphotos.nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;
                        rphotos.photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                        rphotos.position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                        rphotos.updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                        rphotos.urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                        rphotos.pathToTempDownloadsPhoto = "ms-appdata:///temp/ImageCache/" + methodLibrary.BuildFileName(new Uri(rphotos.urlToPhoto));                        
                        rphotocollection.photos.Add(rphotos);
                    }
                }
                rteammedia_ht.Clear();
                photosCollection.Clear();
                var photoList = rphotocollection.photos;
                foreach (var photo in photoList)
                {
                    photosCollection.Add(photo);
                }
                myProgressRing.IsActive = false;
                YesPhotosFound.Visibility = Visibility.Visible;
                string res = await mlibrary.readFile("teamFileandMediaFolder");
                RootMediaFolder rmediafolder = new RootMediaFolder();
                rmediafolder = TeamMediaGroupClass.mediaFolderDataDeserializer(res);
                for (int i = 0; i < rmediafolder.photoCollection.Count; i++)
                {
                    if (rmediafolder.photoCollection[i].folder_id == rphotocollection.folder_id)
                    {
                        rmediafolder.photoCollection[i].photos = rphotocollection.photos;
                    }
                }
                res = TeamMediaGroupClass.mediaFolderDataSerializer(rmediafolder);
                await mlibrary.writeFile("teamFileandMediaFolder", res);
            }
            // serialize rteamMediaFolder and write to file teamFileandMediaFolder
            else
            {
                // no photos found
                myProgressRing.IsActive = false;
                NoPhotosFound.Visibility = Visibility.Visible;
                YesPhotosFound.Visibility = Visibility.Collapsed;
            }

        }
예제 #2
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            PhotoCollectionFolder rphotocollection = (PhotoCollectionFolder)e.Parameter;
            nameOfFolder.Text = rphotocollection.nameOfFolder;
            myProgressRing.IsActive = true;
            string teamMediaAPI = rphotocollection.photosAPI;
            string teamMediaAPIResponse = await apiLibrary.apiCall((string)ApplicationData.Current.LocalSettings.Values["Tokens"], teamMediaAPI);
            RootObjectTeamMedia rteammedia = TeamMediaClass.teamMediaDataDeserializer(teamMediaAPIResponse);
            photosCollection.Clear();
            if (rteammedia.collection.items != null)
            {
                
                NoPhotosFound.Visibility = Visibility.Collapsed;
                Hashtable rteammedia_ht = new Hashtable();
                Hashtable rteamMediaPhotoId_ht = new Hashtable();
                for (int i = 0; i < rteammedia.collection.items[0].data.Count; i++)
                {
                    rteammedia_ht.Add(rteammedia.collection.items[0].data[i].name, i);
                }


                if (rphotocollection.photos != null)
                {
                    for (int i = 0; i < rphotocollection.photos.Count; i++)
                    {
                        rteamMediaPhotoId_ht.Add(rphotocollection.photos[i].photoid, i);
                    }
                    // read teamFileandMediaFolder and deserialize it into rteamMediaGroup
                    for (int i = 0; i < rteammedia.collection.items.Count; i++)
                    {
                        //check if there is anything stored in rphotocollection.photos

                        // if the photoid exists in rphotocollection, compare last update date. 
                        // 1. If media has not been updated, skip
                        // 2. if media has been updated, update. 
                        // 3. If media doesn't exist, update.
                        long apiPhotoId = (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                        if (rteamMediaPhotoId_ht.ContainsKey(apiPhotoId))
                        {
                            if (DateTime.Compare((DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value, (DateTime)rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].updated_at) > 0)
                            {
                                Debug.WriteLine("returned updated_at was greater");
                                // If the "updated_at" returned in the api is greater than what we have, update rphotocollection
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;

                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                                await methodLibrary.downloadImage(rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].urlToPhoto, "photos" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id + "_" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid + "_scaled");
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].pathToPhoto = "ms-appdata:///local/photos" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id + "_" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid + ".png";
                                await mlibrary.scaleImage("photos" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id + "_" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid);
                                //rphotos.pathToPhoto = "ms-appdata:///local/photos" + rphotos.folder_id + "_" + rphotos.photoid + ".png";
                                rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].pathToScaledPhoto = "ms-appdata:///local/photos" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id + "_" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid + "_scaled_scaled.png";
                                //rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].imageHeight = (await getImageHeight((string)rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].pathToScaledPhoto)).ToString();
                                //rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].imageWidth = (await getImageWidth((string)rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].pathToScaledPhoto)).ToString() ;

                            }
                            // if media has not been updated, do nothing.
                        }
                        else
                        {
                            // fetch and update rphotocollection if the photo doesn't exist
                            Debug.WriteLine("photo didn't exist in received collection");
                            Photos rphotos = new Photos();
                            rphotos.folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                            rphotos.is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                            rphotos.nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;

                            rphotos.photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                            rphotos.position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                            rphotos.updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                            rphotos.urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                            await methodLibrary.downloadImage(rphotos.urlToPhoto, "photos" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].folder_id + "_" + rphotocollection.photos[(int)rteamMediaPhotoId_ht[apiPhotoId]].photoid + "_scaled");
                            
                            rphotos.pathToPhoto = "ms-appdata:///local/photos" + rphotos.folder_id + "_" + rphotos.photoid + ".png";
                            rphotos.pathToScaledPhoto = "ms-appdata:///local/photos" + rphotos.folder_id + "_" + rphotos.photoid + "_scaled_scaled.png";
                            //rphotos.imageHeight = (await getImageHeight((string)rphotos.pathToScaledPhoto)).ToString();
                            //rphotos.imageWidth = (await getImageWidth((string)rphotos.pathToScaledPhoto)).ToString();

                            rphotocollection.photos.Add(rphotos); rphotocollection.photos.Add(rphotos);
                        }
                    }
                }
                else
                {
                    rphotocollection.photos = new List<Photos>();
                    Debug.WriteLine("No photos in photocollection. Downloading them.");
                    for (int i = 0; i < rteammedia.collection.items.Count; i++)
                    {
                        Photos rphotos = new Photos();
                        rphotos.folder_id = rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["team_media_group_id"]].value;
                        rphotos.is_private = rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value == null ? true : (bool)rteammedia.collection.items[i].data[(int)rteammedia_ht["is_private"]].value;
                        rphotos.nameOfPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value == null ? "No name provided" : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["description"]].value;
                        rphotos.photoid = rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["id"]].value;
                        rphotos.position = rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value == null ? 0 : (long)rteammedia.collection.items[i].data[(int)rteammedia_ht["position"]].value;
                        rphotos.updated_at = rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rteammedia.collection.items[i].data[(int)rteammedia_ht["updated_at"]].value;
                        rphotos.urlToPhoto = rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value == null ? null : (string)rteammedia.collection.items[i].data[(int)rteammedia_ht["medium_url"]].value;
                        await methodLibrary.downloadImage(rphotos.urlToPhoto, "photos" + rphotos.folder_id + "_" + rphotos.photoid);
                        await mlibrary.scaleImage("photos" + rphotos.folder_id + "_" + rphotos.photoid + "_scaled");
                        rphotos.pathToPhoto = "ms-appdata:///local/photos" + rphotos.folder_id + "_" + rphotos.photoid + ".png";
                        rphotos.pathToScaledPhoto = "ms-appdata:///local/photos" + rphotos.folder_id + "_" + rphotos.photoid + "_scaled_scaled.png";
                        //rphotos.imageHeight = (await getImageHeight((string)rphotos.pathToScaledPhoto)).ToString();
                        //rphotos.imageWidth = (await getImageWidth((string)rphotos.pathToScaledPhoto)).ToString();
                        rphotocollection.photos.Add(rphotos);
                    }
                }
                rteammedia_ht.Clear();
                photosCollection.Clear();
                var photoList = rphotocollection.photos;
                foreach (var photo in photoList)
                {
                    photosCollection.Add(photo);
                }
                myProgressRing.IsActive = false;
                YesPhotosFound.Visibility = Visibility.Visible;
                string res = await mlibrary.readFile("teamFileandMediaFolder");
                RootMediaFolder rmediafolder = new RootMediaFolder();
                rmediafolder = TeamMediaGroupClass.mediaFolderDataDeserializer(res);

                for (int i = 0; i < rmediafolder.photoCollection.Count; i++)
                {
                    if (rmediafolder.photoCollection[i].folder_id == rphotocollection.folder_id)
                    {
                        rmediafolder.photoCollection[i].photos = rphotocollection.photos;
                    }
                }

                res = TeamMediaGroupClass.mediaFolderDataSerializer(rmediafolder);
                await mlibrary.writeFile("teamFileandMediaFolder", res);
            }     
                // serialize rteamMediaFolder and write to file teamFileandMediaFolder
            
            else
            {
                // no photos found
                myProgressRing.IsActive = false;
                NoPhotosFound.Visibility = Visibility.Visible;
                YesPhotosFound.Visibility = Visibility.Collapsed;
            }
            
        }