Skip to content

Commit

Permalink
Advanced Features with a demo GIF file
Browse files Browse the repository at this point in the history
Added Advanced Features:
• Advanced: Robust error handling, check if internet is available,
handle error cases, network failures
• Advanced: Use the ActionBar SearchView or custom layout as the query
box instead of an EditText
• Advanced: User can share an image to their friends or email it to
themselves
• Advanced: Replace Filter Settings Activity with a lightweight modal
overlay
• Added a Cancel button in the Filter Setting Fragment to easily dismiss
the operation without changing anything.
  • Loading branch information
dperiwal committed Sep 24, 2014
1 parent 8705333 commit 779df98
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 21 deletions.
2 changes: 2 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

<application
android:allowBackup="true"
Expand Down
Binary file added DamodarGridImageSearch2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-hdpi/ic_action_search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/ic_action_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-mdpi/ic_action_search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_action_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-xhdpi/ic_action_search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_action_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/drawable-xxhdpi/ic_action_search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_action_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions res/drawable/options_dialog_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#CCCCCC"/>
</shape>
5 changes: 3 additions & 2 deletions res/layout/activity_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_height="match_parent"
tools:context="com.codepath.gridimagesearch.activities.SearchActivity" >

<EditText
<!-- <EditText
android:id="@+id/etQuery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -25,12 +25,13 @@
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="onImageSearch"
android:text="@string/search_label" />
android:text="@string/search_label" /> -->

<GridView
android:id="@+id/gvResults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
Expand Down
31 changes: 25 additions & 6 deletions res/layout/activity_search_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
android:id="@+id/btnSaveOptions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="@drawable/options_dialog_border"
tools:context="com.codepath.gridimagesearch.activities.SearchOptionsActivity" >

<TextView
Expand All @@ -12,8 +17,11 @@
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#CCFF99"
android:gravity="center_horizontal"
android:text="@string/adv_search_options_text"
android:textSize="24sp" >
android:textSize="18sp" >

</TextView>

<TextView
Expand Down Expand Up @@ -101,13 +109,24 @@
android:hint="@string/site_url_text" />

<Button
android:id="@+id/button1"
android:id="@+id/saveOptionsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvSiteFilterLabel"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/save_options_text" />

<Button
android:id="@+id/cancelOptionsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etSiteFilter"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/save_options_text"
android:onClick="saveSearchOptions"/>
android:layout_toRightOf="@+id/saveOptionsButton"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="@string/cancel_options_text" />



</RelativeLayout>
9 changes: 8 additions & 1 deletion res/menu/image_display.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>


<item
android:id="@+id/action_share"
android:orderInCategory="99"
android:showAsAction="ifRoom"
android:icon="@drawable/ic_action_share"
android:title="@string/action_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<string name="image_type_text">Image Type</string>
<string name="site_filter_text">Site Filter</string>
<string name="save_options_text">Save</string>
<string name="cancel_options_text">Cancel</string>
<string name="action_share">Share</string>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codepath.gridimagesearch.activities;

import com.codepath.gridimagesearch.models.SearchOptions;

