Skip to content

Commit b2983c8

Browse files
committed
implement countries request
1 parent fdd12ec commit b2983c8

15 files changed

+822
-9
lines changed

app/app.iml

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<orderEntry type="library" exported="" name="gson-2.2.4" level="project" />
9191
<orderEntry type="library" exported="" name="ormlite-android-4.48" level="project" />
9292
<orderEntry type="library" exported="" name="appcompat-v7-22.0.0" level="project" />
93+
<orderEntry type="library" exported="" name="swipy-1.2.0" level="project" />
9394
<orderEntry type="library" exported="" name="ormlite-core-4.48" level="project" />
9495
<orderEntry type="library" exported="" name="MPAndroidChart-v2.0.9" level="project" />
9596
<orderEntry type="library" exported="" name="eventbus-2.4.0" level="project" />

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ dependencies {
3030
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
3131
compile 'de.greenrobot:eventbus:2.4.0'
3232
compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
33+
compile 'com.github.orangegangsters:swipy:1.2.0@aar'
3334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.github.kingaza.aspree;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
import android.os.Bundle;
6+
import android.support.annotation.Nullable;
7+
import android.support.v4.app.ListFragment;
8+
import android.util.Log;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.ArrayAdapter;
13+
14+
import com.github.kingaza.aspree.model.CountryModel;
15+
import com.github.kingaza.aspree.protocol.Country;
16+
import com.github.kingaza.aspree.protocol.Pagination;
17+
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
18+
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
19+
20+
import retrofit.Callback;
21+
import retrofit.RetrofitError;
22+
import retrofit.client.Response;
23+
24+
/**
25+
* Created by abu on 2015/5/1.
26+
*/
27+
public class CountryFragment extends ListFragment {
28+
29+
private static final String TAG = "CountryFragment";
30+
31+
SwipyRefreshLayout mSwipyRefreshLayout;
32+
private List<String> mCountryNameList = new ArrayList<String>();
33+
private ArrayAdapter<String> mAdapter;
34+
35+
private Pagination mPagination = new Pagination();
36+
37+
Callback<CountryModel.Countries> mCallback = new Callback<CountryModel.Countries>() {
38+
@Override
39+
public void success(CountryModel.Countries countries, Response response) {
40+
mPagination = (Pagination) countries;
41+
Log.i(TAG, "Got page " + mPagination.current_page + "/" + mPagination.pages);
42+
for (Country country : countries.countries) {
43+
mCountryNameList.add(country.name);
44+
}
45+
mAdapter.notifyDataSetChanged();
46+
}
47+
48+
@Override
49+
public void failure(RetrofitError error) {
50+
}
51+
};
52+
53+
@Override
54+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
55+
View view = inflater.inflate(R.layout.fragment_country, container, false);
56+
mSwipyRefreshLayout = (SwipyRefreshLayout) view.findViewById(R.id.swipe_layout);
57+
mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
58+
59+
@Override
60+
public void onRefresh(SwipyRefreshLayoutDirection direction) {
61+
62+
if (direction == SwipyRefreshLayoutDirection.BOTTOM &&
63+
mPagination.current_page < mPagination.pages) {
64+
CountryModel.getCountriesInPage(mPagination.current_page+1, mCallback);
65+
}
66+
67+
mSwipyRefreshLayout.setRefreshing(false);
68+
}
69+
});
70+
return view;
71+
}
72+
73+
@Override
74+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
75+
super.onActivityCreated(savedInstanceState);
76+
CountryModel.getCountries(mCallback);
77+
}
78+
79+
@Override
80+
public void onCreate(Bundle savedInstanceState) {
81+
super.onCreate(savedInstanceState);
82+
setHasOptionsMenu(true);
83+
84+
mAdapter = new ArrayAdapter<String>(getActivity(),
85+
android.R.layout.simple_list_item_1, mCountryNameList);
86+
setListAdapter(mAdapter);
87+
}
88+
89+
90+
}

