11package com .simcoder .uber ;
22
33import android .annotation .SuppressLint ;
4+ import android .app .ProgressDialog ;
5+ import android .support .design .widget .Snackbar ;
46import android .support .v7 .app .AppCompatActivity ;
57import android .os .Bundle ;
68import android .support .v7 .widget .LinearLayoutManager ;
79import android .support .v7 .widget .RecyclerView ;
810import android .text .format .DateFormat ;
11+ import android .util .Log ;
912import android .view .View ;
13+ import android .widget .Button ;
14+ import android .widget .EditText ;
1015import android .widget .TextView ;
1116
1217import com .google .firebase .auth .FirebaseAuth ;
1823import com .simcoder .uber .historyRecyclerView .HistoryAdapter ;
1924import com .simcoder .uber .historyRecyclerView .HistoryObject ;
2025
26+ import org .json .JSONException ;
27+ import org .json .JSONObject ;
28+
29+ import java .io .IOException ;
2130import java .util .ArrayList ;
2231import java .util .Calendar ;
2332import java .util .Locale ;
2433
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+
2542
2643public class HistoryActivity extends AppCompatActivity {
2744 private String customerOrDriver , userId ;
@@ -34,12 +51,18 @@ public class HistoryActivity extends AppCompatActivity {
3451
3552 private Double Balance = 0.0 ;
3653
54+ private Button mPayout ;
55+
56+ private EditText mPayoutEmail ;
57+
3758 @ Override
3859 protected void onCreate (Bundle savedInstanceState ) {
3960 super .onCreate (savedInstanceState );
4061 setContentView (R .layout .activity_history );
4162
4263 mBalance = findViewById (R .id .balance );
64+ mPayout = findViewById (R .id .payout );
65+ mPayoutEmail = findViewById (R .id .payoutEmail );
4366
4467 mHistoryRecyclerView = (RecyclerView ) findViewById (R .id .historyRecyclerView );
4568 mHistoryRecyclerView .setNestedScrollingEnabled (false );
@@ -56,7 +79,16 @@ protected void onCreate(Bundle savedInstanceState) {
5679
5780 if (customerOrDriver .equals ("Drivers" )){
5881 mBalance .setVisibility (View .VISIBLE );
82+ mPayout .setVisibility (View .VISIBLE );
83+ mPayoutEmail .setVisibility (View .VISIBLE );
5984 }
85+
86+ mPayout .setOnClickListener (new View .OnClickListener () {
87+ @ Override
88+ public void onClick (View view ) {
89+ payoutRequest ();
90+ }
91+ });
6092 }
6193
6294 private void getUserHistoryIds () {
@@ -94,8 +126,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
94126
95127 if (dataSnapshot .child ("customerPaid" ).getValue () != null && dataSnapshot .child ("driverPaidOut" ).getValue () == null ){
96128 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 ());
99130 Balance += ridePrice ;
100131 mBalance .setText ("Balance: " + String .valueOf (Balance ) + " $" );
101132 }
@@ -124,4 +155,70 @@ private String getDate(Long time) {
124155 private ArrayList <HistoryObject > getDataSetHistory () {
125156 return resultsHistory ;
126157 }
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+ }
127224}
0 commit comments