Commit 4f4cf8d4 authored by Willard's avatar Willard

Remove redundant SearchActivity and related files

parent 7b9c93e2
......@@ -29,10 +29,6 @@
android:name=".NavDrawerActivity"
android:label="@string/title_activity_nav_drawer"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name=".SearchActivity"
android:label="@string/title_activity_search"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name=".DishViewActivity"
android:label="@string/title_activity_blahblah"
......
package com.testapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testapp.entities.Dish;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class SearchActivity extends AppCompatActivity {
private CanteeneoApiInterface service;
private static Context c;
private DishAdapter adapter;
private ArrayList<Dish> dishes = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
String dishName = getIntent().getStringExtra("query");
ListView lv = (ListView) findViewById(R.id.listView);
adapter = new DishAdapter(this, dishes);
lv.setAdapter(adapter);
c = getApplicationContext();
File httpCacheDirectory = new File(this.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient().newBuilder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://" + getResources().getString(R.string.server_ip) + ":5000/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
service = retrofit.create(CanteeneoApiInterface.class);
searchDish(dishName);
}
private String getChecked(int layoutId) {
LinearLayout checkboxes = (LinearLayout)findViewById(layoutId);
String checked = "";
for(int i = 0; i < checkboxes.getChildCount(); i++) {
View v = checkboxes.getChildAt(i);
if(v instanceof CheckBox) {
CheckBox c = (CheckBox)v;
if(c.isChecked()) {
checked += c.getId();
}
}
}
return checked;
}
private void searchDish(String name) {
Call<List<Dish>> call = service.searchByName(name, getChecked(R.id.type_checkboxes), getChecked(R.id.cuisine_checkboxes), getChecked(R.id.location_checkboxes), 0, 1000);
call.enqueue(new Callback<List<Dish>>() {
@Override
public void onResponse(Call<List<Dish>> call, Response<List<Dish>> response) {
List<Dish> newDishes = response.body();
dishes.clear();
dishes.addAll(newDishes);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Dish>> call, Throwable t) {
}
});
}
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response origResponse = chain.proceed(chain.request());
if (AppUtils.isNetworkAvailable(c)) {
int maxAge = 60; // read from cache for 1 minute
return origResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
return origResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
};
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.testapp.SearchActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_search" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.testapp.SearchActivity"
tools:showIn="@layout/activity_search" >
</RelativeLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment