<commit_before>import commonModule = require("./nativescript-fresco-common");
import utils = require("utils/utils");
import types = require("utils/types");
import application = require("application");
import * as fileSystemModule from "file-system";
import imageSource = require("image-source");

var fs: typeof fileSystemModule;

export function initialize(): void {
    if (application.android) {
        com.facebook.drawee.backends.pipeline.Fresco.initialize(application.android.context);
    }
};

export class FrescoDrawee extends commonModule.FrescoDrawee {
    private _android;
    private imageDrawable;
    private backgroundDrawable;

    public _createUI() {
        this._android = new com.facebook.drawee.view.SimpleDraweeView(this._context);
        this.initDrawee();
    }

    public _clearAndroidReference() {
        this._android.setImageURI(null, null);
        this._android = undefined;
        super._clearAndroidReference();
    }

    get android() {
        return this._android;
    }

    protected onImageUriChanged(args) {
        this.initImage();
    }

    protected onPlaceholderImageUriPropertyChanged(args) {
        this.initPlaceholderImage();
    }

    protected onActualImageScaleTypePropertyChanged(args) {
        this.initActualImageScaleType();
    }

    protected onFadeDurationPropertyChanged(args) {
        this.initFadeDuration();
    }

    protected onBackgroundPropertyChanged(args) {
        this.initBackground();
    }

    private initDrawee() {
        this.initImage();
        this.initPlaceholderImage();
        this.initActualImageScaleType();
        this.initFadeDuration();
        this.initBackground();
    }

    private initActualImageScaleType() {
        if (this._android) {
            if (this.actualImageScaleType) {
                this.updateHierarchy();
            }
        }
    }

    // TODO: refactor this
    private initImage() {
        if (this._android) {
            this._android.setImageURI(null, null);
            if (this.imageUri) {
                if (!utils.isFileOrResourcePath(this.imageUri)) {
                    this._android.setImageURI(android.net.Uri.parse(this.imageUri), null);
                }
                else {
                    var res = utils.ad.getApplicationContext().getResources();
                    if (!res) {
                        return;
                    }
                    if (!utils.isFileOrResourcePath(this.imageUri)) {
                        throw new Error("Path \"" + "\" is not a valid file or resource.");
                    }
                    var path = this.imageUri;
                    if (path.indexOf(utils.RESOURCE_PREFIX) === 0) {
                        var resName = path.substr(utils.RESOURCE_PREFIX.length);
                        var identifier = res.getIdentifier(resName, 'drawable', utils.ad.getApplication().getPackageName());
                        if (0 < identifier) {
                            var uri = new android.net.Uri.Builder()
                                .scheme(com.facebook.common.util.UriUtil.LOCAL_RESOURCE_SCHEME)
                                .path(java.lang.String.valueOf(identifier))
                                .build();
                            this._android.setImageURI(uri);
                        }
                    }
                }
            }
        }
    }

    private initPlaceholderImage() {
        if (this._android) {
            if (this.placeholderImageUri) {
                this.imageDrawable = this.getDrawable(this.placeholderImageUri);
                this.updateHierarchy();
            }
        }
    }

    private initFadeDuration() {
        if (this._android) {
            if (this.fadeDuration) {
                this.updateHierarchy();
            }
        }
    }

    private initBackground() {
        if (this._android) {
            if (this.background) {
                this.backgroundDrawable = this.getDrawable(this.background);

                this.updateHierarchy();
            }
        }
    }

    // TODO: check if this will not lead to performance overhead as it is called for each property set to the FrescoDrawee.
    // This is because some of the proeprties are settable obly from the GenericDraweeHierarchyBuilder rather than from the SimpleDraweeView itself. 
    private updateHierarchy() {
        var builder: GenericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder();
        if (this.placeholderImageUri) {
            builder.setPlaceholderImage(this.imageDrawable);
        }

        if (this.actualImageScaleType) {
            builder.setActualImageScaleType(this.actualImageScaleType);
        }

        if (this.fadeDuration && this.fadeDuration) {
            builder.setFadeDuration(this.fadeDuration);
        }

        if (this.background && this.backgroundDrawable) {
            builder.setBackground(this.backgroundDrawable);
        }

        var hiearchy = builder.build();
        this._android.setHierarchy(hiearchy);
    }

    private getDrawable(path: string) {
        var drawable;
        var builder: GenericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder();

        if (utils.isFileOrResourcePath(path)) {
            if (path.indexOf(utils.RESOURCE_PREFIX) === 0) {
                drawable = this.getDrawableFromResource(path);
            } else {
                drawable = this.getDrawableLocalFile(path);
            }
        }

        return drawable;
    }

    private getDrawableLocalFile(localFilePath: string) {
        var drawable;
        if (!fs) {
            fs = require("file-system");
        }

        var fileName = types.isString(localFilePath) ? localFilePath.trim() : "";
        if (fileName.indexOf("~/") === 0) {
            fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
        }

        drawable = android.graphics.drawable.Drawable.createFromPath(fileName);

        return drawable;
    }

    private getDrawableFromResource(resourceName: string) {
        var img = imageSource.fromResource(resourceName.substr(utils.RESOURCE_PREFIX.length));
        var drawable = new android.graphics.drawable.BitmapDrawable(utils.ad.getApplicationContext().getResources(), img.android);

        return drawable;
    }
}

class GenericDraweeHierarchyBuilder {
    private nativeBuilder: com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;

    constructor() {
        var res = application.android.context.getResources();
        this.nativeBuilder = new com.facebook.drawee.generic.GenericDraweeHierarchyBuilder(res);
    }

