If no recent info then make clickable for login


#1

Hi,
I have an app that currently is checking for recent tweets, however for the recent tweets to be visible my app forces you to login to twitter immediately when opening a page that has recent tweet fields.
Right now I’m trying to modify it so that instead in the field that recent tweets would be it gives a button that says click here to login and see tweets.
Then after the first time they do that they no longer see the button again because they are logged in.
I feel like this is a better way of doing it anyways.

        function getLatestTweet() {

            var twitterUsername = $scope.truck.twitter_name.replace('@', '');
            var value, cacheKey = 'user_timeline_count_2_' + twitterUsername;
            // Get the latest twitter information for current truck
            if ($scope.truck.twitter_name && ionic.Platform.isIOS()) {

                value = OpenTW.getFromTwitterCache(cacheKey);
             
                if (value) {
                    $scope.recentTweet = value.recentTweet;
                    $scope.recentTweet.text = value.text;
                    console.log("used cache " + cacheKey);
                    return;
                }


                Twitter.getTWRequest(
                    'statuses/user_timeline.json',
                    {screen_name: twitterUsername, count: "2"},
                    function (response) {
              

                        $scope.recentTweet = response[0];
                        $scope.recentTweet.text = htmlEntitiesToUnicode(response[0].text);

                        OpenTW.addToTwitterCache(cacheKey, {
                            'recentTweet' : response[0],
                            'text' : htmlEntitiesToUnicode(response[0].text)
                        });

                        $scope.$apply();
                    },
                    function (error) {
                        $ionicPopup.alert({
                            title: ' Error',
                            content: "Error getting twitter information: " + error,
                            okType: 'prompt-button-default'
                        });

                    },
                    {requestMethod: 'GET'}
                );
            } else {
                if (ionic.Platform.isAndroid()) {

                    value = OpenTW.getFromTwitterCache(cacheKey);
                    if (value) {
                        $scope.recentTweet = value.recentTweet;
                        $scope.recentTweet.text = value.text;
                        console.log("used cache " + cacheKey);
                        return;
                    }


                    OpenTW.verify()
                        .then(function () {
                            return OpenTW.apiGetCall({
                                url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
                                data: {
                                    'screen_name': twitterUsername,
                                    'count': "25"
                                }
                            });
                        })
                        .then(function (_data) {
                            $scope.recentTweet = _data[0];
                            $scope.recentTweet.text = htmlEntitiesToUnicode(_data[0].text);

                            OpenTW.addToTwitterCache(cacheKey, {
                                'recentTweet' : _data[0],
                                'text' : htmlEntitiesToUnicode(_data[0].text)
                            });

                        }, function (_error) {
                            $ionicPopup.alert({
                                title: 'Error',
                                content: "usersLookup failure: " + _error + twitterName,
                                okType: 'prompt-button-default'
                            });
                        });
                }
            }
        }

        /**
         *
         */
        function initializeController() {


            $scope.showActivity();

            $scope.truck = KinveyService.getTruckById($stateParams.truckId);

           
            if (ionic.Platform.isAndroid()) {
                if (!window.TW_initialized) {
                    OpenTW.init().then(
                        function () {
                            console.log("login twitter");
                            window.TW_initialized = true;
                            getLatestTweet();
                        },
                        function (error) {
                            console.log('twitter login failed ', error);
                            window.TW_initialized = false;
                        });
                } else {
                    getLatestTweet();
                }

            } else {
                getLatestTweet();
            }

            // adjust the centering of the title after page is displayed
            // @TODO move this to the .css file when I understand it better
            setTimeout(function () {
                angular.element(document).find('h1')[0].style.cssText = 'left: 20px; right: 40px;';
            }, 100);



        }

        initializeController();