-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#331: Extract PictureManager and SoundManager from StepCreateFragment…
… and tests, extract stepCreate data to hold stepTemplate object and bing with view add test
- Loading branch information
Showing
14 changed files
with
585 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 103 additions & 5 deletions
108
...p/src/main/java/pg/autyzm/friendly_plans/manager_app/view/step_create/PictureManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,103 @@ | ||
//package pg.autyzm.friendly_plans.manager_app.view.step_create; | ||
// | ||
//public class PictureManager { | ||
// | ||
//} | ||
package pg.autyzm.friendly_plans.manager_app.view.step_create; | ||
|
||
import android.app.FragmentManager; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.ImageButton; | ||
import android.widget.ImageView; | ||
import com.squareup.picasso.Picasso; | ||
import database.entities.StepTemplate; | ||
import database.repository.AssetRepository; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import javax.inject.Inject; | ||
import pg.autyzm.friendly_plans.AppComponent; | ||
import pg.autyzm.friendly_plans.R; | ||
import pg.autyzm.friendly_plans.asset.AssetType; | ||
import pg.autyzm.friendly_plans.asset.AssetsHelper; | ||
import pg.autyzm.friendly_plans.manager_app.view.task_create.ImagePreviewDialog; | ||
import pg.autyzm.friendly_plans.notifications.ToastUserNotifier; | ||
|
||
public class PictureManager { | ||
|
||
@Inject | ||
AssetRepository assetRepository; | ||
@Inject | ||
ToastUserNotifier toastUserNotifier; | ||
|
||
private final ImageButton clearPicture; | ||
private final ImageView picturePreview; | ||
private final Context context; | ||
private final StepCreateData stepData; | ||
|
||
public static PictureManager getPictureManager(StepCreateData stepDate, ImageButton clearPicture, ImageView picturePreview, | ||
Context context, AppComponent appComponent) { | ||
final PictureManager pictureManager = new PictureManager(stepDate, clearPicture, picturePreview, context); | ||
appComponent.inject(pictureManager); | ||
return pictureManager; | ||
} | ||
|
||
private PictureManager(StepCreateData stepDate, ImageButton clearPicture, ImageView picturePreview, Context context) { | ||
this.stepData = stepDate; | ||
this.clearPicture = clearPicture; | ||
this.picturePreview = picturePreview; | ||
this.context = context; | ||
} | ||
|
||
public void showPicture() { | ||
if (isPictureSet()) { | ||
clearPicture.setVisibility(View.VISIBLE); | ||
showPreview(); | ||
} else { | ||
clearPicture.setVisibility(View.INVISIBLE); | ||
picturePreview.setImageDrawable(null); | ||
} | ||
} | ||
|
||
public void showPicturePreview(StepTemplate stepTemplate, | ||
Context applicationContext, | ||
FragmentManager fragmentManager) { | ||
ImagePreviewDialog preview = new ImagePreviewDialog(); | ||
Bundle args = new Bundle(); | ||
args.putString("imgPath", retrieveImageFile(stepTemplate.getPictureId(), applicationContext)); | ||
|
||
preview.setArguments(args); | ||
preview.show(fragmentManager, "preview"); | ||
} | ||
|
||
public void clearPicture() { | ||
stepData.setPicture(null); | ||
showPicture(); | ||
} | ||
|
||
public void handleAssetSelecting(String filePath) { | ||
AssetsHelper assetsHelper = new AssetsHelper(context); | ||
try { | ||
String assetName = assetsHelper.makeSafeCopy(filePath); | ||
Long assetId = assetRepository | ||
.create(AssetType.getTypeByExtension(assetName), assetName); | ||
stepData.setPicture(assetRepository.get(assetId)); | ||
showPicture(); | ||
} catch (IOException e) { | ||
toastUserNotifier.displayNotifications(R.string.picking_file_error, context); | ||
} | ||
} | ||
|
||
private boolean isPictureSet() { | ||
return stepData.getStepTemplate().getPictureId() != null; | ||
} | ||
|
||
private void showPreview() { | ||
Picasso.with(context) | ||
.load(new File(retrieveImageFile(stepData.getStepTemplate().getPictureId(), context))) | ||
.resize(0, picturePreview.getHeight()) | ||
.into(picturePreview); | ||
} | ||
|
||
private String retrieveImageFile(Long pictureId, Context applicationContext) { | ||
String imageFileName = assetRepository.get(pictureId).getFilename(); | ||
String fileDir = applicationContext.getFilesDir().toString(); | ||
return fileDir + File.separator + imageFileName; | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
.../app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/step_create/SoudManager.java
This file was deleted.
Oops, something went wrong.
150 changes: 150 additions & 0 deletions
150
...app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/step_create/SoundManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package pg.autyzm.friendly_plans.manager_app.view.step_create; | ||
|
||
import android.content.Context; | ||
import android.media.AudioManager; | ||
import android.media.MediaPlayer; | ||
import android.view.View; | ||
import android.view.animation.Animation; | ||
import android.view.animation.AnimationUtils; | ||
import android.widget.ImageButton; | ||
import database.entities.Asset; | ||
import database.repository.AssetRepository; | ||
import java.io.IOException; | ||
import javax.inject.Inject; | ||
import pg.autyzm.friendly_plans.AppComponent; | ||
import pg.autyzm.friendly_plans.R; | ||
import pg.autyzm.friendly_plans.asset.AssetType; | ||
import pg.autyzm.friendly_plans.asset.AssetsHelper; | ||
import pg.autyzm.friendly_plans.notifications.ToastUserNotifier; | ||
|
||
public final class SoundManager { | ||
|
||
@Inject | ||
MediaPlayer mediaPlayer; | ||
@Inject | ||
AssetRepository assetRepository; | ||
@Inject | ||
ToastUserNotifier toastUserNotifier; | ||
@Inject | ||
AssetsHelper assetsHelper; | ||
|
||
private ImageButton playSoundIcon; | ||
private Context context; | ||
private ImageButton clearSound; | ||
private StepCreateData stepData; | ||
|
||
public static SoundManager getSoundManager(StepCreateData stepData, ImageButton playSoundIcon, | ||
ImageButton clearSound, | ||
Context context, AppComponent appComponent) { | ||
final SoundManager soundManager = new SoundManager(stepData, playSoundIcon, clearSound, | ||
context); | ||
appComponent.inject(soundManager); | ||
soundManager.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | ||
soundManager.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | ||
public void onCompletion(MediaPlayer player) { | ||
soundManager.handlePlayingCompletion(); | ||
} | ||
}); | ||
return soundManager; | ||
} | ||
|
||
private SoundManager(StepCreateData stepData, ImageButton playSoundIcon, | ||
ImageButton clearSound, Context context) { | ||
this.stepData = stepData; | ||
this.clearSound = clearSound; | ||
this.playSoundIcon = playSoundIcon; | ||
this.context = context; | ||
} | ||
|
||
public void onPlayStopSoundClick(View view) { | ||
if (isSoundSet()) { | ||
handleSoundPlaying(); | ||
} else { | ||
toastUserNotifier.displayNotifications(R.string.no_file_to_play_error, context); | ||
} | ||
} | ||
|
||
public void stopActions() { | ||
stopSound(); | ||
stopAnimation(); | ||
} | ||
|
||
public void clearSound() { | ||
stepData.setSound(null); | ||
changeSoundSelected(false); | ||
stopActions(); | ||
} | ||
|
||
public void showSound() { | ||
changeSoundSelected(isSoundSet()); | ||
} | ||
|
||
public void handleAssetSelecting(String filePath) { | ||
AssetsHelper assetsHelper = new AssetsHelper(context); | ||
try { | ||
String assetName = assetsHelper.makeSafeCopy(filePath); | ||
Long soundId = assetRepository | ||
.create(AssetType.getTypeByExtension(assetName), assetName); | ||
stepData.setSound(assetRepository.get(soundId)); | ||
} catch (IOException e) { | ||
toastUserNotifier.displayNotifications(R.string.picking_file_error, context); | ||
} | ||
} | ||
|
||
private void changeSoundSelected(Boolean isSelected) { | ||
if (isSelected) { | ||
playSoundIcon.setVisibility(View.VISIBLE); | ||
clearSound.setVisibility(View.VISIBLE); | ||
} else { | ||
playSoundIcon.setVisibility(View.INVISIBLE); | ||
clearSound.setVisibility(View.INVISIBLE); | ||
} | ||
} | ||
|
||
private boolean isSoundSet() { | ||
return stepData.getStepTemplate().getSoundId() != null; | ||
} | ||
|
||
private void handleSoundPlaying() { | ||
if (!mediaPlayer.isPlaying()) { | ||
Asset sound = assetRepository.get(stepData.getStepTemplate().getSoundId()); | ||
playSound(sound); | ||
runAnimation(); | ||
} else { | ||
stopActions(); | ||
} | ||
} | ||
|
||
private void playSound(Asset sound) { | ||
try { | ||
String soundPath = assetsHelper.getFileFullPath(sound); | ||
mediaPlayer.reset(); | ||
mediaPlayer.setDataSource(soundPath); | ||
mediaPlayer.prepare(); | ||
mediaPlayer.start(); | ||
} catch (IOException e) { | ||
toastUserNotifier.displayNotifications(R.string.playing_file_error, context); | ||
} | ||
} | ||
|
||
private void runAnimation() { | ||
playSoundIcon.setImageResource(R.drawable.ic_playing_sound); | ||
Animation rotation = AnimationUtils.loadAnimation(context, R.anim.ic_play_sound_animation); | ||
playSoundIcon.startAnimation(rotation); | ||
} | ||
|
||
private void stopAnimation() { | ||
playSoundIcon.clearAnimation(); | ||
playSoundIcon.setImageResource(R.drawable.ic_play_sound); | ||
} | ||
|
||
private void stopSound() { | ||
mediaPlayer.stop(); | ||
mediaPlayer.reset(); | ||
} | ||
|
||
private void handlePlayingCompletion() { | ||
mediaPlayer.reset(); | ||
stopAnimation(); | ||
} | ||
} |
Oops, something went wrong.