public interface GetNewSearchOptions {
void setNewSearchOptions (SearchOptions newSearchOptions);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.codepath.gridimagesearch.activities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.codepath.gridimagesearch.R;
import com.codepath.gridimagesearch.models.ImageResult;
import com.codepath.gridimagesearch.utils.Utils;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

/**
Expand All @@ -21,6 +25,11 @@
*
*/
public class ImageDisplayActivity extends Activity {

private ImageView ivImageResult;
MenuItem shareItem;
private ShareActionProvider shareActionProvider;
private final static String IMAGE_LOADING_PROBLEM = "Problem loading the image. ";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -43,15 +52,30 @@ protected void onCreate(Bundle savedInstanceState) {
// Pull out the url from the intent
ImageResult imageResult = (ImageResult) getIntent().getSerializableExtra("result");
// Find the image view
ImageView ivImageResult = (ImageView) findViewById(R.id.ivImageResult);
ivImageResult = (ImageView) findViewById(R.id.ivImageResult);
// Load the url in the image view
Picasso.with(this).load(imageResult.fullUrl).into(ivImageResult);
Picasso.with(this).load(imageResult.fullUrl).resize(600, 600).centerInside().into(ivImageResult, new Callback() {
@Override
public void onSuccess() {
// Setup share intent now that image has loaded
setupShareIntent();
}

@Override
public void onError() {
Log.i("INFO", IMAGE_LOADING_PROBLEM);
Toast.makeText(ImageDisplayActivity.this, IMAGE_LOADING_PROBLEM, Toast.LENGTH_SHORT).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_display, menu);
shareItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
shareItem.setVisible(false);
return true;
}

Expand All @@ -66,4 +90,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

// Gets the image URI and setup the associated share intent to hook into the provider
public void setupShareIntent() {
// Fetch Bitmap Uri locally
Uri bmpUri = Utils.getLocalBitmapUri(ivImageResult);
// Create share intent as described above
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Attach share event to the menu item provider
shareActionProvider.setShareIntent(shareIntent);
shareItem.setVisible(true);
}
}
33 changes: 25 additions & 8 deletions src/com/codepath/gridimagesearch/activities/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -24,6 +26,7 @@

import com.codepath.gridimagesearch.R;
import com.codepath.gridimagesearch.adapters.ImageResultsAdapter;
import com.codepath.gridimagesearch.fragments.SearchOptionsFragment;
import com.codepath.gridimagesearch.models.ImageResult;
import com.codepath.gridimagesearch.models.SearchOptions;
import com.codepath.gridimagesearch.utils.EndlessScrollListener;
Expand All @@ -43,11 +46,11 @@
* @author Damodar Periwal
*
*/
public class SearchActivity extends Activity {
public class SearchActivity extends /* Activity */ FragmentActivity implements GetNewSearchOptions {
static final int PAGE_SIZE = 8;
static final int REQUEST_CODE = 50;
static final String SEARCH_OPTION_KEY = "search_options";
static final String NETWORK_UNAVAILABLE_MSG = "Network not available. Aborting.";
static public final String SEARCH_OPTIONS_KEY = "search_options";
static public final String NETWORK_UNAVAILABLE_MSG = "Network not available. Aborting.";

private EditText etQuery;
private GridView gvResults;
Expand Down Expand Up @@ -222,26 +225,40 @@ public boolean onOptionsItemSelected(MenuItem item) {
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
// Create an intent for settings activity
/* // Create an intent for settings activity
Intent i = new Intent(SearchActivity.this, SearchOptionsActivity.class);
// Pass image data in the intent
i.putExtra(SEARCH_OPTION_KEY, searchOptions);
i.putExtra(SEARCH_OPTIONS_KEY, searchOptions);
// Launch the new activity
startActivityForResult(i, REQUEST_CODE);
startActivityForResult(i, REQUEST_CODE);*/

showSearchOptionsDialog();


return true;
}
return super.onOptionsItemSelected(item);
}


private void showSearchOptionsDialog() {
FragmentManager fm = getSupportFragmentManager();
SearchOptionsFragment optionsDialog = SearchOptionsFragment.newInstance(searchOptions);
optionsDialog.show(fm, "fragment_search_options");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
// Update the search options for subsequent queries
searchOptions = (SearchOptions) data
.getSerializableExtra(SEARCH_OPTION_KEY);
setNewSearchOptions((SearchOptions) data
.getSerializableExtra(SEARCH_OPTIONS_KEY));
// Log.i("INFO, return value", searchOptions.toString());
}
}

public void setNewSearchOptions(SearchOptions newSearchOptions) {
searchOptions = newSearchOptions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

Expand All @@ -25,6 +27,8 @@ public class SearchOptionsActivity extends Activity {
private Spinner spColorFilter;
private Spinner spImageType;
private EditText etSiteFilter;
private Button btnSaveOptions;
private Button btnCancelOptions;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -36,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
private void setupResources() {

// Collect the existing search options values passed in the intent.
SearchOptions currentSearchOptions = (SearchOptions) getIntent().getSerializableExtra(SearchActivity.SEARCH_OPTION_KEY);
SearchOptions currentSearchOptions = (SearchOptions) getIntent().getSerializableExtra(SearchActivity.SEARCH_OPTIONS_KEY);
// Image size
spImageSize = (Spinner) findViewById(R.id.spImageSize);
// Create an ArrayAdapter using the string array and a default spinner layout
Expand Down Expand Up @@ -73,6 +77,27 @@ private void setupResources() {
// Site filter
etSiteFilter = (EditText) findViewById(R.id.etSiteFilter);
etSiteFilter.setText(currentSearchOptions.siteFilterUrl);

// Save button
btnSaveOptions = (Button) findViewById(R.id.saveOptionsButton);
btnSaveOptions.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
saveSearchOptions(v);
}
});

// Cancel button
btnCancelOptions = (Button) findViewById(R.id.cancelOptionsButton);
btnCancelOptions.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
finish();;
}
});

}

public void saveSearchOptions(View v) {
Expand All @@ -85,7 +110,7 @@ public void saveSearchOptions(View v) {

// Return the latest search options back to the caller using the new SearchOption object
Intent i = new Intent();
i.putExtra(SearchActivity.SEARCH_OPTION_KEY, newSearchOptions);
i.putExtra(SearchActivity.SEARCH_OPTIONS_KEY, newSearchOptions);
setResult(RESULT_OK, i);
finish();
}
Expand Down
Loading

0 comments on commit 779df98

Please sign in to comment.