All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.news.AndroidNewsViewController Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * 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://kenai.com/hg/bluebill~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.news;

import javax.annotation.Nonnull;
import javax.inject.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.RoleFactory;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.role.ui.android.ViewFactory;
import it.tidalwave.role.ui.spi.DefaultPresentationModel;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.semantic.document.Document;
import it.tidalwave.bluebill.mobile.preferences.GeneralPreferences;
import it.tidalwave.bluebill.mobile.news.DefaultNewsViewController;
import android.app.NotificationManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.role.ui.android.ViewRenderable;
import it.tidalwave.role.ui.android.spi.InflatingViewFactory;
import lombok.RequiredArgsConstructor;
import static it.tidalwave.semantic.rss.RssVocabulary.*;
import static it.tidalwave.bluebill.mobile.news.Readable.Readable;

/***********************************************************************************************************************
 *
 * @stereotype Controller
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class AndroidNewsViewController extends DefaultNewsViewController
  {
    private static final Logger log = LoggerFactory.getLogger(AndroidNewsViewController.class);
    
    @Nonnull
    private final NewsActivity view;
    
    @Nonnull
    private final Provider preferences = Locator.createProviderFor(GeneralPreferences.class);    
    
    private final ViewFactory viewFactory = new InflatingViewFactory(R.layout.news_list_row);

    @RequiredArgsConstructor
    static class NewsItemViewRenderable implements ViewRenderable // FIXME: move to a separate class, register as global
      {
        @Nonnull
        private final PresentationModel newsItemPM;
        
        @Nonnull
        private final Provider preferences = Locator.createProviderFor(GeneralPreferences.class);

        public void renderTo (View view, Object... extra) 
          {
            final Document message = newsItemPM.as(Document.class);

            try
              {
                final String date = preferences.get().formatDateAndTime(message.get(PUB_DATE));
                final TextView tvDate = (TextView)view.findViewById(R.id.tvDate);
                final TextView tvTitle = (TextView)view.findViewById(R.id.tvTitle);
                tvDate.setText(date);
                tvTitle.setText(message.get(TITLE));

                final int icon = newsItemPM.as(Readable).isRead() ? android.R.drawable.star_off
                                                                  : android.R.drawable.star_on;
                ((ImageView)view.findViewById(R.id.ivRead)).setImageResource(icon);
              }
            catch (NotFoundException e)
              {
              }
          }
      };
    
    private final RoleFactory viewRenderableFactory = new RoleFactory()  // FIXME: move to a separate class, register as global
      {
        @Nonnull
        public Object createRoleFor (final @Nonnull PresentationModel presentationModel) 
          {
            return new NewsItemViewRenderable(presentationModel);
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidNewsViewController (final @Nonnull NewsActivity view) 
      {
        super(view);
        this.view = view;
        
        roleRegister.registerRole(viewFactory).forClass(DefaultPresentationModel.class);
        roleRegister.registerRoleFactory(viewRenderableFactory).forClass(DefaultPresentationModel.class);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void loadNewsFeed()
      {
        // The user is reading the news, don't notify him any longer.
        // FIXME: this should go into the generic controller
        final NotificationManager notificationManager = (NotificationManager)view.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(AndroidNewsService.NEWS_NOTIFICATION_ID);

        super.loadNewsFeed();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void readMessage (final @Nonnull Document message) 
      {
          // FIXME: use ControlFlow and try to pass the whole message
        try
          {
            final String date = preferences.get().formatDateAndTime(message.get(PUB_DATE));
            final Intent intent = new Intent(view, NewsItemActivity.class);
            intent.putExtra("date", date);
            intent.putExtra("title", message.get(TITLE));
            intent.putExtra("content", message.get(CONTENT));
            view.startActivity(intent);
          }
        catch (Exception e)
          {
            log.warn("Can't show news item: %s", e);
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy