private async void AddToUploads(List <AppTask> preppedTasks, bool shareWithCreator)
        {
            Random        rand       = new Random();
            AppDataUpload uploadData = new AppDataUpload
            {
                ItemId      = rand.Next(),
                UploadRoute = string.Format("api/CompletedTasks/Submit?activityId={0}&shareWithCreator={1}&enteredName={2}",
                                            DisplayedActivity.Id, shareWithCreator, (TableView.Source as TaskViewSource).enteredName),
                Name        = DisplayedActivity.Name,
                Description = DisplayedActivity.Description,
                ImageUrl    = DisplayedActivity.ImageUrl,
                UploadType  = UploadType.Result,
                CreatedAt   = DateTime.Now,
                FilesJson   = JsonConvert.SerializeObject(MakeUploads(preppedTasks)),
                JsonData    = JsonConvert.SerializeObject(preppedTasks,
                                                          new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    MaxDepth         = 5,
                    TypeNameHandling = TypeNameHandling.Auto
                })
            };

            DatabaseManager dbManager = await GetDatabaseManager();

            dbManager.AddUpload(uploadData);
            dbManager.DeleteProgress(DisplayedActivity.Id);

            NavigationController.PopViewController(true);
            return;
        }
        private void Upload(List <AppTask> preppedTasks, bool shareWithCreator)
        {
            Random        rand       = new Random();
            AppDataUpload uploadData = new AppDataUpload
            {
                ItemId      = rand.Next(),
                UploadRoute = $"api/CompletedTasks/Submit?activityId={learningActivity.Id}&shareWithCreator={shareWithCreator}&enteredName={enteredName}",
                Name        = learningActivity.Name,
                Description = learningActivity.Description,
                ImageUrl    = learningActivity.ImageUrl,
                UploadType  = UploadType.Result,
                FilesJson   = JsonConvert.SerializeObject(Storage.MakeUploads(preppedTasks)),
                JsonData    = JsonConvert.SerializeObject(preppedTasks,
                                                          new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    MaxDepth         = 5,
                    TypeNameHandling = TypeNameHandling.Auto
                })
            };

            shouldSave = false;
            dbManager.AddUpload(uploadData);
            dbManager.DeleteProgress(learningActivity.Id);

            if (!Intent.GetBooleanExtra("FromCollection", false))
            {
                using (Intent intent = new Intent(this, typeof(UploadsActivity)))
                {
                    StartActivity(intent);
                }
            }

            Finish();
        }
示例#3
0
        public void UpdateContent(AppDataUpload data)
        {
            string url = (!string.IsNullOrWhiteSpace(data.ImageUrl)) ? AppUtils.GetPathForLocalFile(data.ImageUrl) : "";

            if (!File.Exists(url))
            {
                url = (string.IsNullOrWhiteSpace(data.ImageUrl)) ?
                      "" :
                      Common.ServerUtils.GetUploadUrl(data.ImageUrl);
            }

            if (string.IsNullOrWhiteSpace(url))
            {
                ImageService.Instance.LoadCompiledResource("AppLogo").Into(ActivityIcon);
            }
            else
            {
                // check if it's a local file
                if (File.Exists(url))
                {
                    ImageService.Instance.LoadFile(url).
                    Transform(new CircleTransformation()).Into(ActivityIcon);
                }
                else
                {
                    //string imgUrl = Common.ServerUtils.GetUploadUrl(url);
                    ImageService.Instance.LoadUrl(url).
                    Transform(new CircleTransformation()).Into(ActivityIcon);
                }
            }

            List <FileUpload> files = JsonConvert.DeserializeObject <List <FileUpload> >(data.FilesJson);

            float totalFileSizeMb = 0;

            string folderPath = (data.UploadType == UploadType.NewActivity) ?
                                Storage.GetCacheFolder() : Storage.GetUploadsFolder();

            foreach (FileUpload up in files)
            {
                if (!string.IsNullOrWhiteSpace(up.RemoteFilePath))
                {
                    continue;
                }

                string absPath = Path.Combine(folderPath, up.LocalFilePath);

                FileInfo fInfo = new FileInfo(absPath);
                if (fInfo.Exists)
                {
                    totalFileSizeMb += fInfo.Length / 1000000f;
                }
            }

            TitleLabel.Text    = data.Name;
            DateLabel.Text     = string.Format("Created: {0:g}", data.CreatedAt.ToLocalTime());
            FileSizeLabel.Text = string.Format("Total size: {0:0.0}MB", totalFileSizeMb);
        }
示例#4
0
 private void DeleteFiles(AppDataUpload data)
 {
     files = JsonConvert.DeserializeObject <List <FileUpload> >(data.FilesJson);
     foreach (FileUpload up in files)
     {
         if (File.Exists(up.LocalFilePath))
         {
             File.Delete(up.LocalFilePath);
         }
     }
 }
示例#5
0
        public static Task <ServerResponse <string> > UploadCollection(AppDataUpload upload, bool updateExisting = false)
        {
            ActivityCollection collection = JsonConvert.DeserializeObject <ActivityCollection>(upload.JsonData);
            List <FileUpload>  files      = JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson);

            // update the collection's image url if it's had one uploaded
            FileUpload f = files?.Where(fi => fi.LocalFilePath == collection.ImageUrl).FirstOrDefault();

            if (f != null && !string.IsNullOrWhiteSpace(f.RemoteFilePath))
            {
                collection.ImageUrl = f.RemoteFilePath;
            }

            return(updateExisting ? Put <string>(upload.UploadRoute, collection) :
                   Post <string>(upload.UploadRoute, collection));
        }
