Swift package to enable persistence layer over NavigationPath
. Package also provides a few navigation utils. Package consists of six main things
SerializedNavPath
type, which is anObservable
that wraps the built-inNavigationPath
- Read-only
pathBinding
property which providesBinding
to the abovepath
property SerializedNavPath
provides persistence from its initialization stage itself by reading the (saved) serialized data from disk, unless we want to create a new objectinit(filenameWithExtension: String, createNew: Bool = false)
- An internal property
navPathLib
of typeSerializedNavPathLib
is also used, as it contains useful methods listed as following (these methods are only accessible within the package)- func readSerializedData(filenameWithExtension: String? = nil) -> Data? - func writeSerializedData(_ data: Data) -> Bool - func eraseSerializedData() - func log(_ message: String)
- Package provides the following routing utils and properties, which are available outside the package
// methods - func createNew() - func getNavPathForNavigationStack() -> Binding<NavigationPath> - func append(_ route: Route) - func removeLast() // properties - var count: Int - var routes: [Route]
- Package provides the following disk utils, which are available outside the package
- func save() - func erase()
As seen in the SerializedNavPathExampleApp
, the SerializedNavPath
object needs be initialized with the filenameWithExtension
parameter. The initialization stage itself will load the path data from disk, and set the path
by de-serializing (or, decoding) this path data. Almost instantly, this SerializedNavPath
is read by the NavigationStack
, defined as following. Here, the getNavPathForNavigationStack
method simply returns the pathBinding
.
NavigationStack(path: navPath.getNavPathForNavigationStack())
Internal
- readSerializedData: Mutating method of
SerializedNavPathLib
to read serialized data from file, where filename is obtained usingfilenameWithExtension
param, or_filenameWithExtension
property. - writeSerializedData: Method of
SerializedNavPathLib
for the corresponding write operation, that returnstrue
on success. - eraseSerializedData: Another method that removes file using
_filenameWithExtension
property.
Public
- save: Saves
json
encodedNavigationPath
data to disk usingwriteSerializedData
method ofnavPathLib
. - erase: Calls
eraseSerializedData
method ofnavPathLib
. - createNew: Erases the serialized data and assigns a new
NavigationPath
object to thepath
property. - getNavPathForNavigationStack: This method returns the private property
pathBinding
. - append: This method appends a new element to the
NavigationPath
, and saves the path data to disk. - count: Computed property that represents the number of elements in the
NavigationPath
. - removeLast: If
count
is non-zero, this method removes the last element from theNavigationPath
. - routes: Computed property obtained by the transformation of
NavigationPath
data to a collection ofRoute
type, returns an empty collection if there is no data.
Debugging can be turned on by setting the
SerializedNavPath.debug
flag. The example app view sets this debug flag in itsinit
method. Running the app shows two kinds of logs. The file-io logs show when serialized data is written to thejson
file, or when this file is removed. The other logs are for information only, for e.g. when attempt is made to modify thepathBinding
property, the log prints pathBinding is readonly.
In the first example recording, clicking the Login button appends "home" Route
to NavigationPath
, and calls the save method of SerializedNavPath
, which in turn calls navPathLib.writeSerializedData
method, passing it the updated path data. If this step produces error, the log prints could not save path, else it prints data written, followed by the filename with extension.
- macOS v13 and above
- iOS v16 and above
First recording shows navigation b/w views, and write logs (for file MainNavPath.json
). Most importantly, it shows
the navPath.count
and navPath.routes
properties in the view. At the end it shows that pathBinding
is read-only,
and calls navPath.erase
for demo purposes only. Second recording shows the persistence of navigation path b/w app launches โ as a feature of the SerializedNavPath
package.
- The above recordings are from
SerializedNavPathExampleApp
view. - This view initializes a
navPath
of typeSerializedNavPath
using a filename constant. - The
title
property retrieves the first route's path fromnavPath
, or a default title if no route exists. - View
body
is aNavigationStack
with conditional navigation destinations based on the route path. - Each destination (
LoginView
,HomeView
,OtherHomeView
) receives and stores thenavPath
for calling the corresponding methods.