app/src/main/java/com/github/kingaza/aspree/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void onNavigationDrawerItemSelected(int position) {
6666
break;
6767
case 1:
6868
fragmentManager.beginTransaction()
69-
.replace(R.id.container, new Fragment())
69+
.replace(R.id.container, new CountryFragment())
7070
.commit();
7171
break;
7272
case 2:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.kingaza.aspree;
2+
3+
/**
4+
* Created by abu on 2015/5/1.
5+
*/
6+
public class SpreeConst {
7+
public static final String API_URL = "http://112.124.29.25:70/api";
8+
9+
public static final String SPREE_TOKEN = "9b87647a648fbc2160d0d221ee382deae10d9dfbf4453c0f";
10+
}

app/src/main/java/com/github/kingaza/aspree/TaxonomyFragment.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.annotation.SuppressLint;
66
import android.os.Bundle;
77
import android.os.Build;
8+
import android.util.Log;
89
import android.content.Context;
910
import android.graphics.Color;
1011
import android.support.annotation.Nullable;
@@ -13,7 +14,6 @@
1314
import android.view.Menu;
1415
import android.view.MenuInflater;
1516
import android.view.MenuItem;
16-
import android.view.View.OnClickListener;
1717
import android.view.View;
1818
import android.view.ViewGroup;
1919
import android.widget.TextView;
@@ -37,6 +37,8 @@
3737

3838
public class TaxonomyFragment extends ListFragment {
3939

40+
private static String TAG = "TaxonomyFragment";
41+
4042
static class SimpleAdapter extends ArrayAdapter<Item>
4143
implements PinnedSectionListAdapter {
4244

@@ -53,7 +55,7 @@ public SimpleAdapter(Context context, int resource, int textViewResourceId) {
5355
public void retrieveFromTaxonomy(Taxonomies taxonomies) {
5456
clear();
5557
int sectionPosition = 0, listPosition = 0;
56-
int sectionsNumber = taxonomies.count;
58+
int sectionsNumber = taxonomies.taxonomies.size();
5759
prepareSections(sectionsNumber);
5860
for (Taxonomy taxonomy : taxonomies.taxonomies) {
5961
Item section = new Item(Item.SECTION, taxonomy.name);
@@ -84,7 +86,6 @@ public View getView(int position, View convertView, ViewGroup parent) {
8486
view.setTag("" + position);
8587
Item item = getItem(position);
8688
if (item.type == Item.SECTION) {
87-
//view.setOnClickListener(PinnedSectionListActivity.this);
8889
view.setBackgroundColor(
8990
parent.getResources().getColor(
9091
COLORS[item.sectionPosition % COLORS.length]));
@@ -257,6 +258,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
257258
Callback<Taxonomies> taxonomiesCallback = new Callback<Taxonomies>() {
258259
@Override
259260
public void success(Taxonomies taxonomies, Response response) {
261+
Log.d(TAG, "Got " + taxonomies.taxonomies.size() + " taxonomies");
260262
mTaxonomies = taxonomies;
261263
updateView();
262264
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.github.kingaza.aspree.model;
2+
3+
import com.github.kingaza.aspree.SpreeConst;
4+
import com.github.kingaza.aspree.protocol.Country;
5+
import com.github.kingaza.aspree.protocol.Pagination;
6+
import com.github.kingaza.aspree.protocol.Taxonomies;
7+
import com.github.kingaza.aspree.protocol.Taxonomy;
8+
9+
import java.util.List;
10+
11+
import retrofit.Callback;
12+
import retrofit.RestAdapter;
13+
import retrofit.http.GET;
14+
import retrofit.http.Headers;
15+
import retrofit.http.Path;
16+
import retrofit.http.Query;
17+
18+
/**
19+
* Created by abu on 2015/5/1.
20+
*/
21+
public class CountryModel {
22+
23+
interface ICountry {
24+
25+
@Headers({
26+
"Content-Type: application/json",
27+
"Accept: application/json",
28+
"X-Spree-Token: " + SpreeConst.SPREE_TOKEN
29+
})
30+
@GET("/countries")
31+
void countries(Callback<Countries> callback);
32+
33+
@Headers({
34+
"Content-Type: application/json",
35+
"Accept: application/json",
36+
"X-Spree-Token: " + SpreeConst.SPREE_TOKEN
37+
})
38+
@GET("/countries")
39+
void countriesInPage(@Query("page") int page, Callback<Countries> callback);
40+
41+
@Headers({
42+
"Content-Type: application/json",
43+
"Accept: application/json",
44+
"X-Spree-Token: " + SpreeConst.SPREE_TOKEN
45+
})
46+
@GET("/countries/{id}")
47+
void country(@Path("id") int id, Callback<Country> callback);
48+
}
49+
50+
public class Countries extends Pagination {
51+
public List<Country> countries;
52+
};
53+
54+
public static void getCountries(Callback<Countries> callback) {
55+
56+
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SpreeConst.API_URL).build();
57+
ICountry spree = restAdapter.create(ICountry.class);
58+
spree.countries(callback);
59+
}
60+
61+
public static void getCountriesInPage(int page, Callback<Countries> callback) {
62+
63+
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SpreeConst.API_URL).build();
64+
ICountry spree = restAdapter.create(ICountry.class);
65+
spree.countriesInPage(page, callback);
66+
}
67+
68+
public static void getCountry(int id, Callback<Country> callback) {
69+
70+
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SpreeConst.API_URL).build();
71+
ICountry spree = restAdapter.create(ICountry.class);
72+
spree.country(id, callback);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.kingaza.aspree.model;
2+
3+
import retrofit.Callback;
4+
import retrofit.RestAdapter;
5+
import retrofit.http.GET;
6+
import retrofit.http.Path;
7+
import retrofit.http.Headers;
8+
9+
import com.github.kingaza.aspree.SpreeConst;
10+
import com.github.kingaza.aspree.protocol.Taxonomy;
11+
import com.github.kingaza.aspree.protocol.Taxonomies;
12+
13+
/**
14+
* Created by abu on 2015/4/26.
15+
*/
16+
public class TaxonomyModel {
17+
18+
interface ITaxonomy {
19+
20+
@Headers({
21+
"Content-Type: application/json",
22+
"Accept: application/json",
23+
"X-Spree-Token: " + SpreeConst.SPREE_TOKEN
24+
})
25+
@GET("/taxonomies")
26+
void taxonomies(Callback<Taxonomies> callback);
27+
28+
@Headers({
29+
"Content-Type: application/json",
30+
"Accept: application/json",
31+
"X-Spree-Token: " + SpreeConst.SPREE_TOKEN
32+
})
33+
@GET("/taxonomies/{id}")
34+
void taxonomy(@Path("id") int id, Callback<Taxonomy> callback);
35+
}
36+
37+
public static void getTaxonomies(Callback<Taxonomies> callback) {
38+
39+
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SpreeConst.API_URL).build();
40+
ITaxonomy spree = restAdapter.create(ITaxonomy.class);
41+
spree.taxonomies(callback);
42+
}
43+
44+
public static void getTaxonomy(int id, Callback<Taxonomy> callback) {
45+
46+
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(SpreeConst.API_URL).build();
47+
ITaxonomy spree = restAdapter.create(ITaxonomy.class);
48+
spree.taxonomy(id, callback);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.kingaza.aspree.protocol;
2+
3+
/**
4+
* Created by abu on 2015/5/1.
5+
*/
6+
7+
import java.util.List;
8+
9+
public class Country {
10+
public int id;
11+
public String iso_name;
12+
public String iso;
13+
public String iso3;
14+
public String name;
15+
public int numcode;
16+
public List<State> states;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.kingaza.aspree.protocol;
2+
3+
/**
4+
* Created by abu on 2015/5/1.
5+
*/
6+
public class Pagination {
7+
public int count;
8+
public int total_count;
9+
public int current_page;
10+
public int per_page;
11+
public int pages;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.kingaza.aspree.protocol;
2+
3+
/**
4+
* Created by abu on 2015/5/1.
5+
*/
6+
public class State {
7+
int id;
8+
String name;
9+
String abbr;
10+
int country_id;
11+
}

app/src/main/java/com/github/kingaza/aspree/protocol/Taxonomies.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import java.util.List;
88

9-
public class Taxonomies {
9+
public class Taxonomies extends Pagination{
1010

11-
public int count;
12-
public int current_page;
13-
public int pages;
11+
// public int count;
12+
// public int current_page;
13+
// public int pages;
1414

1515
public List<Taxonomy> taxonomies;
1616

0 commit comments

Comments
 (0)