Skip to content

Commit

Permalink
Adde comments and some share code
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikramjeet Singh authored and Vikramjeet Singh committed Feb 13, 2015
1 parent ff25922 commit b021d8c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.example.vikramjeet.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -12,6 +18,8 @@

public class ImageDetailActivity extends ActionBarActivity {

private TouchImageView ivImageDetail;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -21,7 +29,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Get image Url
String imageUrl = image.getUrl();
// Find imageView
TouchImageView ivImageDetail = (TouchImageView) findViewById(R.id.ivImageDetail);
ivImageDetail = (TouchImageView) findViewById(R.id.ivImageDetail);
// Load the image into image view using picasso
Picasso.with(this)
.load(imageUrl)
Expand Down Expand Up @@ -51,11 +59,31 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

// if (id == R.id.home) {
// onBackPressed();
// return true;
// }

// Share option selected
if (id == R.id.miShare) {
shareImage();
return true;
}
// if (id == R.id.home) {
// onBackPressed();
// return true;
// }
return super.onOptionsItemSelected(item);
}

public void shareImage() {
Drawable mDrawable = ivImageDetail.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

String path = MediaStore.Images.Media.insertImage(ImageDetailActivity.this.getContentResolver(),
mBitmap, "Image description", null);

Uri uri = Uri.parse(path);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
emailIntent.setType("image/png");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@


public class ImageSearchActivity extends ActionBarActivity implements SettingsFilterDialog.SettingsFilterDialogListener {
private final int kNumberOfImagesPerPage = 8;
private final int REQUEST_CODE = 200;
public static final String url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=";
// private EditText etQuery;
private final String url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=";
private StaggeredGridView gvResults;
private ArrayList<Image> images;
private ImageAdapter adapter;
Expand Down Expand Up @@ -104,10 +104,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
// // Extract name value from result extras
// searchFilter = (Filter) data.getSerializableExtra("Filter");
// }
super.onActivityResult(requestCode, resultCode, data);
}

public String applyFilters(String url, String query) {
Expand Down Expand Up @@ -146,7 +143,6 @@ public boolean onQueryTextSubmit(String query) {
// Create Network client
httpClient = new AsyncHttpClient();
// Create search url
// searchURL = url + query;
searchURL = applyFilters(url, query);

if(isNetworkAvailable()) {
Expand Down Expand Up @@ -177,6 +173,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
//noinspection SimplifiableIfStatement
case R.id.action_settings:
return true;
// Settings option selected
case R.id.miFilters:
showSettings();
return true;
Expand All @@ -188,11 +185,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Helper methods

private void showSettings() {
// // Create an Intent
// Intent settingsIntent = new Intent(ImageSearchActivity.this, SettingsActivity.class);
//// settingsIntent.putExtra("Filter", searchFilter);
// // Start the activity
// startActivityForResult(settingsIntent, REQUEST_CODE);
// Creating Dialog Fragment
FragmentManager fm = getSupportFragmentManager();
SettingsFilterDialog filterDialog = SettingsFilterDialog.newInstance("Advanced Filters");
filterDialog.show(fm, "fragment_settings_filter_dialog");
Expand All @@ -211,16 +204,16 @@ public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
imageListJSON = response.getJSONObject("responseData").getJSONArray("results");
// Cleat list for the initial search (not for pagination)
images.clear();
// Add images to the list
// images.addAll(Image.fromJSONArray(imageListJSON));
//// Refresh data by notifying adapter
// adapter.notifyDataSetChanged();
// Add images to the list
// images.addAll(Image.fromJSONArray(imageListJSON));
// Refresh data by notifying adapter
// adapter.notifyDataSetChanged();

// Using another approach of updating adapter itself and hence it will trigger the change to datasource
adapter.addAll(Image.fromJSONArray(imageListJSON));

} catch (JSONException e) {
// Log.e("ImageSearchActivity", "Failed to parse response json");
Log.e("ImageSearchActivity", "Failed to parse response json");
e.printStackTrace();
}
}
Expand All @@ -234,7 +227,7 @@ public void onFailure(int statusCode, Header[] headers, String responseString, T
}

public void fetchImagesWhileScrolling(String url, int page) {
int offset = 8*(page-1);
int offset = kNumberOfImagesPerPage*(page-1);
String finalURL = url + "&start=" + offset;

httpClient.get(finalURL, new JsonHttpResponseHandler() {
Expand Down Expand Up @@ -262,6 +255,7 @@ public void onFailure(int statusCode, Header[] headers, String responseString, T
});
}

// Check if Internet is available
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class SettingsActivity extends ActionBarActivity {

private final int kDefaultSelection = 0;
private String imageSize;
private String imageColor;
private String imageType;
Expand Down Expand Up @@ -46,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
spinnerImageSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageSize = (String) parent.getItemAtPosition(position);
}
}
Expand All @@ -71,7 +72,7 @@ public void onNothingSelected(AdapterView<?> parent) {
spinnerColorFilter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageColor = (String) parent.getItemAtPosition(position);
}
}
Expand All @@ -95,7 +96,7 @@ public void onNothingSelected(AdapterView<?> parent) {
spinnerImageSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageType = (String) parent.getItemAtPosition(position);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
public class SettingsFilterDialog extends DialogFragment implements TextView.OnClickListener {

private final int kDefaultSelection = 0;
private String imageSize;
private String imageColor;
private String imageType;
Expand Down Expand Up @@ -77,10 +78,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
cancelButton = (Button) view.findViewById(R.id.btnCancel);
cancelButton.setOnClickListener(this);


// String title = getArguments().getString("title");
// getDialog().setTitle("title");

// Load spinner view
Spinner spinnerImageSize = (Spinner) view.findViewById(R.id.spinnerImageSize);
// Create an ArrayAdapter using the string array using custom spinner layout
Expand All @@ -94,7 +91,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
spinnerImageSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageSize = (String) parent.getItemAtPosition(position);
}
}
Expand All @@ -119,7 +116,7 @@ public void onNothingSelected(AdapterView<?> parent) {
spinnerColorFilter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageColor = (String) parent.getItemAtPosition(position);
}
}
Expand All @@ -143,7 +140,7 @@ public void onNothingSelected(AdapterView<?> parent) {
spinnerImageSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
if (position > kDefaultSelection) {
imageType = (String) parent.getItemAtPosition(position);
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/example/vikramjeet/models/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Image implements Serializable {
private String title;
private String height;
private String width;
private String titleNoFormatting;

public Image(JSONObject json) {
try {
Expand All @@ -27,6 +28,8 @@ public Image(JSONObject json) {
title = json.getString("title");
height = json.getString("height");
width = json.getString("width");
titleNoFormatting = json.getString("titleNoFormatting");

} catch(JSONException e) {
Log.e("Image", "Failed to parse json in constructor");
e.printStackTrace();
Expand Down Expand Up @@ -70,4 +73,8 @@ public String getWidth() {
return width;
}

public String getTitleNoFormatting() {
return titleNoFormatting;
}

}

0 comments on commit b021d8c

Please sign in to comment.