public override View GetView
        (
            int itemPosition,
            View objectView,
            ViewGroup viewGroup
        )
        {
            bool isRecycled = (objectView != null);

            if (isRecycled == false)
            {
                objectView = InflateView(viewGroup);
            }

            TextView typeDescription = objectView.FindViewById <TextView>(Resource.Id.TypeDescription);
            TextView objectTime      = objectView.FindViewById <TextView>(Resource.Id.ObjectTime);
            TextView objectLocation  = objectView.FindViewById <TextView>(Resource.Id.ObjectLocation);
            AndroidCachingImageView objectThumbnail = objectView.FindViewById <AndroidCachingImageView>(Resource.Id.ObjectThumbnail);
            ImageView typeThumbnail     = objectView.FindViewById <ImageView>(Resource.Id.TypeThumbnail);
            TextView  sourceDescription = objectView.FindViewById <TextView>(Resource.Id.SourceDescription);
            TextView  objectDescription = objectView.FindViewById <TextView>(Resource.Id.ObjectDescription);
            TextView  objectDetails     = objectView.FindViewById <TextView>(Resource.Id.ObjectDetails);

            HandleThumbnailScaleType(objectThumbnail);

            PortableSyncRenderer <Context> syncRenderer = this[itemPosition];

            if (syncRenderer.ObjectDrawable.HasValue)
            {
                objectThumbnail.SetImageDrawable(GetResources(),
                                                 syncRenderer.ObjectDrawable.Value);
            }
            else
            {
                objectThumbnail.SetImageDrawable(null);
            }

            if (objectTime != null)
            {
                objectTime.Text = string.Format("{0}",
                                                syncRenderer.ObjectTime);
            }

            if (sourceDescription != null)
            {
                sourceDescription.Text = syncRenderer.SourceDescription;
            }

            if (typeThumbnail != null)
            {
                typeThumbnail.SetImageResource(syncRenderer.TypeDrawable);
            }

            if (typeDescription != null)
            {
                typeDescription.Text = string.Format("{0} ({1})",
                                                     _getString(syncRenderer.TypeName),
                                                     syncRenderer.TypeDescription);
            }

            string initialDescription = null;

            if (objectDescription != null)
            {
                initialDescription     = syncRenderer.ObjectDescription;
                objectDescription.Text = initialDescription;
            }

            // it is save to remove event handler even if not added, yet:
            objectThumbnail.Click -= OnObjectThumbnailClicked;
            objectThumbnail.Click += OnObjectThumbnailClicked;

            var asyncRenderer    = syncRenderer as PortableAsyncRenderer <Bitmap>;
            var correlatedEntity = syncRenderer as PortableCorrelatedEntity;

            /*AndroidCrapApplication.ApplicationLogger.LogDebug("{0} - {1} : {2}",
             *                                                itemPosition,
             *                                                correlatedEntity.CorrelationTag,
             *                                                GC.GetTotalMemory(false));*/

            if ((asyncRenderer != null) && (correlatedEntity != null))
            {
                if (objectDescription != null)
                {
                    objectDescription.Tag = correlatedEntity.CorrelationTag;

                    asyncRenderer.FireParallelTextRendering
                    (
                        correlatedEntity,
                        RunOnUiThread,
                        initialDescription,
                        objectDescription
                    );
                }

                objectThumbnail.Tag = correlatedEntity.CorrelationTag;

                if (isRecycled == false)
                {
                    objectView.Tag = objectThumbnail.Background;
                }

                asyncRenderer.FireParallelThumbnailRendering
                (
                    correlatedEntity,
                    RunOnUiThread,
                    objectView,
                    objectThumbnail,
                    GetResources(),
                    _loadingResource,
                    OnCorrupt,
                    onFinished: (thumbnailView,
                                 originalBackground,
                                 loadingAnimation,
                                 isLoaded) =>
                {
                    loadingAnimation?.Stop();

                    if (thumbnailView.Background != originalBackground)
                    {
                        thumbnailView.Background = originalBackground;
                    }

                    TrySetThumbnailWidth(thumbnailView.Width);
                }
                );
            }

            return(objectView);
        }
