Adding Storage Functionalities

The page provides instructions on how to add storage/vult functionalities to the app.

Storage Directories

The following directories lies inside the xcode project. Here is the xcode screenshot for example:

Vult/Storage Functionalities

Description for the directories and files are provided below:

Describing Code in Directories

Vult/App/VultHome.swift

Describing Code:

  • Line 8 import SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views you need to import SwiftUI.

  • Line 9 import PhotosUI framework for displaying a photo picker or choose photo from directory . Before using it, you have to first import the framework:

  • Line 10 defines a VultHome structure that helps us build the UI of your Vult(Storage functionalities) code.

  • Line 11 Defines a Environment Object vultVM in VultHome structure for sharing data between many views in your app.@EnvironmentObject property wrapper ensures views automatically stay updated when that data changes.

  • Line 14 defines the variable bodyfor holding the main view of VultHome screen.

  • Line 15 utilizes GeometryReader from SwiftUI library imported above to calculate screen position and size information .

  • Line 15 builds app layouts with stack views. Individually, VStack positions them in a vertical line.

  • Line 17 calls AllocationDetailsBlock() function in AllocationDetailsBlock.swift

  • Line 19 calls AllocationActionStack() function in AllocationActionStack.swift

  • Line21 calls FilesTable() function in FilesTable.swift

  • Line 23 implements a NavigationLink to push a view programmatically to navigation controller in this case user opens a file or navigates.

  • Line 25 calls an EmptyView() that doesn’t contain any content.

  • Line 28 adds a layer of padding to NavigationLink view defined at line 23 and 24.

  • Line 30 adds an action to list directories before the view in code snippet appears.

  • Line 31 defines title for the navigationLink which is Vult.

  • Line 32 configures the title display mode for the view to be large.

  • Line 33 defines a background in gray color.

  • Line 34 utilize Sheets in SwiftUI to present views(AllocationDetails) that partly cover the underlying screen.

  • Line 35 utilize fileImporter to presents a system interface for allowing the user to import multiple files and then call uploadDocument when the upload is completed.

  • Line 36 adds a modifier for the view and fires an action(uploading image) when the value changes (uploadImage).

  • Line 37 passes vultVM object as @EnvironmentObject* property wrapper to create and update views automatically.

  • Line 41 defines a Previewprovider type that produces view for the VultHome structure

  • Line 44 creates a variable vm to hold VultViewModel instance and access it methods

  • Line 45 specifies a file schema for upload

  • VultHome instance is passed as a view to be produced with environment object sharing data between VultViewModel Instance.

Vult/Model/Allocations.swift

Describing Code:

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 10 makes use of a type alias allows you to provide a new name(Allocations in this case) for an existing data type (Allocation )into your program. The aliased name can be used instead of the existing type throughout the program.

  • Line 12 defines Allocation structure and the following fields :

    • var id : Allocation ID

    • var name : Allocation name

    • var dataShards : Holding data shards value, effects upload and download speeds

    • var parityShards: Holding parity shards value , effects availability

    • var expirationDate: Holding Allocation Expiry date

    • var used Size: Holds Used space in bytes inAllocation

    • var numofWrites : Holds value for number of times data written to allocation.

    • var numofReads : Holds value for number of times data read from allocations.

    • var totalChallenges : Holds Total Challenges for blobbers(opened+closed+successful+failed)

    • var numOpenChallenges : Holds value for number of open challenges blobber has to complete

    • var numSuccessChallenges : Holds value for number of challenges blobber successfully completed.

    • var numFailedChallenges : Holds value for number of failed challenges by blobbers.

    • var latestClosedChallenge :Holds value for latest completed challenge by blobber.

  • Line 19 to 34 created enumerations and cases for all the fields defined in Allocation Structure. A particular case can be executed depending upon the value of switch statement.

  • Line 36 to 45 defines a addStats function that's been marked as mutating and can change any property within its enclosing value(the values in this case that can be changed are from line 37 to 44)

  • Line 47 defines a mutating addSize function to modify the value of instance variable type allocation size.

  • Line 51 defines a allocationFraction variable which returns the fraction of allocation used.

  • Line 55 defines an allocationPercentage variable which returns allocation usedsize in terms of percentage.

  • Line 59 defines a variable defaultName which returns Allocation name retrieved from instance variable. In case the instance name field is empty return "Allocation" as String.

Vult/Model/File.swift

