2
2
3
3
import android .app .Dialog ;
4
4
import android .app .ProgressDialog ;
5
+ import android .content .DialogInterface ;
5
6
import android .content .Intent ;
6
7
import android .os .Bundle ;
7
8
import android .support .v4 .app .Fragment ;
9
+ import android .support .v7 .app .AlertDialog ;
8
10
import android .support .v7 .widget .LinearLayoutManager ;
9
11
import android .support .v7 .widget .RecyclerView ;
10
12
import android .util .Log ;
11
13
import android .util .Pair ;
14
+ import android .view .KeyEvent ;
12
15
import android .view .LayoutInflater ;
13
16
import android .view .View ;
14
17
import android .view .ViewGroup ;
17
20
import android .widget .TextView ;
18
21
import android .widget .Toast ;
19
22
23
+ import com .eusecom .attendance .ApproveActivity ;
20
24
import com .eusecom .attendance .Constants ;
25
+ import com .eusecom .attendance .FbmessClient ;
26
+ import com .eusecom .attendance .GitHubRepoAdapter ;
21
27
import com .eusecom .attendance .MainActivity ;
28
+ import com .eusecom .attendance .NewAbsenceActivity ;
22
29
import com .eusecom .attendance .NewPostActivity ;
23
30
import com .eusecom .attendance .SettingsActivity ;
24
31
import com .eusecom .attendance .models .Attendance ;
25
32
import com .eusecom .attendance .models .DeletedAbs ;
33
+ import com .eusecom .attendance .models .MessData ;
34
+ import com .eusecom .attendance .models .Message ;
35
+ import com .eusecom .attendance .models .NotifyData ;
26
36
import com .eusecom .attendance .retrofit .RfContributor ;
27
37
import com .eusecom .attendance .retrofit .RfEtestApi ;
28
38
import com .eusecom .attendance .retrofit .RfEtestService ;
47
57
import com .google .gson .Gson ;
48
58
import com .google .gson .GsonBuilder ;
49
59
60
+ import java .text .DateFormat ;
61
+ import java .text .SimpleDateFormat ;
62
+ import java .util .Date ;
50
63
import java .util .HashMap ;
51
64
import java .util .Map ;
52
65
55
68
import io .reactivex .disposables .CompositeDisposable ;
56
69
import io .reactivex .observers .DisposableObserver ;
57
70
import io .reactivex .schedulers .Schedulers ;
71
+ import rx .Observer ;
72
+ import rx .Subscription ;
58
73
59
74
import static android .text .TextUtils .isEmpty ;
60
75
import static java .lang .String .format ;
@@ -81,6 +96,8 @@ public ApproveListFragment() {}
81
96
private RfEtestApi _githubService ;
82
97
private CompositeDisposable _disposables ;
83
98
99
+ private Subscription subscription ;
100
+
84
101
@ Override
85
102
public void onCreate (Bundle savedInstanceState ) {
86
103
super .onCreate (savedInstanceState );
@@ -292,6 +309,9 @@ public void onDestroy() {
292
309
if (mAdapter != null ) {
293
310
mAdapter .cleanup ();
294
311
}
312
+ if (subscription != null && !subscription .isUnsubscribed ()) {
313
+ subscription .unsubscribe ();
314
+ }
295
315
_disposables .dispose ();
296
316
}
297
317
@@ -317,10 +337,10 @@ private void approvePost(String postkey, int anodaj, Attendance model) {
317
337
String savetofir = SettingsActivity .getFir (getActivity ());
318
338
String whoapprove = SettingsActivity .getUsOsc (getActivity ());
319
339
320
- _disposables .add (_githubService .contributors (savetofir , postkey , whoapprove , approveabs_json )
340
+ _disposables .add (_githubService .contributors (savetofir , postkey , whoapprove , approveabs_json , anodaj )
321
341
.flatMap (Observable ::fromIterable )
322
342
.flatMap (contributor -> {
323
- Observable <RfUser > _userObservable = _githubService .user (contributor .login )
343
+ Observable <RfUser > _userObservable = _githubService .user (savetofir , postkey , whoapprove , approveabs_json , anodaj , contributor .login )
324
344
.filter (user -> !isEmpty (user .name ) && !isEmpty (user .email ));
325
345
326
346
return Observable .zip (_userObservable ,
@@ -339,19 +359,31 @@ public void onComplete() {
339
359
340
360
@ Override
341
361
public void onError (Throwable e ) {
342
- Log .d (TAG , "error while getting the list of contributors along with full " + "names" );
362
+ String snext = "Error " + e + getResources ().getString (R .string .abs_savednot );
363
+ Toast .makeText (getActivity (), snext , Toast .LENGTH_LONG ).show ();
343
364
}
344
365
345
366
@ Override
346
367
public void onNext (Pair pair ) {
347
368
RfUser user = ((Pair <RfUser , RfContributor >)pair ).first ;
348
369
RfContributor contributor = ((Pair <RfUser , RfContributor >)pair ).second ;
349
370
371
+ String snext = "" ;
350
372
351
- String snext = " " + user .name + " "
373
+ String snextx = " " + user .name + " "
352
374
+ user .email + " "
353
375
+ contributor .contributions + " "
354
- + contributor .memo ;
376
+ + contributor .memo + " "
377
+ + contributor .saved ;
378
+
379
+ if (contributor .saved == 1 ) {
380
+ snext = getResources ().getString (R .string .abs_saved ) + snextx ;
381
+ approveFBPost (abskeydel , contributor .anodaj , model );
382
+ }else {
383
+ snext = getResources ().getString (R .string .abs_savednot );
384
+ approveFBPost (abskeydel , contributor .anodaj , model );
385
+ }
386
+
355
387
356
388
Log .d (TAG , "onnext " + snext );
357
389
Toast .makeText (getActivity (), snext , Toast .LENGTH_LONG ).show ();
@@ -363,6 +395,10 @@ public void onNext(Pair pair) {
363
395
364
396
private void getDialog (String postkey , Attendance model ) {
365
397
398
+ //if savetofir > 0 then save to server
399
+ String savetofir = SettingsActivity .getFir (getActivity ());
400
+ int savetofiri = Integer .parseInt (savetofir );
401
+
366
402
// custom dialog
367
403
final Dialog dialog = new Dialog (getActivity ());
368
404
dialog .setContentView (R .layout .approve_dialog );
@@ -380,7 +416,11 @@ private void getDialog(String postkey, Attendance model) {
380
416
381
417
public void onClick (View v ) {
382
418
dialog .dismiss ();
383
- approvePost (abskeydel , 1 , model );
419
+ if (savetofiri >0 ){
420
+ approvePost (abskeydel , 1 , model );
421
+ }else {
422
+ approvePost (abskeydel , 1 , model );
423
+ }
384
424
}
385
425
});
386
426
Button buttonRefuse = (Button ) dialog .findViewById (R .id .buttonRefuse );
@@ -389,13 +429,56 @@ public void onClick(View v) {
389
429
390
430
public void onClick (View v ) {
391
431
dialog .dismiss ();
392
- approvePost (abskeydel , 0 , model );
432
+ if (savetofiri >0 ){
433
+ approvePost (abskeydel , 0 , model );
434
+ }else {
435
+ approvePost (abskeydel , 0 , model );
436
+ }
393
437
394
438
395
439
}
396
440
});
397
441
dialog .show ();
398
442
443
+ }//end getdialog
444
+
445
+ private void approveFBPost (String postkey , int anodaj , Attendance model ) {
446
+
447
+ //Toast.makeText(getActivity(), "Set approved=1", Toast.LENGTH_LONG).show();
448
+ String Notititle = SettingsActivity .getUsname (getActivity ()) + " " + model .dmxa + " " + model .dmna ;
449
+ long timestampod = Long .parseLong (model .daod ) * 1000L ;
450
+ String datefroms = getDate (timestampod );
451
+ long timestampdo = Long .parseLong (model .dado ) * 1000L ;
452
+ String datetos = getDate (timestampdo );
453
+ String Notibody ="" ;
454
+
455
+ if ( anodaj == 1 ) {
456
+ Notibody = getString (R .string .isapproved ) + " " + model .dmxa + " " + model .dmna + " "
457
+ + getString (R .string .from ) + " " + datefroms + " " + getString (R .string .to ) + " " + datetos
458
+ + " " + model .hodxb + " " + getString (R .string .hodiny );
459
+ }else {
460
+ Notibody = getString (R .string .isrefused ) + " " + model .dmxa + " " + model .dmna + " "
461
+ + getString (R .string .from ) + " " + datefroms + " " + getString (R .string .to ) + " " + datetos
462
+ + " " + model .hodxb + " " + getString (R .string .hodiny );
463
+ }
464
+
465
+ String approvetopic = "/topics/approve" + SettingsActivity .getUsIco (getActivity ());
466
+ //FirebaseRetrofitMessaging firebasemessaging = new FirebaseRetrofitMessaging("/topics/news", Notititle, Notibody);
467
+ FirebaseRxApproveMessaging firebasemessaging = new FirebaseRxApproveMessaging (approvetopic , Notititle , Notibody , anodaj );
468
+ subscription = firebasemessaging .SendNotification ();
469
+
470
+ }
471
+
472
+ private String getDate (long timeStamp ){
473
+
474
+ try {
475
+ DateFormat sdf = new SimpleDateFormat ("dd.MM.yyyy" );
476
+ Date netDate = (new Date (timeStamp ));
477
+ return sdf .format (netDate );
478
+ }
479
+ catch (Exception ex ){
480
+ return "xx" ;
481
+ }
399
482
}
400
483
401
484
@@ -429,4 +512,88 @@ public void onPause() {
429
512
isrunning =false ;
430
513
}
431
514
515
+ public class FirebaseRxApproveMessaging {
516
+
517
+ String to , title , body ;
518
+ int anodaj ;
519
+ private GitHubRepoAdapter adapter = new GitHubRepoAdapter ();
520
+ private Subscription subscription ;
521
+
522
+
523
+ public FirebaseRxApproveMessaging (String to , String title , String body , int anodaj ) {
524
+ this .to = to ;
525
+ this .title = title ;
526
+ this .body = body ;
527
+ this .anodaj = anodaj ;
528
+ }
529
+
530
+
531
+ public Subscription SendNotification () {
532
+
533
+ MessData messdata = new MessData ("This is a GCM Topic Message!" );
534
+ NotifyData notifydata = new NotifyData (title , body );
535
+ Message message = new Message (to , notifydata , "" );
536
+
537
+ subscription = FbmessClient .getInstance ()
538
+ .sendmyMessage ("xxxxx" , message )
539
+ .subscribeOn (rx .schedulers .Schedulers .io ())
540
+ .observeOn (rx .android .schedulers .AndroidSchedulers .mainThread ())
541
+ .subscribe (new Observer <Message >() {
542
+ @ Override
543
+ public void onCompleted () {
544
+
545
+ //hideProgressDialog();
546
+ String notis ="" ;
547
+ if ( anodaj == 1 ) {
548
+ notis = getString (R .string .isapproved );
549
+ }else {
550
+ notis = getString (R .string .isrefused );
551
+ }
552
+ //Log.d(TAG, "In onCompleted()");
553
+ AlertDialog dialog = new AlertDialog .Builder (getActivity ())
554
+ .setTitle (getString (R .string .absence ))
555
+ .setMessage (notis )
556
+ .setPositiveButton (getString (R .string .textok ), new DialogInterface .OnClickListener () {
557
+ public void onClick (DialogInterface dialog , int which ) {
558
+
559
+ //getActivity().finish();
560
+ }
561
+ })
562
+
563
+ .show ();
564
+
565
+ dialog .setOnKeyListener (new Dialog .OnKeyListener () {
566
+
567
+ @ Override
568
+ public boolean onKey (DialogInterface arg0 , int keyCode ,
569
+ KeyEvent event ) {
570
+ // TODO Auto-generated method stub
571
+ if (keyCode == KeyEvent .KEYCODE_BACK ) {
572
+ //getActivity().finish();
573
+ dialog .dismiss ();
574
+ }
575
+ return true ;
576
+ }
577
+ });
578
+ }
579
+
580
+
581
+ @ Override
582
+ public void onError (Throwable e ) {
583
+ e .printStackTrace ();
584
+ Log .d (TAG , "In onError()" );
585
+ }
586
+
587
+ @ Override
588
+ public void onNext (Message message ) {
589
+ Log .d (TAG , "In onNext()" );
590
+ Log .d ("message" , message .getMessage_id ());
591
+ //adapter.setGitHubRepos(gitHubRepos);
592
+ }
593
+ });
594
+ return subscription ;
595
+ }
596
+
597
+ }//end of FirebaseRxApproveMessaging
598
+
432
599
}
0 commit comments