Skip to content

Commit ce8143e

Browse files
committed
Make it so that canDownload() returns a reason instead of a boolean.
1 parent b2d75d5 commit ce8143e

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
package org.librarysimplified.http.api
22

3+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_AVAILABLE
4+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_NOT_PERMITTED
5+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_UNAVAILABLE
6+
37
/**
48
* An observable interface that indicates the availability of different network types.
59
*/
610

711
interface LSHTTPNetworkAccessReadableType :
812
LSHTTPNetworkAvailabilityReadableType,
913
LSHTTPNetworkPolicyReadableType {
14+
15+
/**
16+
* The reason that downloads may or may not be available.
17+
*/
18+
19+
enum class LSHTTPNetworkAvailability {
20+
NETWORK_UNAVAILABLE,
21+
NETWORK_AVAILABLE,
22+
NETWORK_NOT_PERMITTED,
23+
}
24+
1025
/**
1126
* Determine if performing a download should be allowed given the current network availability
1227
* and permissions.
1328
*/
1429

15-
fun canDownload(): Boolean {
16-
if (this.wifiAvailable.get() && this.wifiPermitted.get()) {
17-
return true
30+
fun canDownload(): LSHTTPNetworkAvailability {
31+
val wifiAvailable = this.wifiAvailable.get()
32+
val cellAvailable = this.cellularAvailable.get()
33+
val wifiPermitted = this.wifiPermitted.get()
34+
val cellPermitted = this.cellularPermitted.get()
35+
36+
if (!wifiAvailable && !cellAvailable) {
37+
return NETWORK_UNAVAILABLE
38+
}
39+
40+
if (wifiAvailable && wifiPermitted) {
41+
return NETWORK_AVAILABLE
1842
}
19-
if (this.cellularAvailable.get() && this.cellularPermitted.get()) {
20-
return true
43+
44+
if (cellAvailable && cellPermitted) {
45+
return NETWORK_AVAILABLE
2146
}
22-
return false
47+
48+
return NETWORK_NOT_PERMITTED
2349
}
2450
}

org.librarysimplified.http.tests/src/test/java/org/librarysimplified/http/tests/LSHTTPNetworkAccessTest.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package org.librarysimplified.http.tests
33
import org.junit.jupiter.api.Assertions
44
import org.junit.jupiter.api.Test
55
import org.librarysimplified.http.api.LSHTTPNetworkAccess
6+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_AVAILABLE
7+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_NOT_PERMITTED
8+
import org.librarysimplified.http.api.LSHTTPNetworkAccessReadableType.LSHTTPNetworkAvailability.NETWORK_UNAVAILABLE
69

710
class LSHTTPNetworkAccessTest {
811

@@ -13,7 +16,7 @@ class LSHTTPNetworkAccessTest {
1316
LSHTTPNetworkAccess.setWIFIAvailable(true)
1417
LSHTTPNetworkAccess.setWIFIPermitted(true)
1518

16-
Assertions.assertTrue(LSHTTPNetworkAccess.canDownload())
19+
Assertions.assertEquals(NETWORK_AVAILABLE, LSHTTPNetworkAccess.canDownload())
1720
}
1821

1922
@Test
@@ -23,7 +26,7 @@ class LSHTTPNetworkAccessTest {
2326
LSHTTPNetworkAccess.setWIFIAvailable(true)
2427
LSHTTPNetworkAccess.setWIFIPermitted(true)
2528

26-
Assertions.assertTrue(LSHTTPNetworkAccess.canDownload())
29+
Assertions.assertEquals(NETWORK_AVAILABLE, LSHTTPNetworkAccess.canDownload())
2730
}
2831

2932
@Test
@@ -33,7 +36,7 @@ class LSHTTPNetworkAccessTest {
3336
LSHTTPNetworkAccess.setWIFIAvailable(false)
3437
LSHTTPNetworkAccess.setWIFIPermitted(false)
3538

36-
Assertions.assertTrue(LSHTTPNetworkAccess.canDownload())
39+
Assertions.assertEquals(NETWORK_AVAILABLE, LSHTTPNetworkAccess.canDownload())
3740
}
3841

3942
@Test
@@ -43,7 +46,7 @@ class LSHTTPNetworkAccessTest {
4346
LSHTTPNetworkAccess.setWIFIAvailable(true)
4447
LSHTTPNetworkAccess.setWIFIPermitted(false)
4548

46-
Assertions.assertFalse(LSHTTPNetworkAccess.canDownload())
49+
Assertions.assertEquals(NETWORK_NOT_PERMITTED, LSHTTPNetworkAccess.canDownload())
4750
}
4851

4952
@Test
@@ -53,6 +56,6 @@ class LSHTTPNetworkAccessTest {
5356
LSHTTPNetworkAccess.setWIFIAvailable(false)
5457
LSHTTPNetworkAccess.setWIFIPermitted(true)
5558

56-
Assertions.assertFalse(LSHTTPNetworkAccess.canDownload())
59+
Assertions.assertEquals(NETWORK_UNAVAILABLE, LSHTTPNetworkAccess.canDownload())
5760
}
5861
}

0 commit comments

Comments
 (0)