Sunday, 29 September 2019

How to access sub elements of a custom FrameLayout in a library project?

I am extending a "MapView" that itself extends FrameLayout. But I can't access the ui elements I added: level_layout and *level_scroll. I have no trouble adding the code directly to my MainActivity, but I can't get it to work in a library.

<?xml version="1.0" encoding="utf-8"?>
<android.company.com.library.mapbox.MyMapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mapView">

        <ScrollView
            android:id="@+id/level_scroll"
            android:layout_width="50dp"
            android:layout_height="250dp"
            android:layout_gravity="right"
            android:layout_marginTop="100dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/level_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"></LinearLayout>
        </ScrollView>
</android.company.com.library.mapbox.MyMapView>

In MyMapView.java I am getting a value from:

int level = R.id.level_layout;

But linear is null when trying to get the LinearLayout

LinearLayout linear = (LinearLayout) findViewById(R.id.level_layout);

I tried Adding

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
}

(I try to access my ui elements after this is called)

Calling the following in different places (e.g. constructors)

LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService
        (Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.nameOfTheXmlFile, null);

I don't have multiple layout files with the same name.

I can't use setContentView(...) since this is a View and not an Activity?



from How to access sub elements of a custom FrameLayout in a library project?

Can't upload large files to AWS with Multer S3 NodeJs

I can't upload large files to aws using multer-s3. I'm using the following code:

const upload = multer({
  storage: multerS3({
    s3,
    bucket: 'welive-inc',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: 'TESTING_META_DATA!'});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + randtoken.uid(16))
    }
  })
})

const singleUpload = upload.single('file');

router.post('/image-upload', (req, res) =>{
  singleUpload(req, res, function(err) {
    if (err) {
      console.log(err)
      return res.status(422).send({errors: [{title: 'File Upload Error', detail: err.message}] });
    }
    return res.json({'imageUrl': req.file.location});
  });
});

This code works for small files (images or really small videos), but when it comes to relatively larger files it doens't work. and the console.log(err) returns this error:

{ Error: write EPIPE
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:66:16)
  message: 'write EPIPE',
  errno: 'EPIPE',
  code: 'NetworkingError',
  syscall: 'write',
  region: 'eu-west-3',
  hostname: 'welive-inc.s3.eu-west-3.amazonaws.com',
  retryable: true,
  time: 2019-06-17T21:15:46.958Z,
  statusCode: 400,
  storageErrors: [] }

The frontend doesn't even wait for response and returns this error after couple minutes

net::ERR_EMPTY_RESPONSE


from Can't upload large files to AWS with Multer S3 NodeJs

Last pressed index + 1?

I am using the youtube api to search for youtube videos. The videos will then be displayed on #searchBar with the video id ex. NJNlqeMM8Ns as data-video. I get the video id by pressing on a img:

<img data-video = "" src = "bilder/play.png" alt = "play" class = "knapp" width = "40" height = "40">

Which in my poorly understanding of javascript becomes (this). When I search for videos I will get more than one result which means that I will get more than one img tag.

In this case i want to play the next song when the first one is finished. I tried to get the index when i pressed on my img tag:

        $(".knapp").click(function(){
    var index = $(".knapp").index(this);
    alert(index);
    });

However, when I alerted the index after the video was finshed I always got the value 0 back.

So I thought i could do something like this:

function onPlayerStateChange(event) {
   if (event.data == YT.PlayerState.ENDED){
    playNext();
   }
}

$('#searchBar').on('click', '[data-video]', function(){
player.current_video = $(this).attr('data-video');
playVideo();
});

function playVideo(){
var video_id = player.current_video;
player.loadVideoById(video_id, 0, "large");
}

function playNext(){
var player.current_videon = **$(this + 1).attr('data-video');**
var next_id = player.current_videon;
player.loadVideoById(next_id, 0, "large");
}

But I'm not sure how to make it work, as you can see in the bold section, can I solve my problem like this or do I need another approach?

I tried the next() suggestion, but I think something is missing to complete the exchange

function playNext(){
player.current.videon = $(".knapp").next("[data-video]");
var video_idd = player.current.videon;
player.loadVideoById(video_idd, 0, "large");
}

With some research I found out that i need to set the value of the current video being played and also efter the video was done playing il add this number by 1. However even if it did make the next video play, i was unable to chose the song i wanted anymore...

    function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED){
    player.current_video++;
    playVideo();
}
}
var player = document.querySelector('iframe');
function onYouTubeIframeAPIReady() {
 player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '40mSZPyqpag',
playerVars: {rel: 0},
events: {
  'onStateChange': onPlayerStateChange
}
});
player.current_video = 0;
}

$('#searchBar').on('click', '[data-video]', function(){
 player.current_video = $(this).index();
 playVideo();
});

function playVideo(){
var video_id = $('[data-video]').eq(player.current_video).attr('data-video');
player.loadVideoById(video_id, 0, "large");
}  


from Last pressed index + 1?