1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
otg.ui.createCameraWindow = function() {
  var win = Ti.UI.createWindow();
  
  var containerView = Ti.UI.createView({
    backgroundColor:'#85AAD7',
  });

  // uploadsQueue is an empty global array	
  var tableQueue = Ti.UI.createTableView({
    backgroundColor:'#85AAD7',
    separatorColor:'#85AAD7',
    data:uploadsQueue,
    width:320
  });
	
  containerView.add(tableQueue);
	
  // device stores the candidate file on the system and sends data
  Ti.App.addEventListener('uploadReady',function(candidate) {
    // here we an add a caption if we please
    var captionView = Ti.UI.createView({
      backgroundColor:'#FFF'
    });
	
    var candidateIMG = Ti.UI.createImageView({
      // the candidate file was stored with a uniqie identifier
      image:Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory,candidate.fileStamp + '-thumb320.png'),
      height:367,
      width:320,
      top:0,
      opacity:0.5
    });

    var captionLabel = Ti.UI.createLabel({
      text:'caption',
      color:'#000',
      top:-180,
      textAlign:'center',
      opacity:0.5
    });
		
    var captionBox = Ti.UI.createTextField({
      height:35,
      width:270,
      textAlign:'center',
      borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
    });
		
    var submitButton = Ti.UI.createButton({
      title:'Submit',
      color:'#000',
      selectedColor:'#FFF',
      top:220,
      width:130,
      height:35
    });
		
    captionView.add(candidateIMG);
    captionView.add(captionLabel);
    captionView.add(captionBox);
    captionView.add(submitButton);
		
    // cover the table view with the temporary caption prompt
    containerView.add(captionView);
		
    submitButton.addEventListener('click',function(){
      candidate.caption = captionBox.value;
      // start the xhr send
      otg.remote.beginUpload(candidate);
      // go back to the table view
      containerView.remove(captionView);
      var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory,candidate.fileStamp + '-thumb75.png');
      var progressBar = Ti.UI.createProgressBar({
        width:205,
        height:50,
        right:20,
        min:0,
        max:1,
        value:0,
        style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
        color:'#FFF'
      });
      var thisRow = Ti.UI.createTableViewRow({
        backgroundColor:'#FFF',
        layout:"vertical",
        height:75,
        fileStamp:candidate.fileStamp
      });
      // apprently the progress bar needs to be inside a view to align correctly
      var nestedView = Ti.UI.createView({
        height:75,
        width:320
      });
      var tinyThumb = Ti.UI.createImageView({
        image:tempFile,
        height:75,
        width:75,
        left:0
      });
      nestedView.add(tinyThumb);
      progressBar.show();
      nestedView.add(progressBar);
      thisRow.add(nestedView);
      uploadsQueue.push(thisRow);
      tableQueue.setData(uploadsQueue);
    });
    
    // the xhr.onsendstream will repeatedly fire this function and send an object containing e.progress and an id
    Ti.App.addEventListener('streamProgress',function(upload) {
      for (i=0;i<uploadsQueue.length;i++) {
        if (uploadsQueue[i].fileStamp==upload.id) {
          thisRow = uploadsQueue[i];
          // here I get lost
        }
      }
    });
  });
	
  win.add(containerView);

  return win;
};