-
Notifications
You must be signed in to change notification settings - Fork 308
/
MenuActivity.java
111 lines (94 loc) · 3.98 KB
/
MenuActivity.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
104
105
106
107
108
109
110
111
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.teatime;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.test.espresso.IdlingResource;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.example.android.teatime.IdlingResource.SimpleIdlingResource;
import com.example.android.teatime.model.Tea;
import java.util.ArrayList;
public class MenuActivity extends AppCompatActivity implements ImageDownloader.DelayerCallback {
Intent mTeaIntent;
public final static String EXTRA_TEA_NAME = "com.example.android.teatime.EXTRA_TEA_NAME";
// The Idling Resource which will be null in production.
@Nullable
private SimpleIdlingResource mIdlingResource;
/**
* Only called from test, creates and returns a new {@link SimpleIdlingResource}.
*/
@VisibleForTesting
@NonNull
public IdlingResource getIdlingResource() {
if (mIdlingResource == null) {
mIdlingResource = new SimpleIdlingResource();
}
return mIdlingResource;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Toolbar menuToolbar = (Toolbar) findViewById(R.id.menu_toolbar);
setSupportActionBar(menuToolbar);
getSupportActionBar().setTitle(getString(R.string.menu_title));
// Get the IdlingResource instance
getIdlingResource();
}
/**
* We call ImageDownloader.downloadImage from onStart or onResume instead of in onCreate
* to ensure there is enougth time to register IdlingResource if the download is done
* too early (i.e. in onCreate)
*/
@Override
protected void onStart() {
super.onStart();
ImageDownloader.downloadImage(this, MenuActivity.this, mIdlingResource);
}
/**
* When the thread in {@link ImageDownloader} is finished, it will return an ArrayList of Tea
* objects via the callback's onDone().
*/
@Override
public void onDone(ArrayList<Tea> teas) {
//TextView testing =(TextView)findViewById(R.id.textView);
//testing.setText("Changed");
// Create a {@link TeaAdapter}, whose data source is a list of {@link Tea}s.
// The adapter know how to create grid items for each item in the list.
GridView gridview = (GridView) findViewById(R.id.tea_grid_view);
TeaMenuAdapter adapter = new TeaMenuAdapter(this, R.layout.grid_item_layout, teas);
gridview.setAdapter(adapter);
// Set a click listener on that View
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Tea item = (Tea) adapterView.getItemAtPosition(position);
// Set the intent to open the {@link OrderActivity}
mTeaIntent = new Intent(MenuActivity.this, OrderActivity.class);
String teaName = item.getTeaName();
mTeaIntent.putExtra(EXTRA_TEA_NAME, teaName);
startActivity(mTeaIntent);
}
});
}
}