Commit 210a9bf5 authored by Tup's avatar Tup

Listview is in the nav drawer activity na

parent 04405aae
......@@ -5,7 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="C:\Program Files\Android\Android Studio\gradle\gradle-2.14.1" />
<option name="gradleHome" value="D:\Software\Android Studio\gradle\gradle-2.14.1" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
......
......@@ -27,22 +27,6 @@
</value>
</option>
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
</expanded-state>
<selected-state>
<State>
<id>Android</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
......@@ -59,19 +43,4 @@
<component name="ProjectType">
<option name="id" value="Android" />
</component>
<component name="masterDetails">
<states>
<state key="ScopeChooserConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
package com.testapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
......@@ -15,11 +16,37 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testapp.entities.CData;
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 NavDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private CanteeneoApiInterface service;
private static Context c;
private DishAdapter adapter;
private ArrayList<Dish> dishes = new ArrayList<>();
public static String EXTRA_MESSAGE = "com.testapp.MESSAGE";
@Override
......@@ -55,9 +82,52 @@ public class NavDrawerActivity extends AppCompatActivity
public void goToSearchPage(String query) {
Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra("query", query);
startActivity(intent);
String dishName = 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);
getAllData();
searchDish(dishName);
}
private void getAllData() {
Call<CData> call = service.getAll();
call.enqueue(new Callback<CData>() {
@Override
public void onResponse(Call<CData> call, Response<CData> response) {
int statusCode = response.code();
CData cdata = response.body();
}
@Override
public void onFailure(Call<CData> call, Throwable throwable) {
}
});
}
@Override
......@@ -128,4 +198,41 @@ public class NavDrawerActivity extends AppCompatActivity
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void searchDish(String name) {
Call<List<Dish>> call = service.searchByName(name);
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();
}
}
};
}
......@@ -13,4 +13,11 @@
tools:context="com.testapp.NavDrawerActivity"
tools:showIn="@layout/app_bar_nav_drawer">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
......@@ -11,13 +11,5 @@
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.testapp.SearchActivity"
tools:showIn="@layout/activity_search">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
tools:showIn="@layout/activity_search" >
</RelativeLayout>
......@@ -25,4 +25,10 @@
<string name="title_activity_search">SearchActivity</string>
<string name="server_ip">192.168.43.250</string>
<string-array name="sampleEntries">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
</resources>
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