private void SaveJsonDetails()
        {
            JsonToObject @object = new JsonToObject(this.Weight, this.WakeUptime,
                                                    this.BedTime, this.MeasureSys);

            jsonToObjects.Add(@object);

            // write the list to a local file
            string path = Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData);
            string filename = Path.Combine(path, JSON_FILENAME);

            // use a stream writer to write the text out to file
            using (var streamWriter = new StreamWriter(filename, false))
            {
                // serialise the dogs list to a string using the jsonconvert library
                string jsonText = JsonConvert.SerializeObject(jsonToObjects);
                streamWriter.WriteLine(jsonText);
            }
            DisplayAlert("Success", "Succesfully saved to local storage", "Ok");
        }
Exemplo n.º 2
0
        private void ReadFromJson()
        {
            try
            {
                // Read the list from a local file
                string path = Environment.GetFolderPath(
                    Environment.SpecialFolder.LocalApplicationData);
                string filename = Path.Combine(path, JSON_FILENAME);

                // use a stream reader to read the text out to file
                using (var streamReader = new StreamReader(filename, false))
                {
                    string jsonText = streamReader.ReadToEnd();
                    jsonToObjects = JsonConvert.DeserializeObject <List <JsonToObject> >(jsonText);
                }

                // create object to assign values from list into variables
                JsonToObject objs = new JsonToObject();
                foreach (var obj in jsonToObjects)
                {   // used to run throw list and take latest entry into list (most up-to-date)
                    objs.Weight            = obj.Weight;
                    amountTarget           = objs.DrinkAmount(objs.Weight);
                    targetWaterIntake.Text = "Target : " + amountTarget + " mls";
                    objs.WakeUpTime        = obj.WakeUpTime;
                    objs.SleepTime         = obj.SleepTime;
                    double guidePerHour = objs.TargetLine(amountTarget);
                    guide.Text      = "Guide amount to drink : " + guidePerHour + " mls p/hour";
                    objs.MeasureSys = obj.MeasureSys;
                }
            }
            catch (FileNotFoundException)
            {
                // on error of finding local file ,
                // Settings Page must be loaded for configuration
                Navigation.PushAsync(new Settings());

                // on error reading local file, use default file

                // need a link to the assembly (dll) to get the file
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
                // create a stream to access the file
                Stream stream = assembly.GetManifestResourceStream(
                    "waterAppDev2018.Model.LocalStorage.txt");
                using (var reader = new StreamReader(stream))
                {
                    string jsonText = reader.ReadToEnd();
                    jsonToObjects = JsonConvert.DeserializeObject <List <JsonToObject> >(jsonText);
                }

                // create object to assign values from list into variables
                JsonToObject json = new JsonToObject();
                foreach (var obj in jsonToObjects)
                {
                    json.Weight = obj.Weight;
                    int amountTarget = json.DrinkAmount(json.Weight);
                    targetWaterIntake.Text = "Target : " + amountTarget + " mls";
                    json.WakeUpTime        = obj.WakeUpTime;
                    json.SleepTime         = obj.SleepTime;
                    double guidePerHour = json.TargetLine(amountTarget);
                    guide.Text      = "Guide amount to drink : " + guidePerHour + " mls p/hour";
                    json.MeasureSys = obj.MeasureSys;
                }
            }//catch
        }
 public JsonToObject()
 {
     JsonToObject j = new JsonToObject(this.Weight, this.WakeUpTime,
                                       this.SleepTime, this.MeasureSys);
 }