Skip to content

Commit

Permalink
#331: Extract PictureManager and SoundManager from StepCreateFragment…
Browse files Browse the repository at this point in the history
… and tests, extract stepCreate data to hold stepTemplate object and bing with view add test
  • Loading branch information
malsolec committed Sep 5, 2018
1 parent 9a2d7db commit c6949ee
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 200 deletions.
2 changes: 2 additions & 0 deletions Friendly-plans/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ android {
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
}
Expand All @@ -66,6 +67,7 @@ dependencies {
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.databinding:library:1.3.1'
compile 'com.android.databinding:adapters:1.3.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8'

annotationProcessor "com.google.dagger:dagger-compiler:2.9"
provided 'javax.annotation:jsr250-api:1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import pg.autyzm.friendly_plans.asset.AssetsHelperModule;
import pg.autyzm.friendly_plans.file_picker.FilePickerModule;
import pg.autyzm.friendly_plans.file_picker.FilePickerProxy;
import pg.autyzm.friendly_plans.manager_app.view.step_create.PictureManager;
import pg.autyzm.friendly_plans.manager_app.view.step_create.SoundManager;
import pg.autyzm.friendly_plans.notifications.ToastUserNotifier;
import pg.autyzm.friendly_plans.notifications.ToastUserNotifierModule;
import pg.autyzm.friendly_plans.manager_app.view.child_list.ChildListActivity;
Expand Down Expand Up @@ -97,4 +99,8 @@ public interface AppComponent {
void inject(StepCreateFragment stepCreateFragment);

void inject(SoundComponent soundComponent);

void inject(SoundManager soundManager);

void inject(PictureManager pictureManager);
}
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;
}
}

This file was deleted.

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();
}
}

0 comments on commit c6949ee

Please sign in to comment.