Beispiel #2
0
        private void FireParallelSlideRendering
        (
            int itemPosition,
            View sourceView,
            AndroidCachingImageView objectThumbnail
        )
        {
            if (objectThumbnail.Visibility != ViewStates.Visible)
            {
                return;
            }

            int objectIndex = _objectSources[itemPosition].Value;
            int objectCount = _objectSources[itemPosition].Key.SourceObjects.Count;

            if (objectIndex == objectCount - 1)
            {
                objectIndex = initialIndex;
            }

            objectIndex++;

            PortableAsyncRenderer <Bitmap> asyncRenderer = null;

            PortableBaseSource objectSource = _objectSources[itemPosition].Key;

            if (objectIndex < objectSource.SourceObjects.Count)
            {
                asyncRenderer = objectSource.SourceObjects[objectIndex] as PortableAsyncRenderer <Bitmap>;
            }

            if (asyncRenderer == null)
            {
                objectThumbnail.SetImageDrawable(null);

                return;
            }

            if (objectThumbnail.Visibility != ViewStates.Visible)
            {
                return;
            }

            int?loadingResource = null;

            asyncRenderer.FireParallelThumbnailRendering
            (
                _objectSources[itemPosition].Key,
                RunOnUiThread,
                sourceView,
                objectThumbnail,
                GetResources(),
                loadingResource,
                OnCorrupt,
                onFinished: (thumbnailView,
                             originalBackground,
                             loadingAnimation,
                             isLoaded) =>
            {
                TrySetThumbnailWidth(thumbnailView.Width);

                if (isLoaded == false)
                {
                    return;
                }

                _objectSources[itemPosition] = new KeyValuePair <PortableBaseSource, int>
                                               (
                    _objectSources[itemPosition].Key,
                    objectIndex
                                               );
            }
            );
        }
        public static void FireParallelThumbnailRendering
        (
            this PortableAsyncRenderer <Bitmap> asyncRenderer,
            PortableCorrelatedEntity correlatedEntity,
            Action <Action> runOnUiThread,
            View itemView,
            AndroidCachingImageView thumbnailView,
            Resources activityResources,
            int?backgroundResource,
            Action <CorruptObjectException, ImageView> onCorrupt,
            Action <ImageView, Drawable, AnimationDrawable, bool> onFinished
        )
        {
            Drawable          originalBackground = itemView.Tag as Drawable;
            AnimationDrawable loadingAnimation   = thumbnailView.Background as AnimationDrawable;

            bool isLoaded = false;

            if (correlatedEntity.CorrelationTag != thumbnailView.GetCorrelationTag())
            {
                onFinished?.Invoke(thumbnailView,
                                   originalBackground,
                                   loadingAnimation,
                                   isLoaded);
                return;
            }

            if (backgroundResource != null)
            {
                thumbnailView.SetBackgroundResource(backgroundResource.Value);
            }

            loadingAnimation?.Start();

            itemView.Post // to have image view measured size reliable
            (
                async() =>

            {
                int thumbnailWidth  = thumbnailView.MeasuredWidth;
                int thumbnailHeight = thumbnailView.MeasuredHeight;

                if (thumbnailView.Visibility != ViewStates.Visible)
                {
                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                try
                {
                    await asyncRenderer.RenderThumbnailAsync(correlatedEntity,
                                                             runOnUiThread,
                                                             thumbnailView,
                                                             thumbnailWidth,
                                                             thumbnailHeight,
                                                             activityResources,
                                                             originalBackground,
                                                             loadingAnimation,
                                                             onCorrupt,
                                                             onFinished);
                }
                catch (Exception exception)
                {
                    AndroidCrapApplication.ApplicationLogger.LogError(exception);

                    throw;
                }
            }
            );
        }
Beispiel #4
0
        public override View GetView
        (
            int itemPosition,
            View sourceView,
            ViewGroup viewGroup
        )
        {
            sourceView = sourceView ?? InflateView(viewGroup);

            ImageView sourceThumbnail = sourceView.FindViewById <ImageView>(Resource.Id.SourceThumbnail);
            CheckBox  sourceEnabled   = sourceView.FindViewById <CheckBox>(Resource.Id.SourceEnabled);
            //TextView sourceDescription = sourceView.FindViewById<TextView>(Resource.Id.SourceDescription);
            TextView sourceDetails = sourceView.FindViewById <TextView>(Resource.Id.SourceDetails);
            AndroidCachingImageView objectThumbnail = sourceView.FindViewById <AndroidCachingImageView>(Resource.Id.ObjectThumbnail);

            HandleThumbnailScaleType(objectThumbnail);

            KeyValuePair <PortableBaseSource, int> objectSource = this[itemPosition];

            sourceView.Tag = itemPosition;

            if (sourceThumbnail != null)
            {
                if (objectSource.Key.ProviderName == "Phone") // TODO hardcoded!
                {
                    sourceThumbnail.SetImageResource(Resource.Drawable.perm_group_display);
                }
                else
                {
                    sourceThumbnail.SetImageResource(Resource.Drawable.perm_group_user_dictionary);
                }

                if (sourceEnabled != null)
                {
                    sourceThumbnail.Tag = sourceEnabled;

                    // it is save to remove event handler even if not added, yet:
                    sourceThumbnail.Click -= OnSourceThumbnailClicked;
                    sourceThumbnail.Click += OnSourceThumbnailClicked;
                }
            }

            if (sourceEnabled != null)
            {
                // http://stackoverflow.com/questions/15403417/unable-to-align-checkboxhorizontally-vertically-in-android-app

                string sourceDescription = objectSource.Key.ProviderName;

                if (objectSource.Key.SourceName != null)
                {
                    sourceDescription += " " + objectSource.Key.SourceName;
                }

                sourceEnabled.Checked        = objectSource.Key.IsEnabled;
                sourceEnabled.Tag            = new JavaObjectWrapper <PortableBaseSource>(objectSource.Key);
                sourceEnabled.Clickable      = true;
                sourceEnabled.CheckedChange -= OnSourceEnabledChange;
                sourceEnabled.CheckedChange += OnSourceEnabledChange;

                sourceEnabled.Text = string.Format("{0} ({1})",
                                                   sourceDescription,
                                                   objectSource.Key.SourceObjects.Count);
            }

            if (sourceDetails != null)
            {
                sourceDetails.Text = objectSource.Key.SourceDetails;
            }

            var correlatedEntity = objectSource.Key as PortableCorrelatedEntity;

            // it is save to remove event handler even if not added, yet:
            objectThumbnail.Click -= OnSlideThumbnailClicked;
            objectThumbnail.Click += OnSlideThumbnailClicked;

            if (correlatedEntity != null)
            {
                objectThumbnail.Tag = correlatedEntity.CorrelationTag;

                FireParallelSlideRendering(itemPosition,
                                           sourceView,
                                           objectThumbnail);
            }

            return(sourceView);
        }
        private static async Task RenderThumbnailAsync
        (
            this PortableAsyncRenderer <Bitmap> asyncRenderer,
            PortableCorrelatedEntity correlatedEntity,
            Action <Action> runOnUiThread,
            AndroidCachingImageView thumbnailView,
            int thumbnailWidth,
            int thumbnailHeight,
            Resources activityResources,
            Drawable originalBackground,
            AnimationDrawable loadingAnimation,
            Action <CorruptObjectException, ImageView> onCorrupt,
            Action <ImageView, Drawable, AnimationDrawable, bool> onFinished
        )
        {
            bool isLoaded = false;

            if (correlatedEntity.CorrelationTag != thumbnailView.GetCorrelationTag())
            {
                onFinished?.Invoke(thumbnailView,
                                   originalBackground,
                                   loadingAnimation,
                                   isLoaded);
                return;
            }

            Bitmap thumbnailBitmap = null;
            CorruptObjectException thumbnailException = null;

            BitmapDrawable thumbnailDrawable = AndroidCrapApplication.GetDrawableFromCache
                                               (
                correlatedEntity.CorrelationTag
                                               );

            if (thumbnailDrawable == null)
            {
                try
                {
                    /*thumbnailDrawable = AndroidCrapApplication.GetReusableBitmapDrawable(thumbnailWidth,
                     *                                                                   thumbnailHeight);*/
                    thumbnailBitmap = await asyncRenderer.GetThumbnailAsync
                                      (
                        correlatedEntity,
                        () => thumbnailView.GetCorrelationTag(),
                        thumbnailWidth,
                        thumbnailHeight//,
                        //thumbnailDrawable.Bitmap
                                      );
                }
                catch (Exception exception)
                {
                    AndroidCrapApplication.ApplicationLogger.LogError(exception);
                    thumbnailBitmap?.Dispose();

                    thumbnailException = exception as CorruptObjectException;

                    if (thumbnailException == null)
                    {
                        throw;
                    }
                    else
                    {
                        runOnUiThread
                        (
                            () => onCorrupt(thumbnailException,
                                            thumbnailView)
                        );
                    }

                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                if (correlatedEntity.CorrelationTag != thumbnailView.GetCorrelationTag())
                {
                    thumbnailBitmap?.Dispose();

                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                if (thumbnailBitmap == null)
                {
                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                thumbnailDrawable = AndroidCrapApplication.AddBitmapToCache
                                    (
                    correlatedEntity.CorrelationTag,
                    thumbnailBitmap,
                    activityResources
                                    );
            }

            isLoaded = true;

            onFinished?.Invoke(thumbnailView,
                               originalBackground,
                               loadingAnimation,
                               isLoaded);
            runOnUiThread
            (
                () => { thumbnailView.SetImageDrawable(thumbnailDrawable); }
            );

            /*AndroidCrapApplication.ApplicationLogger.LogDebug("{0} : {1}",
             *                                                correlatedEntity.CorrelationTag,
             *                                                GC.GetTotalMemory(false));*/
        }