    public setPlaceholderImage(drawable): GenericDraweeHierarchyBuilder {<commit_msg>Refactored getDrawable to use image-srouce module. Implemented loading from resource for imageUri.<commit_after>import commonModule = require("./nativescript-fresco-common");
import utils = require("utils/utils");
import types = require("utils/types");
import application = require("application");
import imageSource = require("image-source");

export function initialize(): void {
    if (application.android) {
        com.facebook.drawee.backends.pipeline.Fresco.initialize(application.android.context);
    }
};

export class FrescoDrawee extends commonModule.FrescoDrawee {
    private _android;
    private imageDrawable;
    private backgroundDrawable;

    public _createUI() {
        this._android = new com.facebook.drawee.view.SimpleDraweeView(this._context);
        this.initDrawee();
    }

    public _clearAndroidReference() {
        this._android.setImageURI(null, null);
        this._android = undefined;
        super._clearAndroidReference();
    }

    get android() {
        return this._android;
    }

    protected onImageUriChanged(args) {
        this.initImage();
    }

    protected onPlaceholderImageUriPropertyChanged(args) {
        this.initPlaceholderImage();
    }

    protected onActualImageScaleTypePropertyChanged(args) {
        this.initActualImageScaleType();
    }

    protected onFadeDurationPropertyChanged(args) {
        this.initFadeDuration();
    }

    protected onBackgroundPropertyChanged(args) {
        this.initBackground();
    }

    private initDrawee() {
        this.initImage();
        this.initPlaceholderImage();
        this.initActualImageScaleType();
        this.initFadeDuration();
        this.initBackground();
    }

    private initActualImageScaleType() {
        if (this._android) {
            if (this.actualImageScaleType) {
                this.updateHierarchy();
            }
        }
    }

    private initImage() {
        if (this._android) {
            this._android.setImageURI(null);
            if (this.imageUri) {
                var image = this.getDrawable(this.imageUri);
                if (!image) {
                    this._android.setImageURI(android.net.Uri.parse(this.imageUri), null);
                } else {
                    if (utils.isFileOrResourcePath(this.imageUri)) {
                        var res = utils.ad.getApplicationContext().getResources();
                        if (!res) {
                            return;
                        }

                        var uri;
                        if (this.imageUri.indexOf(utils.RESOURCE_PREFIX) === 0) {
                            var resName = this.imageUri.substr(utils.RESOURCE_PREFIX.length);
                            var identifier = res.getIdentifier(resName, 'drawable', utils.ad.getApplication().getPackageName());
                            if (0 < identifier) {
                                uri = new android.net.Uri.Builder()
                                    .scheme(com.facebook.common.util.UriUtil.LOCAL_RESOURCE_SCHEME)
                                    .path(java.lang.String.valueOf(identifier))
                                    .build();
                            }
                        } else {
                            // TODO: load from local file
                        }

                        this._android.setImageURI(uri);
                    }
                }
            }
        }
    }

    private initPlaceholderImage() {
        if (this._android) {
            if (this.placeholderImageUri) {
                this.imageDrawable = this.getDrawable(this.placeholderImageUri);
                this.updateHierarchy();
            }
        }
    }

    private initFadeDuration() {
        if (this._android) {
            if (this.fadeDuration) {
                this.updateHierarchy();
            }
        }
    }

    private initBackground() {
        if (this._android) {
            if (this.background) {
                this.backgroundDrawable = this.getDrawable(this.background);

                this.updateHierarchy();
            }
        }
    }

    // TODO: check if this will not lead to performance overhead as it is called for each property set to the FrescoDrawee.
    // This is because some of the proeprties are settable obly from the GenericDraweeHierarchyBuilder rather than from the SimpleDraweeView itself. 
    private updateHierarchy() {
        var builder: GenericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder();
        if (this.placeholderImageUri) {
            builder.setPlaceholderImage(this.imageDrawable);
        }

        if (this.actualImageScaleType) {
            builder.setActualImageScaleType(this.actualImageScaleType);
        }

        if (this.fadeDuration && this.fadeDuration) {
            builder.setFadeDuration(this.fadeDuration);
        }

        if (this.background && this.backgroundDrawable) {
            builder.setBackground(this.backgroundDrawable);
        }

        var hiearchy = builder.build();
        this._android.setHierarchy(hiearchy);
    }

    private getDrawable(path: string) {
        var drawable;
        var builder: GenericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder();

        if (utils.isFileOrResourcePath(path)) {
            if (path.indexOf(utils.RESOURCE_PREFIX) === 0) {
                drawable = this.getDrawableFromResource(path);
            } else {
                drawable = this.getDrawableFromLocalFile(path);
            }
        }

        return drawable;
    }

    private getDrawableFromLocalFile(localFilePath: string) {
        var img = imageSource.fromFile(localFilePath);
        var drawable = new android.graphics.drawable.BitmapDrawable(utils.ad.getApplicationContext().getResources(), img.android);

        return drawable;
    }

    private getDrawableFromResource(resourceName: string) {
        var img = imageSource.fromResource(resourceName.substr(utils.RESOURCE_PREFIX.length));
        var drawable = new android.graphics.drawable.BitmapDrawable(utils.ad.getApplicationContext().getResources(), img.android);

        return drawable;
    }
}

class GenericDraweeHierarchyBuilder {
    private nativeBuilder: com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;

    constructor() {
        var res = application.android.context.getResources();
        this.nativeBuilder = new com.facebook.drawee.generic.GenericDraweeHierarchyBuilder(res);
    }

    public setPlaceholderImage(drawable): GenericDraweeHierarchyBuilder {
{'fraction_non_alphanumeric': 0.09695917905379914, 'fraction_numerical': 0.0005281822983475439, 'mean_word_length': 2.01775956284153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '946399', 'n_tokens_mistral': 3558, 'n_tokens_neox': 3443, 'n_words': 605}