Describing Code:

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 10 defines directory strucure for files stored as list.

  • Line 14 makes use of a type alias allows you to provide a new name(Files in this case) for an existing data type (File )into your program. The aliased name can be used instead of the existing type throughout the program.

  • Line 16 defines File structure and the following fields :

    • var id : File ID .

    • var name : File name

    • var mimetype : File MIME Type

    • var path: File path

    • var lookuphash: Holding File LookupHash

    • var type : Holding File Type

    • var size : Holding value for File Size

    • var numBlocks : Holding value file in terms of number of blocks .

    • var actualSize : Holding Actual File Size.

    • var actualNumBlocks : Holding value for file in terms of actual file blocks.

    • var encryption : Holds value for file encrpytion Key.

    • var createdAt : Holding value for when file is created

    • var updatedAt :Holding value for when file is updated.

  • Line 36 to 65 created enumerations and cases for all the fields defined in File Structure. A particular case can be executed depending upon the value of particular statement evaluation in the program.

  • Line 67 to 81 creates the following variable for handling various file operations.

    • localThumbnailPath: Returns thumbnail path for the file.

    • localUploadPath: Returns local path for the uploaded file

    • localFilePath : Returns Local path for the file.

    • isDownloaded : Returns whether the downloaded file exists at specified path.

    • Boolean isUploaded : Returns boolean value true when the file is uploaded

    • completedBytes : Returns number of completed bytes for file download.

  • Line 87 define enum and cases for file status.

  • Line 91 defines variable fileSize and functionalities for cases created for file status

    • In case of completed file status return the file size in bytes.

    • In case of file status in progress return number of completed bytes divide by size in percentage.

    • In case of file status in error ,return failed status.

  • Line 93 to 99 defines variable fileDownloadPercent which returns downloaded file progress in percent.

Vult/View/AllocationActionStack.swift

Describing Code :

  • Line 8 import SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views you need to import SwiftUI.

  • Line 9 import PhotosUI framework for displaying a photo picker or choose photo from directory . Before using it, you have to first import the framework

  • Line 11 defines a AllocationAction structure that helps us manage view for the Allocation functionalities in Vult code.

  • Line 12 defines a Environment Object vultVM in pointing to VultViewModel structure for sharing data between views in your app.@EnvironmentObject property wrapper ensures views automatically stay updated when that data changes.

  • Line 13 creates variable that holds value for possible color schemes, corresponding to the light and dark appearances.

  • Line 15 to 32 builds app layouts with stack views. Individually, HStack positions them in a horizontal line.

    • Line 17 to 20 implements a view that displays a Photos picker for choosing assets from the photo library.

    • Line 21 appends the photopicker with a WalletActionBlock.

    • Line 24 to 27 creates another WalletActionBlock named Upload Document which presents a document picker for choosing documents from library when ontap gesture is detected.

  • Line 34 to 40 defines a Previewprovider type that produces view for the AllocationActionStack structure .An AllocationActionStack instance is passed as a view to be produced.

  • Line 43 to 63 defines view for the WalletActionBlock utilized by AllocationActionBlock

  • Line 44 and 45 defines the following variables for WalletActionBlock.

    • A variable named icon for holding photo.

    • A variable named title for naming particular action functionality uploading documents or photos.

  • Line 48 defines a GeometryReader from SwiftUI library imported above to calculate screen position and size information.

  • Line 50 to 54 builds app layouts with stack views. Individually, VStack positions them in a vertical line .

Vult/View/AllocationDetailsBlock.swift

Describing Code :

  • Line 8 import SwiftUI gives access to SwiftUI-specific functionality . If you are writing UI Views you need to import SwiftUI.

  • Line 10 defines a AllocationDetailsBlock structure that defines how allocation details should be displayed in the App.

    • Line 15 to 22 builds app layouts with stack views. Individually, VStack positions them in a vertical line(top to bottom) .HStack, a horizontal stack, which shows views in a left-to-right list.

    • Allocation Name and Expiration Date is listed as a textView.(Line 16 to 21)

    • Then in another Vertical Stack(VStack) a progress view and text is defined describing how much allocation space is used by files (Line 24 to 32)

  • Line 36 to 40 defines padding,background and shadow for Vertically and Horizontally Stacked Views.

  • Line 44 to 44 defines a Previewprovider type that produces view for the AllocationDetailsBlock structure .An AllocationDetailsBlock() instance is passed with VultViewModel instance as an environment object to share data during view production .

Vult/View/FilesTable.swift

Describing Code :

  • Line 8 import SwiftUI gives access to SwiftUI-specific functionality . If you are writing UI Views you need to import SwiftUI.

  • Line 10 defines a FilesTable structure that dictates how files uploaded to dStorage should be displayed in the App.

    • Line 11 defines VultVM variable that holds a VultViewModel object . @EnvironmentObject property wrapper ensure views are shared properly across an entire SwiftUI app.

    • Line 12 defines a Boolean for holding state in case when the file is previewed

    • Line 14 to 33 builds app layouts with stack views. Individually, VStack positions them in a vertical line(top to bottom) with a text view titled(All Files) in bold

    • Line 17 implements a scrollable view for all the files on the Allocation

    • Line 18 to 20 list all the files available in the allocation

    • Line 21 to 27 defines action in case of tap on a listed file or download button.

      Flow: If user downloads the file open the selected file. else if the file is uploaded download the selected or tapped file

  • Line 35 to 46 defines a Previewprovider type that produces view for the FilesTable structure .A FilesTable() instance is passed with VultViewModel instance as an environment object to share data during view production .

  • Line 48 to 95 defines a structure that dictates the layout for the listed files

    • (Line 49) A variable file holding a particular file state.

    • (Line 51) HStack, a horizontal stack, which shows views in a left-to-right list with spacing of 20 pexels.

    • (Line 53 to 56) In a HStack properties for the displayed image are defined(Image has to be resizable with a specified frame and corner radius )

    • (Line 58 to 63) In case no thumbnail found for a particular image the thumbnail image should have the following properties.

      • A resizable thumbnail image of teal color with a specified frame and corner radius

    • (Line 66 to 74 ) A view that displays file name as text and size with display properties

    • (Line 76 to 89)If the Uploaded files are downloaded display the following symbol..

    • Line (92 to 95) defines padding,background and radius for Vertically and Horizontally Stacked Views.

