
it.tidalwave.bluebill.mobile.media.impl.SoundPresentationModelViewRenderable Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.media.impl;
import javax.annotation.Nonnull;
import java.util.Date;
import java.text.DateFormat;
import javax.swing.Action;
import it.tidalwave.util.As;
import it.tidalwave.util.AsException;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.observation.Observation;
import it.tidalwave.mobile.util.Downloadable;
import it.tidalwave.mobile.util.Downloadable.Status;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import it.tidalwave.role.ui.android.ViewRenderable;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import it.tidalwave.bluebill.mobile.android.R;
import lombok.RequiredArgsConstructor;
import static it.tidalwave.role.PlainTextRenderable.PlainTextRenderable;
import static it.tidalwave.role.Displayable.Displayable;
import static it.tidalwave.role.Identifiable.Identifiable;
import static it.tidalwave.role.ui.ActionProvider.ActionProvider;
import static it.tidalwave.observation.Observation.Observation;
import static it.tidalwave.mobile.util.Downloadable.Downloadable;
import static it.tidalwave.mobile.media.Media.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@RequiredArgsConstructor
public class SoundPresentationModelViewRenderable implements ViewRenderable
{
@Nonnull
private final PresentationModel presentationModel;
@Override @Nonnull
public void renderTo (final @Nonnull View view, final @Nonnull Object... extra)
{
final TextView tvStatus = (TextView)view.findViewById(R.id.tvStatus);
final TextView tvLabel = (TextView)view.findViewById(R.id.tvLabel);
final TextView tvLocationAndDate = (TextView)view.findViewById(R.id.tvLocationAndDate);
final TextView tvAuthor = (TextView)view.findViewById(R.id.tvAuthor);
final TextView tvRights = (TextView)view.findViewById(R.id.tvRights);
final TextView tvDuration = (TextView)view.findViewById(R.id.tvDuration);
final ImageView ivIcon = (ImageView)view.findViewById(R.id.ivIcon);
final Media media = presentationModel.as(Media);
if (media != null)
{
final Downloadable downloadable = media.as(Downloadable);
final Status status = downloadable.getStatus();
final StringBuilder buffer = new StringBuilder(status.getDisplayName());
if (status == Status.DOWNLOADING)
{
buffer.append(String.format(" (%d%%)", Math.round(100 * downloadable.getDownloadProgress())));
}
tvStatus.setText(buffer.toString());
tvStatus.setTextColor(view.getContext().getResources().getColor(AndroidMediaNodeRenderer.COLOR_MAP_BY_STATUS.get(status)));
try
{
final Action action = presentationModel.as(ActionProvider).getDefaultAction();
final Integer resource = AndroidMediaNodeRenderer.ICON_MAP_BY_ICON_KEY.get(action.getValue(Action.LARGE_ICON_KEY));
if (resource == null)
{
throw new NotFoundException("no icon");
}
ivIcon.setImageResource(resource);
ivIcon.setVisibility(AndroidUtilities.visibility(true));
}
catch (NotFoundException e)
{
ivIcon.setVisibility(AndroidUtilities.visibility(false));
}
}
final Observation observation = presentationModel.as(Observation);
// final Media media = observation.as(Media);
final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
final As source = observation.getSource();
final As location = observation.getLocation();
final Date date = observation.getDate();
final String callType = observation.as(Displayable).getDisplayName();
String observationId = "";
try
{
observationId = observation.as(Identifiable).getId().stringValue();
observationId = observationId.replaceAll(".*/", "");
}
catch (AsException e)
{
// no id, never mind
}
// FIXME: use StringRenderable, TextViewRenderable
tvLabel.setText(Html.fromHtml(String.format("%s", callType)));
tvLocationAndDate.setText(location.as(Displayable).getDisplayName() + " " + df.format(date));
try
{
final As author = observation.findObservers().result();
tvAuthor.setText(author.as(Displayable).getDisplayName() + ", " + source.as(Displayable).getDisplayName() + " " + observationId);
}
catch (NotFoundException e)
{
tvAuthor.setText("");
}
tvRights.setText(media.as(PlainTextRenderable).render("%s", RIGHTS));
try
{
tvDuration.setText(AndroidMediaNodeRenderer.formatDuration(media.get(DURATION)));
}
catch (NotFoundException e)
{
tvDuration.setText("");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy