Categories
discuss

Electron app. Multiple html files

I have an electron app with multiple html files in the root directory.

  • index.html
  • page1.html
  • page.html

I cannot find a way to redirect from index.html to page1.html once Electro has started.

Does anyone know how to do this?

Answer

When your first page is index.html you call that page, when you create your window.

const win = new BrowserWindow(options);
win.loadUrl(`file://${__dirname}/index.html`);

If you want to load another page maybe

win.loadUrl(`file://${__dirname}/page.html`);

could help you.

If the page should be loaded after a user action (e.g. click on a link). You can add the link to your index.hmtl page. Electron works here exactly like a browser.

<a href="page.html">Go to page</a>
Categories
discuss

Google Guava EventBus and Exceptions in Event Handlers

the Guava EventBus documentation says that “handlers should not, in general, throw. If they do, the EventBus will catch and log the exception. This is rarely the right solution for error handling and should not be relied upon; it is intended solely to help find problems during development.”

If you know that certain exceptions might occur you can register a SubscriberExceptionHandler with the EventBus and use it to handle these exceptions.

But what happens if an unhandled exception occurs? Normally, I would want an unhandled exception to “bubble up” the calling chain. When using a SubscriberExceptionHandler, I have access to the original exception that was thrown in the event handler, and I just want to rethrow it. But I could not figure out how.

So how can you make sure unexpected exceptions in event handlers are not “swallowed”, regardless if a SubscriberExceptionHandler is used or not?

Any help would be greatly appreciated.

Answer

If you want to deal with unchecked exceptions, you could implement SubscriberExceptionHandler’s method like this:

public void handleException(Throwable exception, SubscriberExceptionContext context) {
    // Check if the exception is of some type you wish to be rethrown, and rethrow it.
    // Here I'll assume you'd like to rethrow RuntimeExceptions instead of 'consuming' them.
    if (exception instanceof RuntimeException) {
        throw (RuntimeException) exception;
    }

    // If the exception is OK to be handled here, do some stuff with it, e.g. log it.
    ...
}

After creating a class implementing the SubscriberExceptionHandler interface, you can pass its instance to the EventBus’s constructor:

EventBus eventBus = new EventBus(new MySubscriberExceptionHandler());

And done, eventBus will use your exception handler which will let RuntimeExceptions bubble up.

Categories
discuss

How to use ES8 async/await with streams?

In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams.

var fs = require('fs');
var crypto = require('crypto');

// the file you want to get the hash    
var fd = fs.createReadStream('/some/file/name.txt');
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');

fd.on('end', function() {
    hash.end();
    console.log(hash.read()); // the desired sha1sum
});

// read all file and pipe it (write it) to the hash object
fd.pipe(hash);

But is it possible to convert this to using ES8 async/await instead of using the callback as seen above, but while still keeping the efficiency of using streams?

Answer

async/await only works with promises, not with streams. There are ideas to make an extra stream-like data type that would get its own syntax, but those are highly experimental if at all and I won’t go into details.

Anyway, your callback is only waiting for the end of the stream, which is a perfect fit for a promise. You’d just have to wrap the stream:

var fd = fs.createReadStream('/some/file/name.txt');
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
// read all file and pipe it (write it) to the hash object
fd.pipe(hash);

var end = new Promise(function(resolve, reject) {
    hash.on('end', () => resolve(hash.read()));
    fd.on('error', reject); // or something like that. might need to close `hash`
});

Now you can await that promise:

(async function() {
    let sha1sum = await end;
    console.log(sha1sum);
}());
Categories
discuss

View Pager – slide to next page programmatically with static variable

I would like to make a slide programmatically in my ViewPager.

My problem is, that the event to slide is called by a button that is placed inside of a fragment that is hold by the ViewPager.

I know about the code:

viewpager.setCurrentItem(int index)

And now I my thought was to make the ViewPager variable public static to access it from the child fragment. But the static variable is not accessible from the child fragment.

How can I achieve this to make a slide by clicking on a button that is placed inside of a page?

Answer

You can use an interface to interact with your ViewPager via your Activity (or a Fragment hosted in said Activity) without exposing any fields.

Inside the ViewPager Fragment, create an interface:

public class FragmentInViewPager extends Fragment {

    private Callback mCallback;

    public interface Callback {
        void setViewPagerCurrentPage(int page);
    }

    @Override
    public void onAttach(Activity activity) {
        mCallback = (Callback) activity; 
        //NB: The above will throw ClassCastException if your Activity
        //Does not implement FragmentInViewPager.Callback
    }

    //To call it...
    int newPageNumber = 27;
    mCallback.setViewPagerCurrentPage(newPageNumber);

Have your Activity implement the interface, and from there interact with your ViewPager directly (if it is a variable in your Activity) or access it in one of your fragments:

public class MainActivity extends AppCompatActivity implements FragmentInViewPager.Callback {

    private ViewPager mViewPager;

    //...
    @Override
    public void setViewPagerCurrentPage(int page) {
        mViewPager.setCurrentItem(page);
        //Or...if it is inside another Fragment...
        ViewPagerFragment frag = (ViewPagerFragment) getFragmentManager().findFragmentByTag("view_pager_fragment_tag");
        if (frag != null) frag.getViewPager().setCurrentItem(page);
    }
Categories
discuss

Promo Video not visible on Google Play, but works on Android Play

I have added “Promo Video” to my app on Google Play. It is placed on youtube, it is between 30s and 2min and it plays correctly on Google Play accessed from Android device. But when I go to app web page on play.google.com (on my laptop, using any browser), I can’t see it there. Note that screenshots appears correctly, and video has been added to app over 48h ago.

I have already searched a lot of HELP articles and google and didn’t find any similar issue or solution.

Answer

I had the same problem. The solution was to use the full YouTube video link as explained here https://support.google.com/googleplay/android-developer/answer/1078870

  • Use the full YouTube video link instead of a shortened link.

    • Use: https://www.youtube.com/watch?v=yourvideoid
    • Don’t use: https://youtu.be/yourvideoid
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..