Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Kot committed Feb 17, 2018
0 parents commit 0d947b2
Show file tree
Hide file tree
Showing 35 changed files with 1,088 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/*
.DS_Store
/build
/captures
.externalNativeBuild
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Sandwich Club Project Starter Code

## Project Overview
In this project, you will complete the **Sandwich Club** app to
show the details of each sandwich once it is selected.

## Why this Project

Building a layout and populating its fields from data received as JSON
is a common task for Android Developers. Although JSON parsing is usually
done using libraries, writing the JSON parsing for this project will
help you to better understand how it is processed.

## What Will I Learn?
Through this project, you will:
- Learn how to submit projects for review
- Practice JSON parsing to a model object
- Design an activity layout
- Populate all fields in the layout accordingly

## How Do I Complete this Project?
Download the [Sandwich Club app starter code.](https://github.com/udacity/sandwich-club-starter-code)

Design the layout for the detail activity so the different elements
display in a sensible way. Implement the JSON parsing in JsonUtils so it
produces a Sandwich Object that can be used to populate the UI that you designed.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.3"
defaultConfig {
applicationId "com.udacity.sandwichclub"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/jose/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.udacity.sandwichclub">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailActivity" />
</application>

</manifest>
130 changes: 130 additions & 0 deletions app/src/main/java/com/udacity/sandwichclub/DetailActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.udacity.sandwichclub;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.udacity.sandwichclub.model.Sandwich;
import com.udacity.sandwichclub.utils.JsonUtils;

import org.json.JSONException;

import java.util.List;

public class DetailActivity extends AppCompatActivity {

public static final String EXTRA_POSITION = "extra_position";
private static final int DEFAULT_POSITION = -1;
Sandwich sandwich;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

ImageView ingredientsIv = findViewById(R.id.image_iv);

Intent intent = getIntent();
if (intent == null) {
closeOnError();
}

assert intent != null;
int position = intent.getIntExtra(EXTRA_POSITION, DEFAULT_POSITION);
if (position == DEFAULT_POSITION) {
// EXTRA_POSITION not found in intent
closeOnError();
return;
}

String[] sandwiches = getResources().getStringArray(R.array.sandwich_details);
String json = sandwiches[position];
try {
sandwich = JsonUtils.parseSandwichJson(json);
} catch (JSONException e) {
e.printStackTrace();
}

if (sandwich == null) {
// Sandwich data unavailable
closeOnError();
return;
}

populateUI();
Picasso.with(this)
.load(sandwich.getImage())
// Error Image
.error(R.drawable.no_connection)
.into(ingredientsIv);

setTitle(sandwich.getMainName());
}

private void closeOnError() {
finish();
Toast.makeText(this, R.string.detail_error_message, Toast.LENGTH_SHORT).show();
}

private void populateUI() {
TextView alsoKnownHeader = findViewById(R.id.also_known_header);
TextView alsoKnownAs = findViewById(R.id.also_known_tv);

TextView placeOfOriginHeader = findViewById(R.id.origin_header);
TextView placeOfOrigin = findViewById(R.id.origin_tv);

TextView descriptionHeader = findViewById(R.id.description_header);
TextView description = findViewById(R.id.description_tv);

TextView ingredientsHeader = findViewById(R.id.ingreddients_header);
TextView ingredients = findViewById(R.id.ingredients_tv);


// Also Known As description
List<String> alsoKnownList = sandwich.getAlsoKnownAs();
if(alsoKnownList.isEmpty()){
alsoKnownHeader.setVisibility(View.GONE);
alsoKnownAs.setVisibility(View.GONE);
}else{
StringBuilder text = new StringBuilder();
for (String s : alsoKnownList){
text.append(s).append(", ");
}
alsoKnownAs.setText(text.substring(0, (text.length()-2)));
}

// Place of Origin description
if(sandwich.getPlaceOfOrigin().isEmpty()){
placeOfOriginHeader.setVisibility(View.GONE);
placeOfOrigin.setVisibility(View.GONE);
}else{
placeOfOrigin.setText(sandwich.getPlaceOfOrigin());
}

// Description
if(sandwich.getDescription().isEmpty()){
descriptionHeader.setVisibility(View.GONE);
description.setVisibility(View.GONE);
}else{
description.setText(sandwich.getDescription());
}

// Ingriedients List
List<String> ingredientsList = sandwich.getIngredients();
if(ingredientsList.isEmpty()){
ingredientsHeader.setVisibility(View.GONE);
ingredients.setVisibility(View.GONE);
}else{
StringBuilder text = new StringBuilder();
for (String s : ingredientsList){
text.append(s).append(", ");
}
ingredients.setText(text.substring(0, (text.length()-2)));
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/udacity/sandwichclub/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.udacity.sandwichclub;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[] sandwiches = getResources().getStringArray(R.array.sandwich_names);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, sandwiches);

//String[] sandwichesDetail = getResources().getStringArray(R.array.sandwich_details);

// Simplification: Using a ListView instead of a RecyclerView
ListView listView = findViewById(R.id.sandwiches_listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
launchDetailActivity(position);
}
});
}

private void launchDetailActivity(int position) {
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_POSITION, position);
startActivity(intent);
}
}
76 changes: 76 additions & 0 deletions app/src/main/java/com/udacity/sandwichclub/model/Sandwich.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.udacity.sandwichclub.model;

import java.util.List;

public class Sandwich {

private String mainName;
private List<String> alsoKnownAs = null;
private String placeOfOrigin;
private String description;
private String image;
private List<String> ingredients = null;

/**
* No args constructor for use in serialization
*/
public Sandwich() {
}

public Sandwich(String mainName, List<String> alsoKnownAs, String placeOfOrigin, String description, String image, List<String> ingredients) {
this.mainName = mainName;
this.alsoKnownAs = alsoKnownAs;
this.placeOfOrigin = placeOfOrigin;
this.description = description;
this.image = image;
this.ingredients = ingredients;
}

public String getMainName() {
return mainName;
}

public void setMainName(String mainName) {
this.mainName = mainName;
}

public List<String> getAlsoKnownAs() {
return alsoKnownAs;
}

public void setAlsoKnownAs(List<String> alsoKnownAs) {
this.alsoKnownAs = alsoKnownAs;
}

public String getPlaceOfOrigin() {
return placeOfOrigin;
}

public void setPlaceOfOrigin(String placeOfOrigin) {
this.placeOfOrigin = placeOfOrigin;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public List<String> getIngredients() {
return ingredients;
}

public void setIngredients(List<String> ingredients) {
this.ingredients = ingredients;
}
}
Loading

0 comments on commit 0d947b2

Please sign in to comment.