47
47
import org .y20k .transistor .core .Collection ;
48
48
import org .y20k .transistor .core .Station ;
49
49
import org .y20k .transistor .helpers .CollectionAdapter ;
50
- import org .y20k .transistor .helpers .DialogAddStation ;
50
+ import org .y20k .transistor .helpers .DialogAdd ;
51
51
import org .y20k .transistor .helpers .ImageHelper ;
52
52
import org .y20k .transistor .helpers .NotificationHelper ;
53
53
import org .y20k .transistor .helpers .ShortcutHelper ;
@@ -221,7 +221,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
221
221
// CASE ADD
222
222
case R .id .menu_add :
223
223
224
- DialogAddStation dialog = new DialogAddStation (mActivity , mCollection );
224
+ DialogAdd dialog = new DialogAdd (mActivity , mCollection );
225
225
dialog .show ();
226
226
return true ;
227
227
@@ -480,7 +480,7 @@ else if (mPlayback && !mSleepTimerRunning) {
480
480
// CASE: A station is playing, Sleep timer is running
481
481
else if (mPlayback ) {
482
482
startSleepTimer (duration );
483
- Toast .makeText (mActivity , mActivity .getString (R .string .toastmessage_timer_duration_increased ) + " [+" + getReadableTime (duration ) +"]" , Toast .LENGTH_SHORT ).show ();
483
+ Toast .makeText (mActivity , mActivity .getString (R .string .toastmessage_timer_duration_increased ) + " [+" + getReadableTime (duration ) + "]" , Toast .LENGTH_SHORT ).show ();
484
484
}
485
485
486
486
}
@@ -548,7 +548,7 @@ public void onClick(View view) {
548
548
549
549
550
550
/* Translates milliseconds into minutes and seconds */
551
- private String getReadableTime (long remainingTime ) {
551
+ private String getReadableTime (long remainingTime ) {
552
552
return String .format (Locale .getDefault (), "%02d:%02d" ,
553
553
TimeUnit .MILLISECONDS .toMinutes (remainingTime ),
554
554
TimeUnit .MILLISECONDS .toSeconds (remainingTime ) -
@@ -559,7 +559,7 @@ private String getReadableTime (long remainingTime) {
559
559
/* Loads app state from preferences */
560
560
private void loadAppState (Context context ) {
561
561
SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences (context );
562
- mStationIDCurrent = settings .getInt (TransistorKeys .PREF_STATION_ID_CURRENT , -1 );
562
+ mStationIDCurrent = settings .getInt (TransistorKeys .PREF_STATION_ID_CURRENTLY_PLAYING , -1 );
563
563
mStationIDLast = settings .getInt (TransistorKeys .PREF_STATION_ID_LAST , -1 );
564
564
mPlayback = settings .getBoolean (TransistorKeys .PREF_PLAYBACK , false );
565
565
mSleepTimerRunning = settings .getBoolean (TransistorKeys .PREF_TIMER_RUNNING , false );
@@ -571,7 +571,7 @@ private void loadAppState(Context context) {
571
571
private void saveAppState (Context context ) {
572
572
SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences (context );
573
573
SharedPreferences .Editor editor = settings .edit ();
574
- editor .putInt (TransistorKeys .PREF_STATION_ID_CURRENT , mStationIDCurrent );
574
+ editor .putInt (TransistorKeys .PREF_STATION_ID_CURRENTLY_PLAYING , mStationIDCurrent );
575
575
editor .putInt (TransistorKeys .PREF_STATION_ID_LAST , mStationIDLast );
576
576
editor .putBoolean (TransistorKeys .PREF_PLAYBACK , mPlayback );
577
577
editor .putBoolean (TransistorKeys .PREF_TIMER_RUNNING , mSleepTimerRunning );
@@ -628,6 +628,7 @@ public void onReceive(Context context, Intent intent) {
628
628
public void onReceive (Context context , Intent intent ) {
629
629
if (intent != null && intent .hasExtra (TransistorKeys .EXTRA_COLLECTION_CHANGE )) {
630
630
handleCollectionChanges (intent );
631
+ // handleCollectionChangesNew(intent); // un-comment to test new animations
631
632
}
632
633
}
633
634
};
@@ -715,56 +716,167 @@ private void handleCollectionChanges(Intent intent) {
715
716
716
717
// CASE: station was added
717
718
case TransistorKeys .STATION_ADDED :
719
+ // get updated collection
718
720
if (intent .hasExtra (TransistorKeys .EXTRA_COLLECTION )) {
719
721
mCollection = intent .getParcelableExtra (TransistorKeys .EXTRA_COLLECTION );
722
+ mCollectionAdapter .setCollection (mCollection );
720
723
}
724
+ // get id of currently playing station
725
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING )) {
726
+ mStationIDCurrent = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING , -1 );
727
+ }
728
+
729
+ // save app state
730
+ saveAppState (mActivity );
731
+
721
732
// refresh collection adapter and station list
722
733
mCollectionAdapter .refresh ();
723
734
refreshStationList ();
735
+
724
736
break ;
725
737
726
738
// CASE: station was renamed
727
739
case TransistorKeys .STATION_RENAMED :
728
- // keep track of playback state
729
- if (mPlayback && intent .hasExtra (TransistorKeys .EXTRA_STATION_ID ) && intent .hasExtra (TransistorKeys .EXTRA_STATION_URI_CURRENT )) {
740
+ // get flag for deletion of currently playing station
741
+ boolean stationCurrentlyPlayingRenamed = false ;
742
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_CURRENTLY_PLAYING_RENAMED )) {
743
+ stationCurrentlyPlayingRenamed = intent .getBooleanExtra (TransistorKeys .EXTRA_STATION_CURRENTLY_PLAYING_RENAMED , false );
744
+ }
730
745
731
- // retrieve and save ID of currently playing station from Uri
732
- mStationIDCurrent = mCollection .findStationID (intent .getStringExtra (TransistorKeys .EXTRA_STATION_URI_CURRENT ));
733
- saveAppState (mActivity );
746
+ // get id of currently playing station
747
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING )) {
748
+ mStationIDCurrent = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING , -1 );
749
+ }
750
+
751
+ // get updated collection
752
+ if (intent .hasExtra (TransistorKeys .EXTRA_COLLECTION )) {
753
+ mCollection = intent .getParcelableExtra (TransistorKeys .EXTRA_COLLECTION );
754
+ mCollectionAdapter .setCollection (mCollection );
755
+ }
734
756
757
+ // keep track of playback state
758
+ if (stationCurrentlyPlayingRenamed && intent .hasExtra (TransistorKeys .EXTRA_STATION_NEW_NAME )) {
735
759
// put up notification
736
760
NotificationHelper .initialize (mCollection );
737
- NotificationHelper .setStationName (mCollection . getStations (). get ( mStationIDCurrent ). getStationName ( ));
761
+ NotificationHelper .setStationName (intent . getStringExtra ( TransistorKeys . EXTRA_STATION_NEW_NAME ));
738
762
NotificationHelper .setStationID (mStationIDCurrent );
739
763
NotificationHelper .updateNotification ();
740
764
}
741
765
766
+ // save app state
767
+ saveAppState (mActivity );
768
+
742
769
// refresh collection adapter and station list
743
770
mCollectionAdapter .refresh ();
744
771
refreshStationList ();
772
+
745
773
break ;
746
774
747
775
// CASE: station was deleted
748
776
case TransistorKeys .STATION_DELETED :
749
- // keep track of playback state
750
- if (mPlayback && intent .hasExtra (TransistorKeys .EXTRA_STATION_ID ) && mStationIDCurrent == intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID , -1 )) {
777
+ // get flag for deletion of currently playing station
778
+ boolean stationCurrentlyPlayingDeleted = false ;
779
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_CURRENTLY_PLAYING_DELETED )) {
780
+ stationCurrentlyPlayingDeleted = intent .getBooleanExtra (TransistorKeys .EXTRA_STATION_CURRENTLY_PLAYING_DELETED , false );
781
+ }
782
+
783
+ // get id of currently playing station
784
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING )) {
785
+ mStationIDCurrent = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID_CURRENTLY_PLAYING , -1 );
786
+ }
787
+
788
+ // get updated collection
789
+ if (intent .hasExtra (TransistorKeys .EXTRA_COLLECTION )) {
790
+ mCollection = intent .getParcelableExtra (TransistorKeys .EXTRA_COLLECTION );
791
+ mCollectionAdapter .setCollection (mCollection );
792
+ }
793
+
794
+ // stop playback if necessary
795
+ if (stationCurrentlyPlayingDeleted ) {
751
796
mPlayback = false ;
752
797
PlayerService .startActionStop (mActivity );
753
- saveAppState (mActivity );
754
- } else if (mPlayback && intent .hasExtra (TransistorKeys .EXTRA_STATION_URI_CURRENT )) {
755
- mStationIDCurrent = mCollection .findStationID (intent .getStringExtra (TransistorKeys .EXTRA_STATION_URI_CURRENT ));
756
- saveAppState (mActivity );
757
798
}
758
799
800
+ // save app state
801
+ saveAppState (mActivity );
802
+
759
803
// refresh collection adapter and station list
760
804
mCollectionAdapter .refresh ();
761
805
refreshStationList ();
806
+
762
807
break ;
763
808
764
809
// TODO station was renamed in PlayerActivityFragment (playback indicator in list)
765
810
766
811
}
767
812
813
+
814
+
815
+ }
816
+
817
+
818
+ /* Handles adding, deleting and renaming of station (New experimental version - unfinished) */
819
+ private void handleCollectionChangesNew (Intent intent ) {
820
+
821
+ // load app state
822
+ loadAppState (mActivity );
823
+
824
+ // get updated collection from intent
825
+ if (intent .hasExtra (TransistorKeys .EXTRA_COLLECTION )) {
826
+ mCollection = intent .getParcelableExtra (TransistorKeys .EXTRA_COLLECTION );
827
+ }
828
+
829
+ switch (intent .getIntExtra (TransistorKeys .EXTRA_COLLECTION_CHANGE , 1 )) {
830
+
831
+ // CASE: station was added
832
+ case TransistorKeys .STATION_ADDED :
833
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID ) && intent .hasExtra (TransistorKeys .EXTRA_STATION )) {
834
+ int stationID = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID , 0 );
835
+ Station station = intent .getParcelableExtra (TransistorKeys .EXTRA_STATION );
836
+ mCollectionAdapter .add (stationID , station );
837
+ mCollectionAdapter .setCollection (mCollection );
838
+ // mCollectionAdapter.refresh();
839
+ }
840
+ // mLayoutManager.addView(1);
841
+ break ;
842
+
843
+ // CASE: station was renamed
844
+ case TransistorKeys .STATION_RENAMED :
845
+
846
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID ) && intent .hasExtra (TransistorKeys .EXTRA_STATION_NEW_NAME )) {
847
+ int stationID = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID , 0 );
848
+ String newStationName = intent .getStringExtra (TransistorKeys .EXTRA_STATION_NEW_NAME );
849
+ mCollectionAdapter .change (stationID , newStationName );
850
+ mCollectionAdapter .setCollection (mCollection );
851
+ // mCollectionAdapter.refresh();
852
+
853
+ int stationIndexChanged = mCollection .getStationIndexChanged ();
854
+ if (stationIndexChanged != -1 ) {
855
+ // position of station has changed
856
+ View stationImageView = mLayoutManager .getChildAt (stationID );
857
+ mLayoutManager .removeViewAt (stationID );
858
+ mLayoutManager .addView (stationImageView , stationIndexChanged );
859
+ }
860
+
861
+ }
862
+
863
+ break ;
864
+
865
+ // CASE: station was deleted
866
+ case TransistorKeys .STATION_DELETED :
867
+
868
+ if (intent .hasExtra (TransistorKeys .EXTRA_STATION_ID )) {
869
+ int stationID = intent .getIntExtra (TransistorKeys .EXTRA_STATION_ID , 0 );
870
+ mCollectionAdapter .remove (stationID );
871
+ mCollectionAdapter .setCollection (mCollection );
872
+ mCollectionAdapter .refresh ();
873
+ mLayoutManager .removeViewAt (stationID );
874
+ }
875
+
876
+ break ;
877
+
878
+ }
879
+
768
880
}
769
881
770
882
}
0 commit comments