-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for download % stream showing an image
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Description | ||
|
||
A download request fetch from a stream to a file with the percentage. | ||
|
||
# How to run | ||
|
||
- Before running Clojure flutter, this demo needs to install the flutter dependencies with: | ||
|
||
```bash | ||
clj -M:cljd init | ||
``` | ||
|
||
- And: | ||
|
||
```bash | ||
flutter pub add path_provider | ||
flutter pub add http | ||
``` | ||
|
||
and after these two processes, you can do | ||
|
||
```bash | ||
clj -M:cljd flutter | ||
``` | ||
|
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,6 @@ | ||
{:paths ["src"] | ||
:deps {org.clojure/clojure {:mvn/version "1.10.1"} | ||
tensegritics/clojuredart {:local/root "../../"}} | ||
:aliases {:cljd {:main-opts ["-m" "cljd.build"]}} | ||
:cljd/opts {:main sample.stream-download-percentage | ||
:kind :flutter}} |
164 changes: 164 additions & 0 deletions
164
samples/stream_download_percentage/src/sample/stream_download_percentage.cljd
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,164 @@ | ||
(ns sample.stream-download-percentage | ||
"Example for using drawer with named routes navigation." | ||
(:require ["package:flutter/material.dart" :as m] | ||
["package:path_provider/path_provider.dart" :as path-provider] | ||
["dart:core" :as dc] | ||
["dart:io" :as dio] | ||
["package:http/http.dart" :as http] | ||
["package:validators/validators.dart" :as validators] | ||
[cljd.flutter :as f] | ||
[clojure.string :as str])) | ||
|
||
(defn calculate-percentage | ||
[{::keys [total | ||
completion]}] | ||
(if (= 0 total) | ||
0 | ||
(-> (- total | ||
completion) | ||
(/ total) | ||
(* 100) | ||
(- 100) | ||
(* -1)))) | ||
|
||
|
||
(defn delete-local-file! | ||
[file-name] | ||
(let [old-file (-> file-name | ||
dio/File)] | ||
(when (await (.exists old-file)) | ||
(-> old-file | ||
.delete)))) | ||
|
||
|
||
(defn parse-uri | ||
[tc] | ||
(let [form-text (-> tc | ||
.-text)] | ||
(when (validators/isURL form-text) | ||
(-> form-text | ||
Uri/parse)))) | ||
|
||
|
||
(defn start-button-handler | ||
[{::keys [file-name | ||
request-uri | ||
*total | ||
*completion | ||
file-image-atom]}] | ||
(delete-local-file! file-name) | ||
(let [streamed-response (some->> request-uri | ||
(http/Request "get") | ||
.send | ||
await) | ||
local-file-open (dio/File file-name) | ||
opened-file (.openWrite local-file-open) | ||
_total-bytes-add (swap! *total | ||
#(+ % (.-contentLength streamed-response)))] | ||
(some-> streamed-response | ||
.-stream | ||
(.listen (fn [event] | ||
(swap! *completion #(+ % (.-length event))) | ||
(.add opened-file event)) | ||
.onDone (fn [] | ||
(reset! file-image-atom local-file-open)))))) | ||
|
||
(defn show-image-ui | ||
[{::keys [file-image]}] | ||
(f/widget | ||
:watch [file-exists? (some-> file-image | ||
.exists)] | ||
m/Center | ||
(if file-exists? | ||
(-> file-image | ||
.readAsBytesSync | ||
(m/Image.memory | ||
.errorBuilder (fn [_ _ _] | ||
(m/Text "Try to download a valid image file")))) | ||
(m/Text "Add a link to download an image to show here")))) | ||
|
||
|
||
(defn show-image-download-button-ui | ||
[{::keys [file-name | ||
path-str]}] | ||
(f/widget | ||
:managed [tc (m/TextEditingController)] | ||
:let [{:flds [text]} tc] | ||
:watch [file-image (atom (dio/File. file-name)) :as file-image-atom] | ||
m/Column | ||
.children | ||
[(m/Spacer) | ||
(show-image-ui {::file-image file-image}) | ||
(m/Spacer) | ||
(f/widget | ||
(m/TextField | ||
.decoration (m/InputDecoration | ||
.border (m/OutlineInputBorder) | ||
.hintText "Add the link to file download") | ||
.controller tc)) | ||
(f/widget | ||
(m/Row .mainAxisAlignment m/MainAxisAlignment.center) | ||
.children | ||
[(m/Spacer) | ||
(f/widget | ||
:let [*completion (atom 0) | ||
*total (atom 0)] | ||
:watch [completion *completion | ||
total *total] | ||
(m/Flexible | ||
.flex 3) | ||
(m/Row | ||
.mainAxisAlignment m/MainAxisAlignment.spaceBetween) | ||
.children [(m/ElevatedButton | ||
.style (m/ButtonStyle | ||
.backgroundColor (m/MaterialStatePropertyAll | ||
m/Colors.red)) | ||
.onPressed (fn [] | ||
(start-button-handler {::file-name file-name | ||
::request-uri (parse-uri tc) | ||
::*total *total | ||
::*completion *completion | ||
::file-image-atom file-image-atom})) | ||
.child (m/Text "Start")) | ||
(m/Spacer) | ||
(m/Card | ||
.child (-> {::total total | ||
::completion completion} | ||
calculate-percentage | ||
(.toStringAsFixed 2) | ||
(str "%") | ||
m/Text)) | ||
(m/Spacer)])]) | ||
(m/Text "The file will be downloaded in this path below:") | ||
(f/widget | ||
(m/Card | ||
.color m/Colors.green | ||
.child | ||
(m/Text path-str))) | ||
(m/Spacer)])) | ||
|
||
|
||
(def initialization-widget | ||
(f/widget | ||
:watch [path (path-provider/getApplicationDocumentsDirectory)] | ||
(if path | ||
(show-image-download-button-ui {::path-str (-> path | ||
str | ||
(str/split #"Directory: ") | ||
second) | ||
::file-name (-> ^dio/Directory path | ||
.-path | ||
(str "/example.jpg"))}) | ||
(m/Text "Loading...")))) | ||
|
||
|
||
(defn main [] | ||
(-> (m/MaterialApp | ||
.title "My first material app" | ||
.theme (m/ThemeData | ||
.useMaterial3 true) | ||
.initialRoute "/" | ||
.home (m/Scaffold | ||
.body initialization-widget)) | ||
f/widget | ||
f/run)) |