Skip to content

Commit

Permalink
#331: fix problems
Browse files Browse the repository at this point in the history
  • Loading branch information
malsolec committed Aug 26, 2018
1 parent 2b74934 commit 9a2d7db
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void setSoundId(Long soundId){
}
}


private void handleSoundPlaying() {
if (!mediaPlayer.isPlaying()) {
Asset sound = assetRepository.get(soundId);
Expand Down Expand Up @@ -108,7 +107,7 @@ private void runAnimation() {

private void stopAnimation() {
playSoundIcon.clearAnimation();
playSoundIcon.setImageResource( R.drawable.ic_play_sound);
playSoundIcon.setImageResource(R.drawable.ic_play_sound);
}

private void stopSound() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public class StepCreateData extends BaseObservable {
private static final String EMPTY_VALUE = "";

private StepTemplate stepTemplate = new StepTemplate();
private Long taskId;

public StepCreateData(Long taskId) {
this.taskId = taskId;
stepTemplate.setTaskTemplateId(taskId);
}

public Long getTaskId() {
return taskId;
return stepTemplate.getTaskTemplateId();
}

@Bindable
Expand All @@ -43,6 +42,11 @@ public void setPictureName(String pictureName) {
notifyPropertyChanged(BR.pictureName);
}

public void setPicture(Asset picture) {
this.stepTemplate.setPicture(picture);
notifyPropertyChanged(BR.pictureName);
}

@Bindable
public String getSoundName() {
return getAssetFileName(stepTemplate.getSoundId());
Expand All @@ -53,9 +57,18 @@ public void setSoundName(String soundName) {
notifyPropertyChanged(BR.soundName);
}

public void setSound(Asset sound) {
this.stepTemplate.setSound(sound);
notifyPropertyChanged(BR.soundName);
}

@Bindable
public String getStepDuration() {
return stepTemplate.getDurationTime().toString();
if(stepTemplate.getDurationTime() != null) {
return stepTemplate.getDurationTime().toString();
}

return EMPTY_VALUE;
}

public void setStepDuration(String stepDuration) {
Expand All @@ -72,14 +85,10 @@ public void setStepTemplate(StepTemplate stepTemplate) {
}

private Asset getAsset(String fileName, Asset asset) {
if (fileName == null) {
if (fileName == null && asset == null) {
return null;
}

if (asset == null) {
asset = new Asset();
}

asset.setFilename(fileName);

return asset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.media.MediaPlayer;
import android.os.Build.VERSION_CODES;
Expand All @@ -18,6 +20,7 @@
import database.entities.StepTemplate;
import database.repository.AssetRepository;
import java.io.File;
import java.io.IOException;
import javax.inject.Inject;

import database.entities.Asset;
Expand All @@ -27,6 +30,7 @@
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.databinding.FragmentStepCreateBinding;
import pg.autyzm.friendly_plans.file_picker.FilePickerProxy;
import pg.autyzm.friendly_plans.manager_app.validation.StepValidation;
Expand Down Expand Up @@ -70,14 +74,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
inflater, R.layout.fragment_step_create, container, false);

AppComponent appComponent = ((App) getActivity().getApplication()).getAppComponent();
View view = binding.getRoot();

ImageButton playSoundIcon = (ImageButton) view.findViewById(R.id.id_btn_play_step_sound);
appComponent.inject(this);

stepData = parseArguments();

soundComponent = SoundComponent.getSoundComponent(
getSoundId(stepData.getStepTemplate()), playSoundIcon, getActivity().getApplicationContext(), appComponent);
appComponent.inject(this);

View view = binding.getRoot();
binding.setSoundComponent(soundComponent);
binding.setStepData(stepData);
binding.setStepDataClick(this);
Expand Down Expand Up @@ -115,17 +121,16 @@ public void run() {
if (stepData != null && stepData.getStepTemplate() != null) {
StepTemplate stepTemplate = stepData.getStepTemplate();

Asset picture = stepTemplate.getPicture();
if (picture != null) {
Long pictureId = stepTemplate.getPictureId();
if (pictureId != null) {
clearPicture.setVisibility(View.VISIBLE);
showPreview(picture.getId(), picturePreview);
showPreview(pictureId, picturePreview);
}

Asset sound = stepTemplate.getSound();
if (sound != null) {
Long soundId = stepTemplate.getSoundId();
if (soundId != null) {
clearSound.setVisibility(View.VISIBLE);
}

}
}
});
Expand All @@ -143,14 +148,14 @@ public void selectStepSound() {

@Override
public void cleanStepPicture() {
stepData.setPictureName(null);
stepData.setPicture(null);
picturePreview.setImageDrawable(null);
clearPicture.setVisibility(View.INVISIBLE);
}

@Override
public void clearStepSound() {
stepData.setPictureName(null);
stepData.setSound(null);
clearSound.setVisibility(View.INVISIBLE);
soundComponent.setSoundId(null);
soundComponent.stopActions();
Expand All @@ -171,7 +176,7 @@ public void saveStepData(StepCreateData stepCreateData) {

try {
StepTemplate stepTemplate = stepCreateData.getStepTemplate();
if (stepTemplate != null) {
if (stepTemplate.getId() != null) {
updateStepTemplate(stepTemplate);
} else {
createStepTemplate(stepCreateData);
Expand All @@ -182,6 +187,45 @@ public void saveStepData(StepCreateData stepCreateData) {
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (filePickerProxy.isFilePicked(resultCode)) {
if (filePickerProxy.isPickFileRequested(requestCode, AssetType.PICTURE)) {
handleAssetSelecting(data, AssetType.PICTURE);
} else if (filePickerProxy.isPickFileRequested(requestCode, AssetType.SOUND)) {
handleAssetSelecting(data, AssetType.SOUND);
}
}
}

protected void handleAssetSelecting(Intent data, AssetType assetType) {
Context context = getActivity().getApplicationContext();
String filePath = filePickerProxy.getFilePath(data);
AssetsHelper assetsHelper = new AssetsHelper(context);
try {
String assetName = assetsHelper.makeSafeCopy(filePath);
Long assetId = assetRepository
.create(AssetType.getTypeByExtension(assetName), assetName);
setAssetValue(assetType, assetRepository.get(assetId));
} catch (IOException e) {
showToastMessage(R.string.picking_file_error);
}
}

private void setAssetValue(AssetType assetType, Asset asset) {
if (assetType.equals(AssetType.PICTURE)) {
stepData.setPicture(asset);
clearPicture.setVisibility(View.VISIBLE);
showPreview(asset.getId(), picturePreview);
} else {
stepData.setSound(asset);
soundComponent.setSoundId(asset.getId());
clearSound.setVisibility(View.VISIBLE);
}
}

private String retrieveImageFile(Long pictureId) {
String imageFileName = assetRepository.get(pictureId).getFilename();
String fileDir = getActivity().getApplicationContext().getFilesDir().toString();
Expand Down

0 comments on commit 9a2d7db

Please sign in to comment.