Dynamic defaultPage for mobile app

Asked

Viewed 22 times

0

Good morning/late/evening!

I am developing a mobile APP! In this APP there is a login screen and a main screen. I want the user not to have to log in every time he enters the APP, the first time the app would open the login screen on the other times would open the main screen. I spent yesterday all day looking for a solution to create a dynamic defaultpage.

Try to explain how this my code!

Estrutura de arquivos do APP Estrutura de arquivos do APP

In the app-root.xml file

<Frame defaultPage="views/login/login-page"></Frame> 

In the app.js file

const applicationModule = require("tns-core-modules/application");
applicationModule.run({moduleName: "app-root"});

I’ve tried things like:

In the app-root.xml file

<Frame defaultPage="{{route}}" loaded="init"></Frame> 

In a new app-route.js file

const observableModule = require("tns-core-modules/data/observable");
exports.init= (args)=> {
    if(appSettings.getString("isLogin", "") == "true"){
        page.bindingContext = new observableModule.fromObject({route: "views/feed/feed-page"});
    }else {
        page.bindingContext = new observableModule.fromObject({route: "views/login/login-page"});
    }
};

I didn’t succeed, I tried several other things like changing the app itself.js. Trying something like In the app.js file

const applicationModule = require("tns-core-modules/application");
let module = "app-root"
if(appSettings.getString("isLogin", "") == "true") module = "views/feed/feed-page"
applicationModule.run({moduleName: module });

This attempt caused the error inserir a descrição da imagem aqui

I would like a good output to encode this feature!

Thank you in advance.

1 answer

0


I managed to solve, Unfortunately it is not a beautiful solution!

When initializing the app I direct to an intermediary Page. This page checks whether the user is logged in or not and redirects to the correct Page.

Follow the code!

app-root.xml

<Frame id="frame" defaultPage="views/initialization/initialization-page"></Frame>

app js.

const applicationModule = require("tns-core-modules/application");
applicationModule.run({moduleName: "app-root"});

In this context the first screen opened and the initialization-page.xml screen

Follow the code of this initialization-page

<Page actionBarHidden="true" loaded="init"></Page>

In initialization-page.js

const frameModule = require("tns-core-modules/ui/frame");
const appSettings = require("tns-core-modules/application-settings");

let page;
exports.init = ()=> {
    if(appSettings.getString("isLogged", "") == "true") frameModule.Frame.topmost().navigate("views/feed/feed-page");
    else frameModule.Frame.topmost().navigate("views/login/login-page");
};

That’s the way I was able to make it work!!

Whoever has a better solution, please enlighten me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.