Beispiel #1
0
		public static Recipe FromJson(Context context, JSONObject json) {
			var recipe = new Recipe ();
			try 
			{
				recipe.TitleText = json.GetString(Constants.RecipeFieldTitle);
				recipe.SummaryText = json.GetString(Constants.RecipeFieldSummary);
				if (json.Has(Constants.RecipeFieldImage)) {
					recipe.RecipeImage = json.GetString(Constants.RecipeFieldImage);
				}
				JSONArray ingredients = json.GetJSONArray(Constants.RecipeFieldIngredients);
				recipe.IngredientsText = "";
				for (int i = 0; i < ingredients.Length(); i++)
				{
					recipe.IngredientsText += " - " + ingredients.GetJSONObject(i).GetString(Constants.RecipeFieldText) + "\n";
				}

				JSONArray steps = json.GetJSONArray(Constants.RecipeFieldSteps);
				for (int i = 0; i < steps.Length(); i++)
				{
					var step = steps.GetJSONObject(i);
					var recipeStep = new RecipeStep();
					recipeStep.StepText = step.GetString(Constants.RecipeFieldText);
					if (step.Has(Constants.RecipeFieldName)) {
						recipeStep.StepImage = step.GetString(Constants.RecipeFieldImage);
					}
					recipe.RecipeSteps.Add(recipeStep);
				}
			}
			catch (Exception ex) {
				Log.Error (Tag, "Error loading recipe: " + ex);
				return null;
			}
			return recipe;
		}
		private void DisplayRecipe (Recipe recipe) {
			Animation fadeIn = AnimationUtils.LoadAnimation (this, Android.Resource.Animation.FadeIn);
			titleTextView.Animation = fadeIn;
			titleTextView.Text = recipe.TitleText;
			summaryTextView.Text = recipe.SummaryText;
			if (recipe.RecipeImage != null) {
				imageView.Animation = fadeIn;
				Bitmap recipeImage = AssetUtils.LoadBitmapAsset (this, recipe.RecipeImage);
				imageView.SetImageBitmap (recipeImage);
			}
			ingredientsTextView.Text = recipe.IngredientsText;

			FindViewById (Resource.Id.ingredientsHeader).Animation = fadeIn;
			FindViewById (Resource.Id.ingredientsHeader).Visibility = ViewStates.Visible;
			FindViewById (Resource.Id.stepsHeader).Animation = fadeIn;

			FindViewById (Resource.Id.stepsHeader).Animation = fadeIn;

			LayoutInflater inf = LayoutInflater.From (this);
			stepsLayout.RemoveAllViews ();
			var stepNumber = 1;
			foreach (Recipe.RecipeStep step in recipe.RecipeSteps) {
				View view = inf.Inflate (Resource.Layout.step_item, null);
				ImageView iv = (ImageView)view.FindViewById (Resource.Id.stepImageView);
				if (step.StepImage == null) {
					iv.Visibility = ViewStates.Gone;
				} else {
					Bitmap stepImage = AssetUtils.LoadBitmapAsset (this, step.StepImage);
					iv.SetImageBitmap (stepImage);
				}
				((TextView)view.FindViewById (Resource.Id.textStep)).Text = (stepNumber++) + ". " + step.StepText;
				stepsLayout.AddView(view);
			}
		}
		private void CreateNotification(Intent intent) {
			recipe = Recipe.FromBundle(intent.GetBundleExtra(Constants.ExtraRecipe));
			List<Notification> notificationPages = new List<Notification> ();

			int stepCount = recipe.RecipeSteps.Count;

			for (int i = 0; i < stepCount; i++) {
				Recipe.RecipeStep recipeStep = recipe.RecipeSteps [i];
				var style = new NotificationCompat.BigTextStyle ();
				style.BigText (recipeStep.StepText);
				style.SetBigContentTitle (String.Format (Resources.GetString (Resource.String.step_count), i + 1, stepCount));
				style.SetSummaryText ("");
				var builder = new NotificationCompat.Builder (this);
				builder.SetStyle (style);
				notificationPages.Add (builder.Build ());
			}

			var notifBuilder = new NotificationCompat.Builder(this);

			if (recipe.RecipeImage != null) {
				Bitmap recipeImage = Bitmap.CreateScaledBitmap(
					AssetUtils.LoadBitmapAsset(this, recipe.RecipeImage),
					Constants.NotificationImageWidth, Constants.NotificationImageHeight, false);
				notifBuilder.SetLargeIcon(recipeImage);
			}
			notifBuilder.SetContentTitle (recipe.TitleText);
			notifBuilder.SetContentText (recipe.SummaryText);
			notifBuilder.SetSmallIcon (Resource.Mipmap.ic_notification_recipe);

			Notification notification = notifBuilder.Extend(new NotificationCompat.WearableExtender().AddPages(notificationPages)).Build();
			notificationManager.Notify (Constants.NotificationId, notification);
		}
		private void LoadRecipe () {
			JSONObject jsonObject = AssetUtils.LoadJSONAsset (this, recipeName);
			if (jsonObject != null) {
				recipe = Recipe.FromJson (this, jsonObject);
				if (recipe != null)
					DisplayRecipe (recipe);
			}
		}
Beispiel #5
0
		public static Recipe FromBundle(Bundle bundle) {
			var recipe = new Recipe ();
			recipe.TitleText = bundle.GetString (Constants.RecipeFieldTitle);
			recipe.SummaryText = bundle.GetString (Constants.RecipeFieldSummary);
			recipe.RecipeImage = bundle.GetString (Constants.RecipeFieldImage);
			recipe.IngredientsText = bundle.GetString (Constants.RecipeFieldIngredients);
			var stepBundles = bundle.GetParcelableArrayList (Constants.RecipeFieldSteps);
			if (stepBundles != null) {
				foreach (IParcelable stepBundle in stepBundles) {
					recipe.RecipeSteps.Add (RecipeStep.FromBundle ((Bundle)stepBundle));
				}
			}
			return recipe;
		}