1
1
package com .simcoder .uber ;
2
2
3
3
import android .annotation .SuppressLint ;
4
+ import android .app .ProgressDialog ;
5
+ import android .support .design .widget .Snackbar ;
4
6
import android .support .v7 .app .AppCompatActivity ;
5
7
import android .os .Bundle ;
6
8
import android .support .v7 .widget .LinearLayoutManager ;
7
9
import android .support .v7 .widget .RecyclerView ;
8
10
import android .text .format .DateFormat ;
11
+ import android .util .Log ;
9
12
import android .view .View ;
13
+ import android .widget .Button ;
14
+ import android .widget .EditText ;
10
15
import android .widget .TextView ;
11
16
12
17
import com .google .firebase .auth .FirebaseAuth ;
18
23
import com .simcoder .uber .historyRecyclerView .HistoryAdapter ;
19
24
import com .simcoder .uber .historyRecyclerView .HistoryObject ;
20
25
26
+ import org .json .JSONException ;
27
+ import org .json .JSONObject ;
28
+
29
+ import java .io .IOException ;
21
30
import java .util .ArrayList ;
22
31
import java .util .Calendar ;
23
32
import java .util .Locale ;
24
33
34
+ import okhttp3 .Call ;
35
+ import okhttp3 .Callback ;
36
+ import okhttp3 .MediaType ;
37
+ import okhttp3 .OkHttpClient ;
38
+ import okhttp3 .Request ;
39
+ import okhttp3 .RequestBody ;
40
+ import okhttp3 .Response ;
41
+
25
42
26
43
public class HistoryActivity extends AppCompatActivity {
27
44
private String customerOrDriver , userId ;
@@ -34,12 +51,18 @@ public class HistoryActivity extends AppCompatActivity {
34
51
35
52
private Double Balance = 0.0 ;
36
53
54
+ private Button mPayout ;
55
+
56
+ private EditText mPayoutEmail ;
57
+
37
58
@ Override
38
59
protected void onCreate (Bundle savedInstanceState ) {
39
60
super .onCreate (savedInstanceState );
40
61
setContentView (R .layout .activity_history );
41
62
42
63
mBalance = findViewById (R .id .balance );
64
+ mPayout = findViewById (R .id .payout );
65
+ mPayoutEmail = findViewById (R .id .payoutEmail );
43
66
44
67
mHistoryRecyclerView = (RecyclerView ) findViewById (R .id .historyRecyclerView );
45
68
mHistoryRecyclerView .setNestedScrollingEnabled (false );
@@ -56,7 +79,16 @@ protected void onCreate(Bundle savedInstanceState) {
56
79
57
80
if (customerOrDriver .equals ("Drivers" )){
58
81
mBalance .setVisibility (View .VISIBLE );
82
+ mPayout .setVisibility (View .VISIBLE );
83
+ mPayoutEmail .setVisibility (View .VISIBLE );
59
84
}
85
+
86
+ mPayout .setOnClickListener (new View .OnClickListener () {
87
+ @ Override
88
+ public void onClick (View view ) {
89
+ payoutRequest ();
90
+ }
91
+ });
60
92
}
61
93
62
94
private void getUserHistoryIds () {
@@ -94,8 +126,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
94
126
95
127
if (dataSnapshot .child ("customerPaid" ).getValue () != null && dataSnapshot .child ("driverPaidOut" ).getValue () == null ){
96
128
if (dataSnapshot .child ("distance" ).getValue () != null ){
97
- distance = dataSnapshot .child ("distance" ).getValue ().toString ();
98
- ridePrice = (Double .valueOf (distance ) * 0.4 );
129
+ ridePrice = Double .valueOf (dataSnapshot .child ("price" ).getValue ().toString ());
99
130
Balance += ridePrice ;
100
131
mBalance .setText ("Balance: " + String .valueOf (Balance ) + " $" );
101
132
}
@@ -124,4 +155,70 @@ private String getDate(Long time) {
124
155
private ArrayList <HistoryObject > getDataSetHistory () {
125
156
return resultsHistory ;
126
157
}
158
+
159
+
160
+
161
+
162
+ public static final MediaType MEDIA_TYPE = MediaType .parse ("application/json" );
163
+ ProgressDialog progress ;
164
+ private void payoutRequest () {
165
+ progress = new ProgressDialog (this );
166
+ progress .setTitle ("Processing your payout" );
167
+ progress .setMessage ("Please Wait..." );
168
+ progress .setCancelable (false ); // disable dismiss by tapping outside of the dialog
169
+ progress .show ();
170
+
171
+ final OkHttpClient client = new OkHttpClient ();
172
+ JSONObject postData = new JSONObject ();
173
+ try {
174
+ postData .put ("uid" , FirebaseAuth .getInstance ().getCurrentUser ().getUid ());
175
+ postData .put ("email" , mPayoutEmail .getText ());
176
+ } catch (JSONException e ) {
177
+ e .printStackTrace ();
178
+ }
179
+
180
+ RequestBody body = RequestBody .create (MEDIA_TYPE ,
181
+ postData .toString ());
182
+
183
+ final Request request = new Request .Builder ()
184
+ .url ("https://us-central1-uberapp-408c8.cloudfunctions.net/payout" )
185
+ .post (body )
186
+ .addHeader ("Content-Type" , "application/json" )
187
+ .addHeader ("Authorization" , "Your Token" )
188
+ .addHeader ("cache-control" , "no-cache" )
189
+ .build ();
190
+ client .newCall (request ).enqueue (new Callback () {
191
+ @ Override
192
+ public void onFailure (Call call , IOException e ) {
193
+ String mMessage = e .getMessage ().toString ();
194
+ Log .w ("failure Response" , mMessage );
195
+ progress .dismiss ();
196
+ }
197
+
198
+ @ Override
199
+ public void onResponse (Call call , Response response )
200
+ throws IOException {
201
+
202
+ int responseCode = response .code ();
203
+
204
+
205
+ if (response .isSuccessful ())
206
+ switch (responseCode ) {
207
+ case 200 :
208
+ Snackbar .make (findViewById (R .id .layout ), "Payout Successful!" , Snackbar .LENGTH_LONG ).show ();
209
+ break ;
210
+ case 501 :
211
+ Snackbar .make (findViewById (R .id .layout ), "Error: no payout available" , Snackbar .LENGTH_LONG ).show ();
212
+ break ;
213
+ default :
214
+ Snackbar .make (findViewById (R .id .layout ), "Error: couldn't complete the transaction" , Snackbar .LENGTH_LONG ).show ();
215
+ break ;
216
+ }
217
+ else
218
+ Snackbar .make (findViewById (R .id .layout ), "Error: couldn't complete the transaction" , Snackbar .LENGTH_LONG ).show ();
219
+
220
+ progress .dismiss ();
221
+ }
222
+ });
223
+ }
127
224
}
0 commit comments