/// <summary>
        /// Creates whole sample structure.
        /// </summary>
        private async Task CreateAllAsync()
        {
            Assembly currentAssembly = null;
#if WINDOWS_UWP
            // Get current assembly that contains the image
            currentAssembly = GetType().GetTypeInfo().Assembly;
#else
            // Get current assembly that contains the image
            currentAssembly = Assembly.GetExecutingAssembly();
#endif

            // You can no longer check to see if groups.json exists on disk here. You have to 
            // open it and verify that it isn't null.           
            Stream groupsJson = currentAssembly.GetManifestResourceStream("ArcGISRuntimeXamarin.groups.json");
            try
            {
                await Task.Run(() =>
                {
                    if (groupsJson == null)
                        throw new NotImplementedException("groups.json file cannot be opened");
                    _sampleStructureMap = SampleStructureMap.Create(groupsJson);
                });
            }
            // This is thrown if even one of the files requires permissions greater 
            // than the application provides. 
            catch (UnauthorizedAccessException e)
            {
                throw; //TODO
            }
            catch (DirectoryNotFoundException e)
            {
                throw; //TODO
            }
            catch (Exception e)
            {
                throw; //TODO
            }
        }
        /// <summary>
        /// Gets sample by it's name.
        /// </summary>
        /// <param name="sampleName">The name of the sample.</param>
        /// <returns>Return <see cref="SampleModel"/> for the sample if found. Null if sample not found.</returns>
        //public SampleModel GetSampleByName(string sampleName)
        //{
        //    List<SampleModel> sampleList = new List<SampleModel>();

        //    foreach (var category in Categories)
        //    {
        //        foreach (var subCategory in category.SubCategories)
        //        {
        //            // Changed to use the SampleInfo class, but that means you have to manually create a list of the SampleModel items.
        //            foreach (var item in subCategory.SampleInfo)
        //            {
        //               // sampleList.Add(item.Sample);
        //            }
        //            var result = sampleList.FirstOrDefault(x => x.SampleName == sampleName);
        //            if (result != null)
        //                return result;
        //        }
        //    }
        //    return null;
        //}

        #region Factory methods
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="groupsJSON">Full path to the groups JSON file</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(Stream groupsJSON)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            try
            {
                // KD - Need two MemoryStreams? Need to investigate.
                using (groupsJSON)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        groupsJSON.CopyTo(ms);
                        var jsonInBytes = ms.ToArray();

                        using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                        {
                            structureMap         = serializer.ReadObject(ms2) as SampleStructureMap;
                            structureMap.Samples = new List <SampleModel>();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            #region CreateSamples
            //TODO: This part basically works, but needs some review, particularly once we add
            // Tutorials and Workflows and such.

            // Create all samples and add them to the groups since they are not part of
            // main configuration file

            List <string> pathList = new List <string>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/", "."));
                        }
                    }
                }
            }

            SampleModel sampleModel = new SampleModel();
            foreach (var samplePath in pathList)
            {
                sampleModel = SampleModel.Create(samplePath);
                if (sampleModel != null)
                {
                    structureMap.Samples.Add(sampleModel);
                }
            }

            var addedSamples = new List <SampleModel>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    //if (subCategory.SampleNames == null)
                    //    subCategory.SampleNames = new List<string>();

                    foreach (var sampleName in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                        addedSamples.Add(sample);
                    }
                }
            }

            #endregion
            // Create all samples
            //foreach (var sampleGroupFolder in sampleGroupFolders) // ie. Samples\Layers
            //{
            //    // This creates samples from all folders and adds them to the samples list
            //    // This means that sample is created even if it's not defined in the groups list
            //    var sampleFolders = sampleGroupFolder.GetDirectories();
            //    foreach (var sampleFolder in sampleFolders)  // ie. Samples\Layers\ArcGISTiledLayerFromUrl
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }
            //}

            //// Create all tutorials
            //if (tutorialsDirectory.Exists)
            //    foreach (var sampleFolder in tutorialsDirectory.GetDirectories()) // ie. Tutorials\AddMapToApp
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }

            //// Create all workflows
            //if (workflowDirectory.Exists)
            //    foreach (var sampleFolder in workflowDirectory.GetDirectories()) // ie. Workflows\SearchFeatures
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }

            //// Set samples to the sub-categories
            //var addedSamples = new List<SampleModel>();
            //foreach (var cateory in structureMap.Categories)
            //{
            //    foreach (var subCategory in cateory.SubCategories)
            //    {
            //        if (subCategory.Samples == null)
            //            subCategory.Samples = new List<SampleModel>();

            //        if (subCategory.SampleNames == null)
            //            subCategory.SampleNames = new List<string>();

            //        foreach (var sampleName in subCategory.SampleNames)
            //        {
            //            var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName);

            //            if (sample == null) continue;

            //            subCategory.Samples.Add(sample);
            //            addedSamples.Add(sample);
            //        }
            //    }
            //}

            //// Add samples that are not defined to the end of the groups
            //var notAddedSamples = structureMap.Samples.Where(x => !addedSamples.Contains(x)).ToList();
            //foreach (var sampleModel in notAddedSamples)
            //{
            //    var category = structureMap.Categories.FirstOrDefault(x => x.CategoryName == sampleModel.Category);
            //    if (category == null)
            //        continue;

            //    var subCategory = category.SubCategories.FirstOrDefault(x => x.SubCategoryName == sampleModel.SubCategory);
            //    if (subCategory != null)
            //    {
            //        subCategory.SampleNames.Add(sampleModel.SampleName);
            //        subCategory.Samples.Add(sampleModel);
            //    }
            //}

            //if (structureMap.Featured == null)
            //    structureMap.Featured = new List<FeaturedModel>();

            //// Set all sample models to the featured models
            //foreach (var featured in structureMap.Featured)
            //{
            //    var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == featured.SampleName);
            //    if (sample != null)
            //        featured.Sample = sample;
            //}
            //#endregion

            return(structureMap);
        }
