<commit_before>angular.module('moped.nowplaying', [
  'moped.mopidy',
  'moped.lastfm',
  'moped.util'
])

.controller('NowPlayingCtrl', function NowPlayingController($scope, mopidyservice, lastfmservice, util) {
  var checkPositionTimer;
  var isSeeking = false;
  var defaultTrackImageUrl = 'assets/images/vinyl-icon.png';

  resetCurrentTrack();

  $scope.$on('moped:slidervaluechanging', function(event, value) {
    isSeeking = true;
  });

  $scope.$on('moped:slidervaluechanged', function(event, value) {
    seek(value);
    isSeeking = false;
  });

  $scope.$on('mopidy:state:online', function(event, data) {
    mopidyservice.getCurrentTrack().then(function(track) {
      mopidyservice.getTimePosition().then(function(timePosition) {
        updateCurrentTrack(track, timePosition);
      });
    });
    mopidyservice.getState().then(function (state) {
      if (state === 'playing') {
        checkPositionTimer = setInterval(function() {
          checkTimePosition();
        }, 1000);                
      }
    });
  });

  $scope.$on('mopidy:state:offline', function() {
    clearInterval(checkPositionTimer);
    resetCurrentTrack();
  });

  $scope.$on('mopidy:event:playbackStateChanged', function(event, data) {
    if (data.new_state === 'playing') {
      checkPositionTimer = setInterval(function() {
        checkTimePosition();
      }, 1000); 
    }
    else {
      clearInterval(checkPositionTimer);
    }
  });

  $scope.$on('mopidy:event:trackPlaybackStarted', function(event, data) {
    updateCurrentTrack(data.tl_track.track, data.time_position);
  });

  $scope.$on('mopidy:event:trackPlaybackPaused', function(event, data) {
    updateCurrentTrack(data.tl_track.track, data.time_position);
  });

  function updateCurrentTrack(track, timePosition) {
    if (track) {
      $scope.currentTrack = track.name;
      $scope.currentArtists = track.artists;
      $scope.currentTrackLength = track.length;
      $scope.currentTrackLengthString = util.timeFromMilliSeconds(track.length);

      if (track !== null && timePosition !== null && track.length > 0) {
        $scope.currentTimePosition = (timePosition / track.length) * 100;
        $scope.currentTrackPosition = util.timeFromMilliSeconds(timePosition);
      }
      else
      {
        $scope.currentTimePosition = 0;
        $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
      }

      if (track.album !== undefined) {
        $scope.currentAlbumUri = track.album.uri;
        lastfmservice.getTrackImage(track, 'medium', function(err, trackImageUrl) {
          if (! err && trackImageUrl !== undefined && trackImageUrl !== '') {
            $scope.currentTrackImageUrl = trackImageUrl;
          }
          else
          {
            $scope.currentTrackImageUrl = defaultTrackImageUrl;
          }
          $scope.$apply();
        });
      }
      else {
        $scope.currentAlbumUri = null;
      }
    }
  }

  function resetCurrentTrack() {
    $scope.currentTrack = '';
    $scope.currentAlbumUri = '';
    $scope.currentArtists = [];
    $scope.currentTrackLength = 0;
    $scope.currentTrackLengthString = '';
    $scope.currentTimePosition = 0; // 0-100
    $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
    $scope.currentTrackImageUrl = defaultTrackImageUrl;
  }

  function checkTimePosition() {
    if (! isSeeking) {
      mopidyservice.getTimePosition().then(function(timePosition) {
        if ($scope.currentTrackLength > 0 && timePosition > 0) {
          $scope.currentTimePosition = (timePosition / $scope.currentTrackLength) * 100;
          $scope.currentTrackPosition = util.timeFromMilliSeconds(timePosition);
        }
        else {
          $scope.currentTimePosition = 0;
          $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
        }
      });
    }
  }

  function seek(sliderValue) {
    if ($scope.currentTrackLength > 0) {
      var milliSeconds = ($scope.currentTrackLength / 100) * sliderValue;
      mopidyservice.seek(milliSeconds);      
    }
  }

});
<commit_msg>Update time position with separate timer every second.<commit_after>angular.module('moped.nowplaying', [
  'moped.mopidy',
  'moped.lastfm',
  'moped.util'
])

