-
Notifications
You must be signed in to change notification settings - Fork 66
/
MainActivity.java
160 lines (137 loc) · 5.85 KB
/
MainActivity.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (C) 2016 Square, Inc.
*
* 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.owensbikes;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.squareup.sdk.pos.ChargeRequest;
import com.squareup.sdk.pos.CurrencyCode;
import com.squareup.sdk.pos.PosApi;
import com.squareup.sdk.pos.PosClient;
import com.squareup.sdk.pos.PosSdk;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
import static com.squareup.sdk.pos.ChargeRequest.TenderType.CARD;
import static com.squareup.sdk.pos.ChargeRequest.TenderType.CASH;
public class MainActivity extends AppCompatActivity {
private static final String ORDER_INFO = "ORDER_INFO";
private static final String ORDER_NUMBER = "ORDER_NUMBER";
private static final int FIRST_ORDER_NUMBER = 1;
private static final int CHARGE_REQUEST_CODE = 0xCAFE;
private PosClient posClient;
private DataLoader dataLoader;
private SharedPreferences orderInfoPrefs;
private ProgressBar spinner;
private TextView loadingMessage;
private BikeItemManager itemManager;
private DialogComposer dialogComposer;
private TransactionResultHandler transactionResultHandler;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (ProgressBar) findViewById(R.id.progress_bar);
loadingMessage = (TextView) findViewById(R.id.progress_bar_text);
orderInfoPrefs = getSharedPreferences(ORDER_INFO, MODE_PRIVATE);
posClient = PosSdk.createClient(this, BuildConfig.CLIENT_ID);
dialogComposer = new DialogComposer(this, posClient);
transactionResultHandler = new TransactionResultHandler(this, posClient, dialogComposer);
dataLoader = BikeApplication.from(this).getDataLoader();
boolean isTablet = getResources().getBoolean(R.bool.isTablet);
boolean isLandscape =
getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE;
boolean twoLists = isTablet ^ isLandscape;
AdapterController adapterController =
twoLists ? new AdapterController.TwoLists(this) : new AdapterController.SingleList(this);
itemManager = new BikeItemManager(this, adapterController);
itemManager.loadState(savedInstanceState);
loadItems();
View checkoutButton = findViewById(R.id.checkout_button);
checkoutButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
checkout();
}
});
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
itemManager.saveState(outState);
}
private void loadItems() {
spinner.setVisibility(View.VISIBLE);
loadingMessage.setVisibility(View.VISIBLE);
dataLoader.loadItemsFromAssets(new DataLoader.Listener() {
@Override public void onDataLoaded(List<BikeItem> loadedItems) {
spinner.setVisibility(View.INVISIBLE);
loadingMessage.setVisibility(View.INVISIBLE);
// Item manager will loadState data into the recycler views.
itemManager.loadItems(loadedItems);
}
@Override public void onLoadFailed(CharSequence message) {
spinner.setVisibility(View.INVISIBLE);
loadingMessage.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
public void checkout() {
int amount = itemManager.getTotal();
String note = itemManager.getNote();
Set<ChargeRequest.TenderType> tenderTypes = EnumSet.of(CARD, CASH);
// Order number is an integer stored in a SharedPreference as an example of
// requestMetadata usage.
long orderNumber = orderInfoPrefs.getLong(ORDER_NUMBER, FIRST_ORDER_NUMBER) + 1;
orderInfoPrefs.edit().putLong(ORDER_NUMBER, orderNumber).apply();
String requestMetadata = String.valueOf(orderNumber);
ChargeRequest.Builder chargeRequest = new ChargeRequest.Builder(amount, CurrencyCode.USD) //
.note(note) //
.autoReturn(PosApi.AUTO_RETURN_TIMEOUT_MIN_MILLIS, TimeUnit.MILLISECONDS) //
.requestMetadata(requestMetadata) //
.restrictTendersTo(tenderTypes);
try {
Intent chargeIntent = posClient.createChargeIntent(chargeRequest.build());
startActivityForResult(chargeIntent, CHARGE_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
dialogComposer.showPointOfSaleUninstalledDialog();
}
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHARGE_REQUEST_CODE) {
if (data == null) {
transactionResultHandler.onNoResult();
}
if (resultCode == RESULT_OK) {
itemManager.clearStateAndReloadItems();
transactionResultHandler.onSuccess(data);
} else {
transactionResultHandler.onError(data);
}
}
}
@Override protected void onDestroy() {
super.onDestroy();
BikeApplication.from(this).getDataLoader().clearListener();
}
}