Example #3
0
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="groupsJSON">Full path to the groups JSON file</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(Stream groupsJSON)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            try
            {
                // KD - Need two MemoryStreams? Need to investigate. Has to do with needing to open the json from the Android
                // Activity which gives you a stream. Then you need to get back to bytes.
                using (groupsJSON)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        groupsJSON.CopyTo(ms);
                        var jsonInBytes = ms.ToArray();

                        using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                        {
                            structureMap         = serializer.ReadObject(ms2) as SampleStructureMap;
                            structureMap.Samples = new List <SampleModel>();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.WriteLine(ex.Message);
            }

            #region CreateSamples

            List <string> pathList = new List <string>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/", "."));
                        }
                    }
                }
            }

            var sampleModel = new SampleModel();
            foreach (var samplePath in pathList)
            {
                sampleModel = SampleModel.Create(samplePath);
                if (sampleModel != null)
                {
                    structureMap.Samples.Add(sampleModel);
                }
            }

            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    foreach (var sampleName in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                    }
                }
            }

            #endregion

            return(structureMap);
        }
        /// <summary>
        /// Creates whole sample structure.
        /// </summary>
        private async Task CreateAllAsync()
        {
            // You can no longer check to see if groups.json exists on disk here. You have to 
            // open it and verify that it isn't null. 

            Stream groupsJson = GetType().Assembly.GetManifestResourceStream("ArcGISRuntimeXamarin.groups.json");
            try
            {
                await Task.Run(() =>
                {
                    if (groupsJson == null)
                        throw new NotImplementedException("groups.json file cannot be opened");
                    _sampleStructureMap = SampleStructureMap.Create(groupsJson); // Passing the Activity context here again
                });
            }
            // This is thrown if even one of the files requires permissions greater 
            // than the application provides. 
            catch (UnauthorizedAccessException e)
            {
                throw; //TODO
            }
            catch (DirectoryNotFoundException e)
            {
                throw; //TODO
            }
            catch (Exception e)
            {
                throw; //TODO
            }
        }