Vult/View/ZCNProgressStyle.swift

Describing Code :

  • Line 11 defines a ZCNProgressStyle structure that dictates the layout for the ZCN progress bar in case of faucet send and recieve.

  • Line 15 defines function makeBody to define how the layout should be for the ZCN progress bar.

  • Line 16 defines a variable that holds ZCN progress in fractions

  • Line 18 to 25 defines a ZStack, a depth-based stack, which shows views in a back-to-front list defines a rounded rectangle inn which ZCN progress bar in fractions should be displayed.

Vult/ViewModel/VultViewModel.swift

Describing Code :

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 9 import Zcncore is required to access Gosdk functions.

  • Line 10 import Combine is required to handle asynchronous events.

  • Line 11 import PhotosUI framework for displaying a photo picker or choose photo from directory . Before using it, you have to first import the framework:

  • Line 13 defines a VultViewModel structure that utilizes the zcncore libraries and provide the following functionalities for Vult UI:

    • Line 14 Inside the structure a variable zboxAllocationHandle is created to handle ZBoxAllocation object.

    • @Published is one of the most useful property wrappers in SwiftUI, allowing us to create observable objects that automatically announce when changes occur.

    • Line 16 defines the variable allocationfor holding a Allocation type

    • Line 17 defines a boolean which holds state for whether AllocationDetails are present.

    • Line 18 defines a boolean which holds state for whether document picker to upload document is active.

    • Line 20 defines variable files for holding an array of files

    • Line 21 defines variable that holds value for the current selected photo in the UI

    • Line 23 defines variable selectedFile to hold value for a selected file in the UI

    • Line 24 defines a boolean variable to hold state of opened files.

    • Line 26 to 36 defines a constructor which initializes a VultViewModel instance and creates/retrieves an allocation on dStorage via gosdk getAllocation function.

  • Line 32 defines getAllocation()function to retrieve allocation details .

    Following Allocation details are retrieved utilizing gosdk zboxAllocation library

    • Allocation ID(Line 40)

    • Allocation Size(Line 41)

    • Allocation Data Shards(Line 42)

    • Allocation Parity Shards)(Line 43)

    • Allocation Name(Line 44)

    • Allocation ExpirationDate(Line 45)

  • Line 51 calls addStats function in Allocation.swift

  • Line 52 assigns all the allocation details to VultViewModel instance allocation variable.

  • Line 63 defines function uploadImage that provides functionality for uploading an image to the UI.

    • utilizes a Swiftui Photos picker view to select media assets .(Line63)

    • Using a new variable newItem to hold selected media asset.(line 66)

    • Fetching the file name from media assets and assigning those values to name variable.

    • At line 68 the data variable holds the selected media asset and uploads it to dStorage using [gosdk uploadFile] function.

    • A catch statment with error is defined at line 71 in case above functionalities does not work as expected .

  • Line 77 defines a function uploadDocument for uploading document via Vult App

    • A Result special type is specified that allows to encapsulate a successful value or some kind of error type in a single piece of data.(Line 77)

    • A url variable holds all the doc data retrieved via Result special type.

    • At line 82 and 83 the data variable holds the selected doc asset and uploads it using uploadFile function.

  • Line 89 defines a function uploadFile taking the following parameters.

    • data : Data --> A single piece of data

    • name: name --> File Name.

    • Line 91 and 92 creates variable localPath and thumbnailPath that holds value for the local file path and file thumbnail path.The paths are retrieved using Utils structure.

    • line 94 to 99 retrieve data from Data block and writes them to localPath and thumbnailPath.

    • Line 104 to 110 utilize gosdk uploadFile function and pass the necessary parameters to upload file to dStorage network.

      • localPath: Provide the local File Path to upload file from.

      • remotePath : Provide a path to upload file to decentralized remote storage.

      • thumbnailpath : Provide thumbnail path of the uploaded file.

      • statusCb : Does all the checks whether file has uploaded successfully.

      • withThumbnail: Retrieves the path of Thumbnail via Utils structure method.

  • Line 112 to 120 defines a function downloadImage that utilize gosdk downloadFile function for downloading file to dStorage

  • Line 122 to 144 defines a function listDir that will list all the directories in the allocation.

  • Line 148 ZboxStatusCallbackProtocol() extends new functionality to the VultVIewModel structure with the following callback functions :

    • (Line 153 to 167) func completed : Displays output when file upload operations are completed successfully

    • (Line 169 to 176) func error : Displays error output when file operations encountered an error.

    • (Line 178 to 197)func inProgress : Displays progress of file upload operations .

    • (Line 203 to 217) func started: Displays whether file operations have started.

Last updated