Exemplo n.º 1
0
        async void OnSelectClick(object sender, EventArgs e)
        {
            if (ContentView.FileList.GithubFiles == null)
            {
                return;
            }

            Device.BeginInvokeOnMainThread(delegate
            {
                ContentView.FileList.Hide();
                ContentView.ShowLoading();
            });

            List <GithubFile>           folder = ContentView.FileList.GithubFiles;
            List <DownloadedGithubFile> files  = await HubClient.Instance.DownloadFolder(GithubOwner, GithubRepo, CurrentBranch, folder);

            Toast.Show("Saving...", ContentView);

            /*
             * This is where we update pathing -> Shouldn't use repository folder hierarcy
             * We get the root of the style folder, and use that as the root folder for our folder hierarcy
             *
             * We can safely assume the root folder of the style, not the repository,
             * contains a config file (currently only supports project.json)
             * and therefore can remove any other folder hierarchy
             *
             * TODO What if it always isn't like that? && support other types of config files
             *
             */

            DownloadedGithubFile projectFile = files.Find(file => file.IsProjectFile);

            string folderPath = projectFile.FolderPath;

            string[] split = folderPath.Split('/');

            string rootFolder     = split[split.Length - 1];
            int    length         = folderPath.IndexOf(rootFolder, StringComparison.Ordinal);
            string repoRootFolder = folderPath.Substring(0, length);

            foreach (DownloadedGithubFile file in files)
            {
                file.Path = file.Path.Replace(repoRootFolder, "");
                FileUtils.SaveFileToFolder(file.Stream, file.Path, file.Name);
            }

            string zipname = rootFolder + Parser.ZipExtension;
            string source  = Path.Combine(Parser.ApplicationFolder, rootFolder);

            Toast.Show("Comperssing...", ContentView);

            string destination = Parser.Compress(source, zipname, MyStyleFolder);

            // Destination contains filename, just remove it
            destination = destination.Replace(zipname, "");

            Toast.Show("Done!", ContentView);

            Device.BeginInvokeOnMainThread(delegate
            {
                ContentView.HideLoading();
            });

            RepositoryData item = new RepositoryData();

            item.GithubId = GithubId;

            item.StyleName = rootFolder;

            item.Owner          = GithubOwner;
            item.Name           = GithubRepo;
            item.RepositoryPath = GithubPath;

            item.Branch            = CurrentBranch;
            item.LocalPath         = Path.Combine(MyStyleFolder, rootFolder);
            item.CanUploadToGithub = true;

            item.ConstructPrimaryKey();

            bool inserted = LocalStorage.Instance.Insert(item);

            // TODO Theoretically should check this earlier to and then prompt
            // the user whether they wish to overwrite an existing style
            if (!inserted)
            {
                LocalStorage.Instance.Update(item);
            }

            ShowMyStyles();
        }