Skip to content

Commit

Permalink
Merge pull request #52 from Tiebe/learning_resources
Browse files Browse the repository at this point in the history
Learning resources
  • Loading branch information
Tiebe authored Apr 29, 2023
2 parents 5f74a77 + 84b8a16 commit 8e639e4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
package nl.tiebe.otarium.ui.home.elo.children.learningresources

import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.value.MutableValue
import com.arkivanov.decompose.value.Value
import dev.tiebe.magisterapi.api.learningresource.LearningResourceFlow
import dev.tiebe.magisterapi.response.learningresource.LearningResource
import io.ktor.http.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import nl.tiebe.otarium.Data
import nl.tiebe.otarium.ui.home.elo.ELOChildComponent
import nl.tiebe.otarium.ui.root.componentCoroutineScope
import nl.tiebe.otarium.utils.openUrl

interface LearningResourcesChildComponent : ELOChildComponent {
val learningResources: Value<List<LearningResource>>
val isRefreshing: Value<Boolean>

val scope: CoroutineScope

fun refreshLearningResources()

fun openLearningResource(learningResource: LearningResource) {
scope.launch {
val url = LearningResourceFlow.getLearningResourceUrl(
Url(Data.selectedAccount.tenantUrl),
Data.selectedAccount.tokens.accessToken,
learningResource.links.first { it.rel == "content" }.href
)

openUrl(url ?: return@launch)
}
}

}

class DefaultLearningResourcesChildComponent(componentContext: ComponentContext) : LearningResourcesChildComponent, ComponentContext by componentContext {
override val learningResources: MutableValue<List<LearningResource>> = MutableValue(emptyList())
override val isRefreshing: MutableValue<Boolean> = MutableValue(false)

override val scope = componentCoroutineScope()

override fun refreshLearningResources() {
scope.launch {
isRefreshing.value = true
learningResources.value = LearningResourceFlow.getLearningResources(Url(Data.selectedAccount.tenantUrl), Data.selectedAccount.tokens.accessToken, Data.selectedAccount.accountId)
isRefreshing.value = false
}
}

init {
refreshLearningResources()
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
package nl.tiebe.otarium.ui.home.elo.children.learningresources

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState

@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
internal fun LearningResourcesChildScreen(component: LearningResourcesChildComponent) {
val learningResources = component.learningResources.subscribeAsState().value
val pullRefreshState = rememberPullRefreshState(component.isRefreshing.subscribeAsState().value, component::refreshLearningResources)

Box(modifier = Modifier.fillMaxSize().pullRefresh(pullRefreshState)) {
val scrollState = rememberScrollState()

Column(Modifier.verticalScroll(scrollState)) {
learningResources.forEach {
ListItem(
headlineText = { Text(it.title) },
modifier = Modifier.clickable { component.openLearningResource(it) }
)

Divider()
}
}

PullRefreshIndicator(
state = pullRefreshState,
refreshing = component.isRefreshing.subscribeAsState().value,
modifier = Modifier.align(Alignment.TopCenter)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nl.tiebe.otarium.ui.home.elo.children.studyguides.folder
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.value.MutableValue
import com.arkivanov.decompose.value.Value
import dev.tiebe.magisterapi.api.StudyGuideFlow
import dev.tiebe.magisterapi.api.studyguide.StudyGuideFlow
import dev.tiebe.magisterapi.response.studyguide.Resource
import dev.tiebe.magisterapi.response.studyguide.StudyGuideContent
import dev.tiebe.magisterapi.response.studyguide.StudyGuideContentItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nl.tiebe.otarium.ui.home.elo.children.studyguides.listscreen
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.value.MutableValue
import com.arkivanov.decompose.value.Value
import dev.tiebe.magisterapi.api.StudyGuideFlow
import dev.tiebe.magisterapi.api.studyguide.StudyGuideFlow
import dev.tiebe.magisterapi.response.studyguide.StudyGuide
import io.ktor.http.*
import kotlinx.coroutines.launch
Expand Down

0 comments on commit 8e639e4

Please sign in to comment.