1- package io.snabble.sdk ;
1+ package io.snabble.sdk
2+
3+ import io.snabble.sdk.Snabble.instance
4+ import io.snabble.sdk.Snabble.application
5+ import io.snabble.sdk.Snabble.projects
6+ import io.snabble.sdk.ReceiptInfo
7+ import io.snabble.sdk.ReceiptsApi
8+ import io.snabble.sdk.ReceiptsApi.RawReceiptUpdateCallback
9+ import io.snabble.sdk.Receipts.ReceiptInfoCallback
10+ import io.snabble.sdk.ReceiptsApi.ReceiptUpdateCallback
11+ import io.snabble.sdk.Receipts.ReceiptDownloadCallback
12+ import io.snabble.sdk.Snabble
13+ import io.snabble.sdk.Project
14+ import io.snabble.sdk.utils.Dispatch
15+ import okhttp3.Call
16+ import okhttp3.Callback
17+ import okhttp3.Request
18+ import okhttp3.Response
19+ import org.apache.commons.io.IOUtils
20+ import java.io.File
21+ import java.io.FileOutputStream
22+ import java.io.IOException
23+ import kotlin.Throws
24+
25+ /* *
26+ * Class to download user receipts in pdf format to internal storage.
27+ */
28+ class Receipts internal constructor() {
29+ private val receiptsApi = ReceiptsApi ()
30+ private var call: Call ? = null
231
3- import androidx.annotation.NonNull;
4-
5- import org.apache.commons.io.IOUtils;
6-
7- import java.io.File;
8- import java.io.FileOutputStream;
9- import java.io.IOException;
10-
11- import io.snabble.sdk.utils.Dispatch;
12- import okhttp3.Call;
13- import okhttp3.Callback;
14- import okhttp3.Request;
15- import okhttp3.Response;
16-
17- public class Receipts {
18-
19- public interface ReceiptInfoCallback {
20- void success(ReceiptInfo [] receiptInfos);
21- void failure();
22- }
23-
24- public interface ReceiptDownloadCallback {
25- void success(File pdf);
26- void failure();
27- }
28-
29- private final ReceiptsApi receiptsApi;
30- private Call call;
31-
32- Receipts () {
33- receiptsApi = new ReceiptsApi ();
34- }
35-
36- public void getRawReceipts(final ReceiptsApi .RawReceiptUpdateCallback rawReceiptUpdateCallback) {
37- receiptsApi.getRaw(rawReceiptUpdateCallback);
32+ /* *
33+ * Get the raw api response of the receipts
34+ */
35+ fun getRawReceipts (rawReceiptUpdateCallback : RawReceiptUpdateCallback ? ) {
36+ receiptsApi.getRaw(rawReceiptUpdateCallback)
3837 }
3938
40- public void getReceiptInfos(final ReceiptInfoCallback receiptInfoCallback) {
41- receiptsApi.get(new ReceiptsApi .ReceiptUpdateCallback () {
42- @Override
43- public void success(final ReceiptInfo [] receiptInfos) {
44- Dispatch .mainThread(() -> receiptInfoCallback.success(receiptInfos));
39+ /* *
40+ * Get the receipt info
41+ */
42+ fun getReceiptInfo (receiptInfoCallback : ReceiptInfoCallback ) {
43+ receiptsApi.get(object : ReceiptUpdateCallback {
44+ override fun success (receiptInfos : Array <ReceiptInfo >) {
45+ Dispatch .mainThread { receiptInfoCallback.success(receiptInfos) }
4546 }
4647
47- @Override
48- public void failure() {
49- Dispatch .mainThread(receiptInfoCallback::failure);
48+ override fun failure () {
49+ Dispatch .mainThread { receiptInfoCallback.failure() }
5050 }
51- });
51+ })
5252 }
5353
54- public void cancelDownload() {
55- if (call != null ) {
56- call.cancel();
57- call = null ;
58- }
54+ @Deprecated(message = " Use getReceiptInfo instead" )
55+ fun getReceiptInfos (receiptInfoCallback : ReceiptInfoCallback ) {
56+ getReceiptInfo(receiptInfoCallback)
57+ }
58+
59+ /* *
60+ * Cancel the currently outstanding download.
61+ */
62+ fun cancelDownload () {
63+ call?.cancel()
64+ call = null
5965 }
6066
6167 /* *
6268 * Downloads a receipts pdf and stores it in the projects internal storage directory.
6369 */
64- public void download(final ReceiptInfo receiptInfo,
65- final ReceiptDownloadCallback callback) {
66- if (receiptInfo.getPdfUrl() == null ) {
67- callback.failure();
68- return ;
70+ fun download (receiptInfo : ReceiptInfo , callback : ReceiptDownloadCallback ? ) {
71+ if (receiptInfo.pdfUrl == null ) {
72+ callback?.failure()
73+ return
6974 }
75+ val request: Request = Request .Builder ()
76+ .url(receiptInfo.pdfUrl)
77+ .get()
78+ .build()
7079
71- final Request request = new Request .Builder ()
72- .url(receiptInfo.getPdfUrl())
73- .get()
74- .build();
75-
76- cancelDownload();
80+ cancelDownload()
7781
7882 // .pdf extension is needed for adobe reader to work
79- final File file = new File (Snabble .getInstance().getApplication().getCacheDir(),
80- receiptInfo.getId() + " .pdf" );
81-
83+ val file = File (instance.application.cacheDir, receiptInfo.id + " .pdf" )
8284 if (file.exists()) {
83- if (callback != null ) {
84- callback.success(file);
85- }
86-
87- return ;
85+ callback?.success(file)
86+ return
8887 }
8988
90- Project project = null ;
91-
92- Snabble snabble = Snabble .getInstance();
93- for (Project p : snabble.getProjects()) {
94- if (p.getId().equals(receiptInfo.getProjectId())) {
95- project = p;
96- break ;
97- }
98- }
89+ val project = Snabble .projects.find { it.id == receiptInfo.projectId }
9990
10091 if (project == null ) {
101- callback.failure();
102- return ;
92+ callback? .failure()
93+ return
10394 }
10495
105- call = project.getOkHttpClient().newCall(request);
106- call.enqueue(new Callback () {
107- @Override
108- public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
109- if (response.isSuccessful()) {
110- FileOutputStream fos = new FileOutputStream (file);
111- IOUtils .copy(response.body().byteStream(), fos);
112-
113- if (callback != null ) {
114- callback.success(file);
115- }
96+ call = project.okHttpClient.newCall(request)
97+ call?.enqueue(object : Callback {
98+ @Throws(IOException ::class )
99+ override fun onResponse (call : Call , response : Response ) {
100+ if (response.isSuccessful) {
101+ val fos = FileOutputStream (file)
102+ IOUtils .copy(response.body?.byteStream(), fos)
103+ callback?.success(file)
116104 } else {
117- if (callback != null ) {
118- callback.failure();
119- }
105+ callback?.failure()
120106 }
121-
122- response.close();
107+ response.close()
123108 }
124109
125- @Override
126- public void onFailure(@NonNull Call call, @NonNull IOException e) {
127- if (callback != null ) {
128- callback.failure();
129- }
110+ override fun onFailure (call : Call , e : IOException ) {
111+ callback?.failure()
130112 }
131- });
113+ })
114+ }
115+
116+ interface ReceiptInfoCallback {
117+ fun success (receiptInfos : Array <ReceiptInfo >? )
118+ fun failure ()
119+ }
120+
121+ interface ReceiptDownloadCallback {
122+ fun success (pdf : File ? )
123+ fun failure ()
132124 }
133125}
0 commit comments