Skip to content

Commit

Permalink
Add SearchPageFragment and SearchRequestHttpBuilder. (They are not fu…
Browse files Browse the repository at this point in the history
…lly completed)
  • Loading branch information
Sheng-Xuan committed Feb 9, 2017
1 parent c8f0ef9 commit 6301fc3
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.example.team_pigeon.movie_pigeon;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import org.example.team_pigeon.movie_pigeon.models.Movie;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Handler;

import static android.content.ContentValues.TAG;

/**
* Created by SHENGX on 2017/2/5.
*/

public class SearchPageFragment extends Fragment {
private Button btnSearch;
private RadioGroup rgrpSearchBy;
private RadioButton rbtnTitle,rbtnActor,rbtnCountry;
private EditText etSearch;
private android.app.FragmentManager fragmentManager;
private SearchRequestHttpBuilder searchRequestHttpBuilder;
private ArrayList<Movie> movies;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_search_page, container, false);
fragmentManager = getFragmentManager();
btnSearch = (Button) view.findViewById(R.id.button_search);
rgrpSearchBy = (RadioGroup)view.findViewById(R.id.rg_search_by);
etSearch = (EditText)view.findViewById(R.id.editText_search);

rgrpSearchBy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.button_title:
etSearch.setHint("Search movies by title...");
break;
case R.id.button_actor:
etSearch.setHint("Search movies by actor...");
break;
case R.id.button_country:
etSearch.setHint("Search movies by country...");
break;
}
}
});

btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MovieListFragment movieListFragment = new MovieListFragment();
Bundle arguments = new Bundle();
searchRequestHttpBuilder = new SearchRequestHttpBuilder(etSearch.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
try {
searchRequestHttpBuilder.run();
} catch (IOException e) {
e.printStackTrace();
}
if (searchRequestHttpBuilder.getMovies()!=null){

}
}
}).start();

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

movies = searchRequestHttpBuilder.getMovies();
arguments.putSerializable("movies",movies);
movieListFragment.setArguments(arguments);
fragmentTransaction.replace(R.id.fl_content,movieListFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}

//To create a list of movies for testing purpose
public ArrayList<Movie> testCase(int n){
ArrayList<Movie> movies = new ArrayList<>();
for(int i=0;i<n;i++){
Movie movie = new Movie("movie"+i,i+"");
movie.setPosterURL("https://s-media-cache-ak0.pinimg.com/736x/e8/5f/e6/e85fe6cf9815c33aa93f1656623b4442.jpg");
movie.setGenres("Action, Comedy, Short");
movie.setPlot("In the deadliest incident, 53 people died in one village after an avalanche in Nuristan, a north-eastern Afghan province on the Pakistan border. Thirteen people were also killed in an avalanche in northern Pakistan, nine of them in the town of Chitral. Dozens of houses have been destroyed and people were reported to have frozen to death, trapped in cars. Afghanistan's minister of state natural disasters, Wais Ahmad Barmak, confirmed the number of dead to BBC Afghan. There were also avalanches to the north of the Afghan capital, Kabul. \"Avalanches have buried two entire villages,\" a spokesman for the ministry told news agency AFP of the Barg Matal area in Nuristan. The neighbouring mountainous province of Badakhshan was also badly hit by snow storms.");
movies.add(movie);
}
return movies;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.example.team_pigeon.movie_pigeon;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.example.team_pigeon.movie_pigeon.models.Movie;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
* Created by SHENGX on 2017/2/8.
*/

public class SearchRequestHttpBuilder {
private Request request;
private Response response;
private String keywords;
private String url = new String("http://128.199.231.190:8080/api/movies/title");
private final OkHttpClient client = new OkHttpClient();
private Gson gson = new Gson();
private ArrayList<Movie> movies;
public SearchRequestHttpBuilder(String keywords){
this.keywords = keywords;
}
public void run() throws IOException {
request = new Request.Builder().url(url).header("title","%"+keywords+"%").addHeader("Authorization", "Basic c29uZ0B0ZXN0LmNvbTp0ZXN0").build();
response = client.newCall(request).execute();
if(!response.isSuccessful()){
throw new IOException("Unexpected code" + response);
}
this.movies = gson.fromJson(response.body().charStream(), new TypeToken<ArrayList<Movie>>(){}.getType());
}

public ArrayList<Movie> getMovies(){
return this.movies;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_search_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/pigeon"
android:id="@+id/imageView2" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg_search_by"
android:layout_gravity="center">

<TextView
android:text="Search By"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="15sp"
android:layout_weight="1" />

<RadioButton
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/button_title"
android:layout_weight="1" />

<RadioButton
android:text="Actor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_actor"
android:layout_weight="1" />

<RadioButton
android:text="Country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_country"
android:layout_weight="1" />
</RadioGroup>

<EditText
android:layout_width="300dp"
android:layout_height="60dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText_search"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Search movies by title..."/>

<Button
android:text="Search"
android:id="@+id/button_search"
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_height="60dp" />
</LinearLayout>

0 comments on commit 6301fc3

Please sign in to comment.