0

I have an Adobe Air desktop message board App built in Flash cs5, it loads an external ".txt" file in a dynamic text field and checks for a new file every 2 minutes. I need it to notify user with (NotificationType.CRITICAL) only if the file is new not just everytime it loads it. is it possible?

all the code in the app:

NativeApplication.nativeApplication.startAtLogin=true

stage.nativeWindow.alwaysInFront=true;

//external text file load and recheck every 2 minutes

var myInterval:uint  = setInterval (loadUrl, 120000);
var loader:URLLoader = new URLLoader(new URLRequest("https://my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {
  var loadedText:URLLoader = URLLoader(event.target);
if(myText_txt.htmlText!=loadedText.data){
   myText_txt.htmlText = loadedText.data;
     stage.nativeWindow.notifyUser(NotificationType.CRITICAL) 
}else {
  //do nothing
   }
}

function loadUrl():void {
    loader = new URLLoader(new URLRequest("https:///my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);
}

 // button control


Minimize_BTN.addEventListener(MouseEvent.CLICK, minimize);
function minimize(e:MouseEvent){
stage.nativeWindow.minimize();

}

drag_BTN.addEventListener(MouseEvent.MOUSE_DOWN, drag);
function drag(e:MouseEvent){
stage.nativeWindow.startMove();

}

stop(); //Stop on the frame you want

2 Answers2

0

Why don't you use FileReference object to find out about some properties like modificationDate and based on this verify if file is different you can also test it's size.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
  • That would be a more efficient way but I don't think that's possible if the file is being loaded from a web server, which appears to be the case in the example code. – T Graham Jan 16 '13 at 08:44
  • 1
    true - but if it comes from server then depending on the accessibility to the server one could add a feature e.g. PHP file that could check if file in question is modified and from flash you would call this file (in example it is PHP but could be anything SSI script) and if positive load new version of the file. – Lukasz 'Severiaan' Grela Jan 16 '13 at 09:15
  • Yes, using middleware would be the most efficient approach – you could have the server either push a notification or push the actual file whenever that file has been updated. Several possibilities... – T Graham Jan 16 '13 at 12:32
0

Your code might not work if the myText_txt field is modifying the loaded text (e.g., if myText_txt's condenseWhite property is set to true). A more accurate way to determine if the text has changed would be to store it in a String variable named, for example, oldText and then comparing oldText with the newly loaded text.

Here's part of your code re-written to include the oldText variable. This code is also more efficient because it instantiates some variables only once and it avoids duplicating some code:

import flash.net.URLRequest;

function completeHandler(event:Event):void
{
    var newText:String = loader.data;
    if(newText != oldText)
    {
            myText_txt.htmlText = newText;
            stage.nativeWindow.notifyUser(NotificationType.CRITICAL);
            oldText = newText;
    }
}

function loadUrl():void
{
    loader.load(req);
}

// Initialize your variables and event handlers only one time
var req:URLRequest = new URLRequest("https:///my_text_file.txt");

// Set cacheResponse to false to prevent a successful response
// from being cached.
req.cacheResponse = false;

// And don't bother checking the cache, either. 
// Not necessary, but the request will execute a little faster.
req.useCache = false;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);

// Use oldText inside completeHandler() to determine 
// whether the file's text has changed
var oldText:String = "";

var myInterval:uint = setInterval(loadUrl, 120000);

// Start loading the text right away
loadUrl();
T Graham
  • 1,329
  • 10
  • 14
  • Quick question on this :) Is the file only downloaded when its new also? – Brian Haugen Jan 16 '13 at 19:36
  • Sorry, I should have added that to the code when I first posted the example. The URLRequest class has a Boolean property named "cacheResponse" with a default setting of true. When you set this property to false, AIR will download the file each time a new server request is made. I've updated the code to include this setting for the URLRequest instance. – T Graham Jan 16 '13 at 20:53
  • Forgot to add the "req.useCache = false;" line of code. It's now in their too. – T Graham Jan 16 '13 at 21:11
  • ok I think I got this. so how you have it set up now is: If the file is new it will NotificationType.CRITICAL. if not do nothing. also if File is new it will download to the dynamic text field. If file is "not" new it will download nothing. correct? – Brian Haugen Jan 17 '13 at 03:53
  • The file will always be downloaded but the text field won't be updated unless the text in the latest file is different from the text currently being displayed. In order to prevent the entire file from being downloaded you would need some server-side scripting. – T Graham Jan 19 '13 at 21:39