// Returns json as string private string SerializeCurrentData() { var presenter = new PrescriptionJSONPresenter(Hash, PlaceDate); presenter.Account = ActiveAccount; presenter.Contact = ActiveContact; presenter.MedicationsList = Medications; var serializer = new JavaScriptSerializer(); return(serializer.Serialize(presenter)); }
// Import new .amk file in inbox, or read existing file from amiko. It // depends to save or not to `autoSavingMode`. Returns enum result ("Invalid"/"Found"/"ok"). public async Task <Result> ImportFile(string path) { if ((!path.Contains(_inboxDir) && !path.Contains(_amikoDir)) || !File.Exists(path)) { return(Result.Invalid); } var name = AMIKO_FILE_SUFFIX_RGX.Replace(Path.GetFileName(path), ""); // same file exists for active contact with check of hash value var currentPath = FindFilePathByNameAndHashFor(name, Hash, ActiveContact); Log.WriteLine("currentPath: {0}", currentPath); if (currentPath != null) { ReadFile(currentPath); LoadFiles(); return(Result.Found); } // load all fields from the file PrescriptionJSONPresenter presenter = null; string hash = null; string placeDate = null; Contact contact = null; try { string rawInput = File.ReadAllText(path); string json = Utilities.Base64Decode(rawInput) ?? "{}"; presenter = DeserializeJson(json); if (presenter == null || presenter.patient == null) { return(Result.Invalid); } hash = presenter.prescription_hash; if (hash == null || hash.Equals(string.Empty)) { return(Result.Invalid); } placeDate = presenter.place_date; if (placeDate == null || placeDate.Equals(string.Empty)) { return(Result.Invalid); } // NOTE: // Uid (in file) may wrong one from iOS and macOS (they have old // implementation) contact = presenter.Contact; contact.Uid = FixedUidOf(contact); } catch (Exception ex) { Log.WriteLine(ex.Message); return(Result.Invalid); } // same file exists for another contact var existingPath = FindFilePathByNameAndHashFor(name, hash, contact); Log.WriteLine("existingPath: {0}", existingPath); if (existingPath != null) { ReadFileFor(existingPath, contact); LoadFiles(); return(Result.Found); } this._Medications = new HashSet <Medication>(presenter.MedicationsList); UpdateMedicationList(); if (!AutoSavingMode) { name = String.Format("{0} {1}", name, notSaved); } this.ActiveFileName = name; this.ActiveFilePath = path; this.ActiveContact = contact; this.ActiveAccount = presenter.Account; this.Hash = hash; this.PlaceDate = placeDate; this.IsPreview = true; if (AutoSavingMode) { await Save(false); } LoadFiles(); return(Result.Ok); }