@@ -91,52 +91,46 @@ public void cancelDownload(final ReceiptInfo receiptInfo) {
9191 receiptInfo .call = null ;
9292 }
9393
94+ ReceiptInfo add (final Project project ,
95+ final String url ,
96+ final String shopName ,
97+ final String price ) {
98+ final String absoluteUrl = Snabble .getInstance ().absoluteUrl (url );
99+ final String id = Utils .sha1Hex (absoluteUrl );
100+
101+ ReceiptInfo receiptInfo = new ReceiptInfo (id , project .getId (), absoluteUrl , shopName , price );
102+ receiptInfo .setProject (project );
103+ saveReceiptInfo (receiptInfo );
104+
105+ return receiptInfo ;
106+ }
107+
108+ /**
109+ * Downloads a receipts pdf and stores it in the projects internal storage directory.
110+ */
94111 public void download (final ReceiptInfo receiptInfo ,
95112 final ReceiptDownloadCallback callback ) {
96113 if (receiptInfo .isDownloaded ()) {
97114 callback .success (receiptInfo );
98115 return ;
99116 }
100117
101- download (receiptInfo .getProject (),
102- receiptInfo .getUrl (),
103- receiptInfo .getShopName (),
104- receiptInfo .getPrice (), callback );
105- }
106-
107- public void download (final Project project ,
108- final String url ,
109- final String shopName ,
110- final String price ,
111- final ReceiptDownloadCallback callback ) {
112- if (url == null || project == null ) {
118+ if (receiptInfo .getUrl () == null || receiptInfo .getProject () == null ) {
113119 callback .failure ();
114120 return ;
115121 }
116122
117- final String absoluteUrl = Snabble .getInstance ().absoluteUrl (url );
118- final String id = Utils .sha1Hex (absoluteUrl );
119-
120- ReceiptInfo receiptInfo = receiptInfoList .get (id );
121-
122- if (receiptInfo == null ) {
123- receiptInfo = new ReceiptInfo (id , project .getId (), absoluteUrl , shopName , price );
124- receiptInfo .setProject (project );
125- }
126-
127- final ReceiptInfo finalReceiptInfo = receiptInfo ;
128-
129123 final Request request = new Request .Builder ()
130- .url (absoluteUrl )
124+ .url (receiptInfo . getUrl () )
131125 .get ()
132126 .build ();
133127
134- Call call = project .getOkHttpClient ().newCall (request );
128+ Call call = receiptInfo . getProject () .getOkHttpClient ().newCall (request );
135129 call .enqueue (new Callback () {
136130 @ Override
137131 public void onResponse (Call call , Response response ) throws IOException {
138132 if (response .isSuccessful ()) {
139- finalReceiptInfo .call = null ;
133+ receiptInfo .call = null ;
140134
141135 ResponseBody body = response .body ();
142136 if (body == null ) {
@@ -147,14 +141,14 @@ public void onResponse(Call call, Response response) throws IOException {
147141 }
148142
149143 // .pdf extension is needed for adobe reader to work
150- File file = new File (storageDirectory , finalReceiptInfo .getId () + ".pdf" );
144+ File file = new File (storageDirectory , receiptInfo .getId () + ".pdf" );
151145 FileOutputStream fos = new FileOutputStream (file );
152146 IOUtils .copy (body .byteStream (), fos );
153- finalReceiptInfo .setFilePath (file .getAbsolutePath ());
154- saveReceiptInfo (finalReceiptInfo );
147+ receiptInfo .setFilePath (file .getAbsolutePath ());
148+ saveReceiptInfo (receiptInfo );
155149
156150 if (callback != null ) {
157- callback .success (finalReceiptInfo );
151+ callback .success (receiptInfo );
158152 }
159153 } else {
160154 if (callback != null ) {
0 commit comments