.controller('NowPlayingCtrl', function NowPlayingController($scope, mopidyservice, lastfmservice, util) {
  var CHECK_POSITION_INTERVAL = 10000;
  var INCREASE_POSITION_INTERVAL = 1000;
  var checkPositionTimer;
  var increasePositionTimer;
  var isSeeking = false;
  var defaultTrackImageUrl = 'assets/images/vinyl-icon.png';

  resetCurrentTrack();

  $scope.$on('moped:slidervaluechanging', function(event, value) {
    isSeeking = true;
  });

  $scope.$on('moped:slidervaluechanged', function(event, value) {
    seek(value);
    isSeeking = false;
    checkTimePosition();
  });

  $scope.$on('mopidy:state:online', function(event, data) {
    mopidyservice.getCurrentTrack().then(function(track) {
      mopidyservice.getTimePosition().then(function(timePosition) {
        updateCurrentTrack(track, timePosition);
      });
    });
    mopidyservice.getState().then(function (state) {
      if (state === 'playing') {
        checkPositionTimer = setInterval(function() {
          checkTimePosition();
        }, CHECK_POSITION_INTERVAL);                
        checkTimePosition();
      }
    });
  });

  $scope.$on('mopidy:state:offline', function() {
    clearInterval(checkPositionTimer);
    clearInterval(increasePositionTimer);
    resetCurrentTrack();
  });

  $scope.$on('mopidy:event:playbackStateChanged', function(event, data) {
    if (data.new_state === 'playing') {
      checkPositionTimer = setInterval(function() {
        checkTimePosition();
      }, CHECK_POSITION_INTERVAL); 
      checkTimePosition();
    }
    else {
      clearInterval(checkPositionTimer);
      clearInterval(increasePositionTimer);
    }
  });

  $scope.$on('mopidy:event:trackPlaybackStarted', function(event, data) {
    updateCurrentTrack(data.tl_track.track, data.time_position);
  });

  $scope.$on('mopidy:event:trackPlaybackPaused', function(event, data) {
    updateCurrentTrack(data.tl_track.track, data.time_position);
  });

  function updateCurrentTrack(track, timePosition) {
    if (track) {
      $scope.currentTrack = track.name;
      $scope.currentArtists = track.artists;
      $scope.currentTrackLength = track.length;
      $scope.currentTrackLengthString = util.timeFromMilliSeconds(track.length);

      if (track !== null && timePosition !== null && track.length > 0) {
        $scope.currentTimePosition = (timePosition / track.length) * 100;
        $scope.currentTrackPosition = util.timeFromMilliSeconds(timePosition);
      }
      else
      {
        $scope.currentTimePosition = 0;
        $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
      }

      if (track.album !== undefined) {
        $scope.currentAlbumUri = track.album.uri;
        lastfmservice.getTrackImage(track, 'medium', function(err, trackImageUrl) {
          if (! err && trackImageUrl !== undefined && trackImageUrl !== '') {
            $scope.currentTrackImageUrl = trackImageUrl;
          }
          else
          {
            $scope.currentTrackImageUrl = defaultTrackImageUrl;
          }
          $scope.$apply();
        });
      }
      else {
        $scope.currentAlbumUri = null;
      }
    }
  }

  function resetCurrentTrack() {
    $scope.currentTrack = '';
    $scope.currentAlbumUri = '';
    $scope.currentArtists = [];
    $scope.currentTrackLength = 0;
    $scope.currentTrackLengthString = '';
    $scope.currentTimePosition = 0; // 0-100
    $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
    $scope.currentTrackImageUrl = defaultTrackImageUrl;
  }

  function checkTimePosition() {
    if (! isSeeking) {

      clearInterval(increasePositionTimer);

      mopidyservice.getTimePosition().then(function(timePosition) {
        if ($scope.currentTrackLength > 0) {
          $scope.currentTimePosition = (timePosition / $scope.currentTrackLength) * 100;
          $scope.currentTrackPosition = util.timeFromMilliSeconds(timePosition);
          // start timer that updates position without checking mopidy
          increasePositionTimer = setInterval(function() {
            updateTimePosition(INCREASE_POSITION_INTERVAL);
          }, INCREASE_POSITION_INTERVAL);    
        }
        else {
          $scope.currentTimePosition = 0;
          $scope.currentTrackPosition = util.timeFromMilliSeconds(0);
        }
      });
    }
  }

  function updateTimePosition(msToUpdate) {
    if (! isSeeking && checkPositionTimer && $scope.currentTrackLength > 0) {
      var previousTimePositionMs = ($scope.currentTimePosition / 100) * $scope.currentTrackLength;
      var newTimePositionMs = previousTimePositionMs + msToUpdate;
      $scope.currentTimePosition = (newTimePositionMs / $scope.currentTrackLength) * 100;
      $scope.currentTrackPosition = util.timeFromMilliSeconds(newTimePositionMs);
      $scope.$apply();
    }
  }

  function seek(sliderValue) {
    if ($scope.currentTrackLength > 0) {
      var milliSeconds = ($scope.currentTrackLength / 100) * sliderValue;
      mopidyservice.seek(milliSeconds);
    }
  }

});

{'fraction_non_alphanumeric': 0.12865052654434914, 'fraction_numerical': 0.007708175008142438, 'mean_word_length': 3.1420863309352516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3808959', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2698, 'n_words': 400}