示例#6
0
        public static Task <ServerResponse <string> > UploadActivity(AppDataUpload upload, bool updateExisting = false)
        {
            LearningActivity  activity = JsonConvert.DeserializeObject <LearningActivity>(upload.JsonData);
            List <FileUpload> files    = JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson);

            // update the activity's image url if it's had one uploaded
            FileUpload f = files?.Where(fi => fi.LocalFilePath == activity.ImageUrl).FirstOrDefault();

            if (f != null && !string.IsNullOrWhiteSpace(f.RemoteFilePath))
            {
                activity.ImageUrl = f.RemoteFilePath;
            }

            // Update tasks (and their children) which have had files uploaded
            // to point at the new file URLs
            foreach (LearningTask parentTask in activity.LearningTasks)
            {
                string newJson = GetNewTaskJsonData(parentTask, files);
                if (!string.IsNullOrWhiteSpace(newJson))
                {
                    parentTask.JsonData = newJson;
                }

                if (parentTask.ChildTasks == null)
                {
                    continue;
                }

                foreach (LearningTask childTask in parentTask.ChildTasks)
                {
                    string newChildJson = GetNewTaskJsonData(childTask, files);
                    if (!string.IsNullOrWhiteSpace(newChildJson))
                    {
                        childTask.JsonData = newChildJson;
                    }
                }
            }

            return(updateExisting ? Put <string>(upload.UploadRoute, activity) :
                   Post <string>(upload.UploadRoute, activity));
        }
示例#7
0
        private async Task DeleteResults(int index)
        {
            ShowLoading();

            AppDataUpload thisUpload = viewSource.Rows[index];

            List <FileUpload> files             = JsonConvert.DeserializeObject <List <FileUpload> >(thisUpload.FilesJson);
            string            uploadsFolderPath = (thisUpload.UploadType == UploadType.NewActivity) ?
                                                  Storage.GetCacheFolder() : Storage.GetUploadsFolder();

            foreach (FileUpload up in files)
            {
                string absPath = Path.Combine(uploadsFolderPath, up.LocalFilePath);
                if (File.Exists(absPath))
                {
                    File.Delete(absPath);
                }
            }

            DatabaseManager dbManager = await Storage.GetDatabaseManager();

            dbManager.DeleteUpload(viewSource.Rows[index]);

            List <AppDataUpload> newList = dbManager.GetUploadQueue().ToList();

            viewSource.Rows.RemoveAt(index);
            viewSource.UpdateData(newList);
            TableView.ReloadData();

            await(ParentViewController as MainTabBarController).UpdateUploadsBadge(newList.Count);
            ManageNavItems();

            HideLoading();

            Toast.ShowToast("Deleted");
        }
示例#8
0
        private async Task <bool> UploadResults(int index, Action OnFinish)
        {
            ShowLoading();

            AppDataUpload upload = viewSource.Rows[index];

            DatabaseManager dbManager = await Storage.GetDatabaseManager();

            // Upload relevent files
            bool success = await Storage.UploadFiles(
                JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson),
                index,
                (percentage) =>
            {
                Console.WriteLine("Upload percentage: " + percentage);
                loadPop.loadingLabel.Text = string.Format("Uploading: {0}%", percentage);
            },
                (listPos, jsonData) =>
            {
                viewSource.Rows[listPos].FilesJson = jsonData;
                dbManager.UpdateUpload(viewSource.Rows[listPos]);
                viewSource.UpdateData(dbManager.GetUploadQueue().ToList());
                upload = viewSource.Rows[index];
            },
                (upload.UploadType == UploadType.NewActivity ||
                 upload.UploadType == UploadType.UpdatedActivity)?Storage.GetCacheFolder() : Storage.GetUploadsFolder()
                );

            if (!success)
            {
                HideLoading();
                AppUtils.ShowSimpleDialog(this, "Unable to Upload", "Something went wrong, please try again later.", "Got it");
                return(false);
            }

            ServerResponse <string> resp = new ServerResponse <string>();

            if (upload.UploadType == UploadType.NewActivity ||
                upload.UploadType == UploadType.UpdatedActivity)
            {
                resp = await ServerUtils.UploadActivity(upload, upload.UploadType == UploadType.UpdatedActivity);
            }
            else
            {
                // Uploading activity results
                List <FileUpload> files = JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson);

                AppTask[] results = JsonConvert.DeserializeObject <AppTask[]>(upload.JsonData) ?? new AppTask[0];
                resp = await ServerUtils.UpdateAndPostResults(results, files, upload.UploadRoute);
            }

            HideLoading();

            if (resp == null)
            {
                // do this but for iOS, fool
                var suppress = AppUtils.SignOut(this);
                return(false);
            }

            if (!resp.Success)
            {
                AppUtils.ShowSimpleDialog(this, "Unable to Upload", "Something went wrong, please try again later.", "Got it");
                return(false);
            }

            dbManager.DeleteUpload(upload);

            var newList = dbManager.GetUploadQueue().ToList();

            viewSource.UpdateData(newList);
            TableView.ReloadData();

            await(ParentViewController as MainTabBarController).UpdateUploadsBadge(newList.Count);
            ManageNavItems();

            OnFinish?.Invoke();

            return(true);
        }