-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Tiebe/learning_resources
Learning resources
- Loading branch information
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...l/tiebe/otarium/ui/home/elo/children/learningresources/LearningResourcesChildComponent.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,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() | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...n/nl/tiebe/otarium/ui/home/elo/children/learningresources/LearningResourcesChildScreen.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,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) | ||
) | ||
} | ||
|
||
} |
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