This repository has been archived by the owner on May 24, 2022. It is now read-only.
/
EcommerceActivity.java
103 lines (87 loc) · 3.77 KB
/
EcommerceActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.algolia.instantsearch.examples.ecommerce;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.algolia.instantsearch.helpers.InstantSearch;
import com.algolia.instantsearch.helpers.Searcher;
import com.algolia.instantsearch.ui.views.SearchBox;
import com.squareup.leakcanary.RefWatcher;
public class EcommerceActivity extends AppCompatActivity {
private static final String ALGOLIA_APP_ID = "latency";
private static final String ALGOLIA_INDEX_NAME = "bestbuy_promo";
private static final String ALGOLIA_API_KEY = "91e5b0d48d0ea9c1eb7e7e063d5c7750";
private FilterResultsWindow filterResultsWindow;
private Drawable arrowDown;
private Drawable arrowUp;
private Button buttonFilter;
private Searcher searcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ecommerce);
String query = "";
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
}
searcher = Searcher.create(ALGOLIA_APP_ID, ALGOLIA_API_KEY, ALGOLIA_INDEX_NAME);
new InstantSearch(this, searcher); // Initialize InstantSearch in this activity with searcher
searcher.search(query); // Show results for empty query (on app launch) / voice query (from intent)
((SearchBox) findViewById(R.id.searchBox)).disableFullScreen(); // disable fullscreen input UI on landscape
filterResultsWindow = new FilterResultsWindow.Builder(this, searcher)
.addSeekBar("salePrice", "initial price", 100)
.addSeekBar("customerReviewCount", "reviews", 100)
.addCheckBox("promoted", "Has a discount", true)
.addSeekBar("promoPrice", "price with discount", 100)
.build();
buttonFilter = (Button) findViewById(R.id.btn_filter);
buttonFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final boolean willDisplay = !filterResultsWindow.isShowing();
if (willDisplay) {
filterResultsWindow.showAsDropDown(buttonFilter);
} else {
filterResultsWindow.dismiss();
}
toggleArrow(buttonFilter, willDisplay);
}
});
}
@Override
protected void onStop() {
filterResultsWindow.dismiss();
toggleArrow(buttonFilter, false);
super.onStop();
}
@Override
protected void onDestroy() {
filterResultsWindow.dismiss();
searcher.destroy();
toggleArrow(buttonFilter, false);
super.onDestroy();
RefWatcher refWatcher = EcommerceApplication.getRefWatcher(this);
refWatcher.watch(this);
refWatcher.watch(findViewById(R.id.hits));
}
private void toggleArrow(Button b, boolean up) {
final Drawable[] currentDrawables = b.getCompoundDrawables();
final Drawable newDrawable;
if (up) {
if (arrowUp == null) {
arrowUp = getResources().getDrawable(R.drawable.arrow_up_flat);
}
newDrawable = arrowUp;
} else {
if (arrowDown == null) {
arrowDown = getResources().getDrawable(R.drawable.arrow_down_flat);
}
newDrawable = arrowDown;
}
b.setCompoundDrawablesWithIntrinsicBounds(currentDrawables[0], currentDrawables[1], newDrawable, currentDrawables[3]);
}
}