forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
… initial download state from store
- Loading branch information
Showing
19 changed files
with
455 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...wser/state/src/main/java/mozilla/components/browser/state/reducer/DownloadStateReducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.browser.state.reducer | ||
|
||
import mozilla.components.browser.state.action.DownloadAction | ||
import mozilla.components.browser.state.state.BrowserState | ||
|
||
internal object DownloadStateReducer { | ||
|
||
/** | ||
* [DownloadAction] Reducer function for modifying [BrowserState.queuedDownloads]. | ||
*/ | ||
fun reduce(state: BrowserState, action: DownloadAction): BrowserState { | ||
return when (action) { | ||
is DownloadAction.QueueDownloadAction -> { | ||
state.copy(queuedDownloads = state.queuedDownloads + (action.download.id to action.download)) | ||
} | ||
is DownloadAction.RemoveQueuedDownloadAction -> { | ||
state.copy(queuedDownloads = state.queuedDownloads - action.downloadId) | ||
} | ||
is DownloadAction.RemoveAllQueuedDownloadsAction -> { | ||
state.copy(queuedDownloads = emptyMap()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...browser/state/src/test/java/mozilla/components/browser/state/action/DownloadActionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.browser.state.action | ||
|
||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.content.DownloadState | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.support.test.ext.joinBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class DownloadActionTest { | ||
|
||
@Test | ||
fun `QueueDownloadAction adds download`() { | ||
val store = BrowserStore(BrowserState()) | ||
|
||
val download1 = DownloadState("https://mozilla.org/download1", destinationDirectory = "") | ||
store.dispatch(DownloadAction.QueueDownloadAction(download1)).joinBlocking() | ||
assertEquals(download1, store.state.queuedDownloads[download1.id]) | ||
assertEquals(1, store.state.queuedDownloads.size) | ||
|
||
val download2 = DownloadState("https://mozilla.org/download2", destinationDirectory = "") | ||
store.dispatch(DownloadAction.QueueDownloadAction(download2)).joinBlocking() | ||
assertEquals(download2, store.state.queuedDownloads[download2.id]) | ||
assertEquals(2, store.state.queuedDownloads.size) | ||
} | ||
|
||
@Test | ||
fun `RemoveQueuedDownloadAction removes download`() { | ||
val store = BrowserStore(BrowserState()) | ||
|
||
val download = DownloadState("https://mozilla.org/download1", destinationDirectory = "") | ||
store.dispatch(DownloadAction.QueueDownloadAction(download)).joinBlocking() | ||
assertEquals(download, store.state.queuedDownloads[download.id]) | ||
assertFalse(store.state.queuedDownloads.isEmpty()) | ||
|
||
store.dispatch(DownloadAction.RemoveQueuedDownloadAction(download.id)).joinBlocking() | ||
assertTrue(store.state.queuedDownloads.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `RemoveAllQueuedDownloadsAction removes all downloads`() { | ||
val store = BrowserStore(BrowserState()) | ||
|
||
val download = DownloadState("https://mozilla.org/download1", destinationDirectory = "") | ||
val download2 = DownloadState("https://mozilla.org/download2", destinationDirectory = "") | ||
store.dispatch(DownloadAction.QueueDownloadAction(download)).joinBlocking() | ||
store.dispatch(DownloadAction.QueueDownloadAction(download2)).joinBlocking() | ||
|
||
assertFalse(store.state.queuedDownloads.isEmpty()) | ||
assertEquals(2, store.state.queuedDownloads.size) | ||
|
||
store.dispatch(DownloadAction.RemoveAllQueuedDownloadsAction).joinBlocking() | ||
assertTrue(store.state.queuedDownloads.isEmpty()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...eature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadMiddleware.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.feature.downloads | ||
|
||
import android.app.DownloadManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import mozilla.components.browser.state.action.BrowserAction | ||
import mozilla.components.browser.state.action.DownloadAction | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.lib.state.Middleware | ||
import mozilla.components.lib.state.MiddlewareStore | ||
|
||
/** | ||
* [Middleware] implementation for managing downloads via the provided download service. It's | ||
* purpose is to react to global download state changes (e.g. of [BrowserState.queuedDownloads]) | ||
* and notify the download service, as needed. | ||
*/ | ||
class DownloadMiddleware( | ||
private val applicationContext: Context, | ||
private val downloadServiceClass: Class<*> | ||
) : Middleware<BrowserState, BrowserAction> { | ||
|
||
override fun invoke( | ||
store: MiddlewareStore<BrowserState, BrowserAction>, | ||
next: (BrowserAction) -> Unit, | ||
action: BrowserAction | ||
) { | ||
next(action) | ||
when (action) { | ||
is DownloadAction.QueueDownloadAction -> { | ||
val intent = Intent(applicationContext, downloadServiceClass) | ||
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, action.download.id) | ||
applicationContext.startService(intent) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
...onents/feature/downloads/src/main/java/mozilla/components/feature/downloads/ext/Intent.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.