https://kotlinlang.org logo
#compose
Title
# compose
i

Ian Warwick

01/02/2020, 9:29 AM
Has there been any mention of dealing with orientation changes and state persistence? I see JetNews uses global variables to store state and wonder if this is the proposed solution or one that JetNews just took (which is feasible of course) or if there is some way to init state and persist it as long as the current activity including config changes like view models do?
z

Zsolt

01/02/2020, 9:37 AM
State persistence is manageable with ambient Bundles hooked into root Activity, though I'm not sure if this is right or wrong. You can check https://github.com/zsoltk/compose-router for an example.
i

Ian Warwick

01/02/2020, 9:41 AM
Ah cool, I was told in another thread that ambients are equivalent to service locator,
am doing something very similar in my playground app though with URI based routing (because where I work we like deep linking!) 😆
z

Zsolt

01/02/2020, 10:29 AM
Yeah, I've seen it. It's possible to add URI matching to the above too. One benefit would be compile-time safety with sealed classes vs strings, and to not allow just any random URI but have some control over what's allowed.
i

Ian Warwick

01/02/2020, 10:51 AM
Yep this is true
Instead of using String,
Uri
would ensure valid URI's though I don't like it, for strong typedness definetly sealed classes are the way to go, something for me to think about thanks! 🙂
1
p

Paul Woitaschek

01/02/2020, 2:19 PM
Afaik uri doesn't validate anything
Copy code
public static Uri parse(String uriString) {
        return new StringUri(uriString);
    }
`
Copy code
private StringUri(String uriString) {
    if (uriString == null) {
        throw new NullPointerException("uriString");
    }

    this.uriString = uriString;
}
So it only validates that you don't pass null
👍 2
i

Ian Warwick

01/02/2020, 2:21 PM
ah interesting I always thought it was validated to be correct though sounds like I was wrong
p

Paul Woitaschek

01/02/2020, 2:22 PM
Of cause you thought so
`
Copy code
/**
 * Creates a Uri which parses the given encoded URI string.
 *
 * @param uriString an RFC 2396-compliant, encoded URI
 * @throws NullPointerException if uriString is null
 * @return Uri for this given uri string
 */
This really implies that it's validating
i

Ian Warwick

01/02/2020, 2:25 PM
cool thanks for clarifying I tried it and yes anything parses fine
Uri.parse("abc")
! 😆
🚓 1
s

shikasd

01/02/2020, 5:05 PM
I am actually not quite sure that it will validate anything logically. E.g. if you want to open gallery in user info screen, you could do
/user/1/gallery
as a route, meaning you have
gallery
in
user
screen. However, you can do
/user/1/not-gallery
, it will pass validation, but will be logically incorrect, as you don't have
not-gallery
in
user
screen.
z

Zsolt

01/03/2020, 12:31 AM
Updated compose-router with a proof-of-concept impl of deep link uri based routing. See repo / bottom of main README for two examples. The mechanism separates the URI and the actual routing in that it doesn't have direct control over what happens. It's first matched / parsed / validated, then mapped to elements of Routing sealed classes.
👍 1