Skip to content

Commit

Permalink
add: Base navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nojipiz committed Jul 31, 2023
1 parent 764a34f commit 6b2e96a
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/scala/jobs/HeavyJob.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jobs

import zio._
import scala.concurrent.Future

object HeavyJob{

import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
def run( update: (Int) => Unit ): Future[Unit] = Future {
Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(HeavyJob.runTask(update = update)).getOrThrowFiberFailure()
}
}

private def runTask( update: (Int) => Unit ) = for{
_ <- ZIO.logInfo("Hello from ZIO!")
data <- Random.nextInt
} yield (update(data))
}
29 changes: 29 additions & 0 deletions src/main/scala/main/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import scala.scalajs.js
import scala.scalajs.LinkingInfo
import scala.scalajs.js.annotation.{JSExportTopLevel, JSImport}

import zio._
import slinky.native.AppRegistry
import slinky.hot
import java.time._
import navigation.AppNavigator
import slinky.core.annotations.react
import slinky.core.StatelessComponent
import slinky.core.facade.ReactElement

object Main {
if (LinkingInfo.developmentMode) {
hot.initialize()
}

@react class App extends StatelessComponent{
type Props = Unit
def render(): ReactElement = AppNavigator()
}

@JSExportTopLevel("app")
val app = App.componentConstructor
}

66 changes: 66 additions & 0 deletions src/main/scala/navigation/AppNavigator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package navigation

import slinky.core._
import slinky.core.facade.ReactElement
import slinky.core.facade.Hooks._
import slinky.core.annotations.react
import slinky.native._

import scala.scalajs.js
import scala.scalajs.js.Dynamic.literal
import typings.reactRouterNative.components.{Route, Link, NativeRouter, Routes}
import screens.ZioScreen
import screens.common._

@react object AppNavigator {
type Props = Unit
case class State(val amountTimesZIO: Int)

val component = FunctionalComponent[Props]{ props =>
val (state, updateState) = useState(State(amountTimesZIO = 0))

val updateTimesZIO = (number:Int) => {
updateState(state.copy(amountTimesZIO = number))
}

NativeRouter(
View(style = literal(
marginTop = 25,
padding = 10,
flex = 1,
))(
View(
style = literal(
flexDirection = "row",
justifyContent = "space-around",
)
)(
NavigationButton(
title = "ZIO",
route = "/zio",
notifications = Some(state.amountTimesZIO),
),
NavigationButton(title = "Home", route = "/"),
NavigationButton(title = "Requests", route = "/requests")
),
Routes(
Route.IndexRouteProps()
.path("/")
.element(Text("Home")),
Route.PathRouteProps()
.path("/zio")
.element(
ZioScreen(
times = state.amountTimesZIO,
updateTimes = Some(updateTimesZIO)
)
),
Route.PathRouteProps()
.path("/requests")
.element(Text("Request"))
)
)
)
}
}

41 changes: 41 additions & 0 deletions src/main/scala/navigation/NavigationButton.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package navigation

import slinky.core.annotations.react
import slinky.native._
import slinky.core._
import scala.scalajs.js.Dynamic.literal
import scala.scalajs.js.undefined
import slinky.core.facade.ReactElement
import typings.reactRouterNative.components._
import typings.reactRouterNative.mod.useNavigate

@react object NavigationButton {
val touchableStyle = literal(
color = "black",
backgroundColor = "white",
)
val textStyle = literal(
fontSize = 18,
fontWeight ="bold",
)

case class Props(
val title: String,
val route: String,
val notifications: Option[Int] = None,
)

val component = FunctionalComponent[Props] { props =>
val navigation = useNavigate()

View()(
TouchableOpacity(
onPress = () => {
navigation(props.route)
},
style = touchableStyle)(
Text(style = textStyle)(props.title)
)
)
}
}
54 changes: 54 additions & 0 deletions src/main/scala/screens/ZioScreen.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package screens

import slinky.core.annotations.react
import slinky.native._
import slinky.core._
import scala.scalajs.js.Dynamic.literal
import scala.scalajs.js.undefined
import scala.scalajs.js.timers._
import slinky.core.facade.ReactElement
import typings.reactRouterNative.components._
import _root_.screens.common.Styles
import slinky.core.facade.Hooks._
import jobs.HeavyJob

@react object ZioScreen{

case class Props(
val times:Int,
val updateTimes: Option[(Int) => Unit]
)

def runTask(updateTimes:(Int) => Unit) = setTimeout(1){
HeavyJob.run( update = updateTimes )
}

val component = FunctionalComponent[Props] { props =>
val (isLoading, setIsLoading) = useState(false)

View(style = Styles.centerContent)(
Text(style = literal(
fontSize = 15,
fontWeight ="bold"
)
)(s"ZIO Status"),

if(isLoading){
Text("Loading ...")
}
else{
Text(s"Random number: ${props.times}")
},

Button(
onPress = () => {
setIsLoading(true)
runTask(props.updateTimes.get)
setIsLoading(false)
},
title = "Run!"
)
)
}
}

11 changes: 11 additions & 0 deletions src/main/scala/screens/common/Styles.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package screens.common

import scala.scalajs.js

object Styles{
val centerContent = js.Dynamic.literal(
flex = 1,
justifyContent = "center",
alignItems = "center"
)
}

0 comments on commit 6b2e96a

Please sign in to comment.