how to use foreign code

Hi fuse. I am trying to figure out how to use foreign code. this is the code i wrote to get print out some text using Log.d .
can anyone please help me to figure this out. i really want to learn this and i will write some community libraries after that.
Thank you…

my uno file - (CallLog.uno)

using Uno.Threading;
using Uno;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Android;
using Uno.Permissions;
using Fuse;
using Fuse.Scripting;

[extern(Android) ForeignInclude(Language.Java, "android.content.Context")]

[UXGlobalModule]
public class CallLog : NativeModule{
    public CallLog(){
        AddMember(new NativeFunction("callHistory",(NativeCallback)callHistory));
    }

    object callHistory(Context c, object[] args){
        callHistory();
        return null;
    }

    [Foreign(Language.Java)]
    public static extern(Android) void callHistory()
    @{  

        android.util.Log.d("ForeignCodeExample","hi");

    @}      

    static extern(!Android) void callHistory(){
        debug_log("Notifications not supported on this platform.");
    }

}

my ux file

<App>
    <DockPanel>
        <StatusBarBackground Dock="Top" />
        <BottomBarBackground Dock="Bottom" />

        <CallLog ux:Global="CallLog" />

        <JavaScript>
            var Observable = require("FuseJS/Observable");
            var Log = require("CallLog");
            var val = Observable("");

            function call() {
                Log.callHistory();
            }
            module.exports = {call};

        </JavaScript>
        <Panel Height="60" Width="200">
        <Rectangle Color="Blue">
            <Clicked Handler="{call}" />
        </Rectangle>
        </Panel>
    </DockPanel>
</App>

my unoproj file -

{
  "RootNamespace":"",
  "Packages": [
    "Fuse",
    "FuseJS",
    "Uno.Permissions"
  ],
  "Includes": [
    "*",
    "*.uno"
  ]
}


I am also having a problem with the foreign code. Hope this answer will help me. I really appreciate if you can improve the foreign code documentation with good examples.

Pretty decent attempt right there, and I’m wondering if there was any particular issues you wanted to ask about?

There’s a couple suggestions I have:

  1. You don’t need the "*.uno" line in .unoproj, because "*" already includes all .uno files.
  2. Global modules need to register an instance (showing in code below).
  3. You should use a bit better naming for the methods, and avoid using the same names for different things (showing in code below).

Other than that, yeah man! Keep going!

using Uno.Threading;
using Uno;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Android;
using Uno.Permissions;
using Fuse;
using Fuse.Scripting;

[extern(Android) ForeignInclude(Language.Java, "android.content.Context")]

[UXGlobalModule]
public class CallLog : NativeModule{
	static readonly CallLog _instance;
    public CallLog(){

    	if (_instance != null) return;
    	_instance = this;
    	Resource.SetGlobalKey(_instance, "CallLog");

        AddMember(new NativeFunction("callHistory",(NativeCallback)CallHistory));
    }

    object CallHistory(Context c, object[] args){
        CallHistoryImpl();
        return null;
    }

    [Foreign(Language.Java)]
    public static extern(Android) void CallHistoryImpl()
    @{  

        android.util.Log.d("ForeignCodeExample","hi");

    @}      

    static extern(!Android) void CallHistoryImpl(){
        debug_log("Notifications not supported on this platform.");
    }

}
1 Like