-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compose state restoration and web history mode
1 parent
3adac4a
commit af8c513
Showing
22 changed files
with
899 additions
and
37 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
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
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
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
109 changes: 104 additions & 5 deletions
109
integration/compose/common/src/dev/programadorthi/routing/compose/ComposeRoutingExt.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 |
---|---|---|
@@ -1,13 +1,112 @@ | ||
package dev.programadorthi.routing.compose | ||
|
||
import dev.programadorthi.routing.compose.history.ComposeHistoryNeglectAttributeKey | ||
import dev.programadorthi.routing.core.RouteMethod | ||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.call | ||
import io.ktor.http.Parameters | ||
import io.ktor.util.Attributes | ||
|
||
public fun Routing.canPop(): Boolean = callStack.size > 1 | ||
internal expect fun Routing.popOnPlatform( | ||
result: Any? = null, | ||
fallback: () -> Unit, | ||
) | ||
|
||
public fun Routing.pop(result: Any? = null) { | ||
if (!canPop()) return | ||
popOnPlatform(result = result) { | ||
if (callStack.size < 2) return@popOnPlatform | ||
|
||
poppedCall = callStack.removeLastOrNull() | ||
poppedCall?.popped = true | ||
poppedCall?.popResult = result | ||
poppedCall = callStack.removeLastOrNull() | ||
poppedCall?.popped = true | ||
poppedCall?.popResult = result | ||
|
||
val last = callStack.lastOrNull() ?: return@popOnPlatform | ||
if (last.content == null) { | ||
execute(last) | ||
} | ||
} | ||
} | ||
|
||
public fun Routing.push( | ||
path: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
uri = path, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.Push, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
public fun Routing.pushNamed( | ||
name: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
name = name, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.Push, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
public fun Routing.replace( | ||
path: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
uri = path, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.Replace, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
public fun Routing.replaceNamed( | ||
name: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
name = name, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.Replace, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
public fun Routing.replaceAll( | ||
path: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
uri = path, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.ReplaceAll, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
public fun Routing.replaceAllNamed( | ||
name: String, | ||
parameters: Parameters = Parameters.Empty, | ||
neglect: Boolean = false, | ||
) { | ||
call( | ||
name = name, | ||
parameters = parameters, | ||
routeMethod = RouteMethod.ReplaceAll, | ||
attributes = neglect.toAttributes(), | ||
) | ||
} | ||
|
||
private fun Boolean.toAttributes(): Attributes { | ||
val attributes = Attributes() | ||
attributes.put(ComposeHistoryNeglectAttributeKey, this) | ||
return attributes | ||
} |
40 changes: 40 additions & 0 deletions
40
.../compose/common/src/dev/programadorthi/routing/compose/history/ComposeHistoryAttribute.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 @@ | ||
package dev.programadorthi.routing.compose.history | ||
|
||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application | ||
import dev.programadorthi.routing.core.application.Application | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
import io.ktor.util.AttributeKey | ||
|
||
internal val ComposeHistoryModeAttributeKey: AttributeKey<ComposeHistoryMode> = | ||
AttributeKey("ComposeHistoryModeAttributeKey") | ||
|
||
internal val ComposeHistoryNeglectAttributeKey: AttributeKey<Boolean> = | ||
AttributeKey("ComposeHistoryNeglectAttributeKey") | ||
|
||
internal val ComposeHistoryRestoredCallAttributeKey: AttributeKey<Boolean> = | ||
AttributeKey("ComposeHistoryRestoredCallAttributeKey") | ||
|
||
internal var Application.historyMode: ComposeHistoryMode | ||
get() = attributes[ComposeHistoryModeAttributeKey] | ||
set(value) { | ||
attributes.put(ComposeHistoryModeAttributeKey, value) | ||
} | ||
|
||
internal var Routing.historyMode: ComposeHistoryMode | ||
get() = application.historyMode | ||
set(value) { | ||
application.historyMode = value | ||
} | ||
|
||
internal var ApplicationCall.neglect: Boolean | ||
get() = attributes.getOrNull(ComposeHistoryNeglectAttributeKey) ?: false | ||
set(value) { | ||
attributes.put(ComposeHistoryNeglectAttributeKey, value) | ||
} | ||
|
||
internal var ApplicationCall.restored: Boolean | ||
get() = attributes.getOrNull(ComposeHistoryRestoredCallAttributeKey) ?: false | ||
set(value) { | ||
attributes.put(ComposeHistoryRestoredCallAttributeKey, value) | ||
} |
16 changes: 16 additions & 0 deletions
16
...ration/compose/common/src/dev/programadorthi/routing/compose/history/ComposeHistoryExt.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,16 @@ | ||
package dev.programadorthi.routing.compose.history | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
|
||
internal expect suspend fun ApplicationCall.platformPush(routing: Routing) | ||
|
||
internal expect suspend fun ApplicationCall.platformReplace(routing: Routing) | ||
|
||
internal expect suspend fun ApplicationCall.platformReplaceAll(routing: Routing) | ||
|
||
internal expect fun ApplicationCall.shouldNeglect(): Boolean | ||
|
||
@Composable | ||
internal expect fun Routing.restoreState() |
Oops, something went wrong.