Skip to content

Commit

Permalink
feat: シンプルな構造体を扱うようにし、RAMと帯域の使用率を少なくするようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Jul 3, 2023
1 parent e73465d commit e13102b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.pantasystem.milktea.api.misskey
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Response
import retrofit2.Retrofit
Expand All @@ -20,7 +20,7 @@ interface InstanceInfosAPI {
@Query("name") name: String? = null,
@Query("limit") limit: Int? = null,
@Query("offset") offset: Int? = null,
): Response<List<InstanceInfosResponse.InstanceInfo>>
): Response<List<SimpleInstanceInfo>>
}

@Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.pantasystem.milktea.api.misskey.infos

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SimpleInstanceInfo(
@SerialName("url") val url: String,
@SerialName("name") val name: String,
@SerialName("description") val description: String? = null,
@SerialName("iconUrl") val iconUrl: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -135,7 +134,6 @@ fun AuthFormScreen(
.fillMaxWidth()
.weight(1f),
uiState = uiState,
instanceDomain = instanceDomain,
onInputInstanceDomain = onInputInstanceDomain,
onBottomReached = onBottomReached
)
Expand Down Expand Up @@ -259,7 +257,6 @@ private fun AgreementLayout(
private fun FilteredInstances(
modifier: Modifier = Modifier,
uiState: AuthUiState,
instanceDomain: String,
onInputInstanceDomain: (String) -> Unit,
onBottomReached: () -> Unit,
) {
Expand All @@ -273,11 +270,7 @@ private fun FilteredInstances(
}
}
}
val instances = remember(uiState.misskeyInstanceInfosResponse, uiState.formState) {
uiState.misskeyInstanceInfosResponse.filter {
it.meta.uri.contains(instanceDomain) || it.name.contains(instanceDomain)
}
}
val instances = uiState.misskeyInstanceInfosResponse
LazyColumn(
modifier,
state = listState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MisskeyInstanceInfoCard(
modifier: Modifier = Modifier,
info: InstanceInfosResponse.InstanceInfo,
info: SimpleInstanceInfo,
selected: Boolean,
onClick: () -> Unit,
) {
Expand All @@ -44,7 +44,7 @@ fun MisskeyInstanceInfoCard(
verticalAlignment = Alignment.CenterVertically
) {
Image(
rememberAsyncImagePainter(info.meta.iconUrl),
rememberAsyncImagePainter(info.iconUrl),
contentDescription = null,
modifier = Modifier
.size(32.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.flow.distinctUntilChanged
import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo
import net.pantasystem.milktea.auth.viewmodel.SignUpUiState
import net.pantasystem.milktea.common.ResultState
import net.pantasystem.milktea.common.StateContent
Expand All @@ -42,7 +42,7 @@ fun SignUpScreen(
uiState: SignUpUiState,
onInputKeyword: (String) -> Unit,
onNextButtonClicked: (InstanceInfoType) -> Unit,
onSelected: (InstanceInfosResponse.InstanceInfo) -> Unit,
onSelected: (SimpleInstanceInfo) -> Unit,
onNavigateUp: () -> Unit,
onBottomReached: () -> Unit,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.pantasystem.milktea.api.misskey.InstanceInfoAPIBuilder
import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo
import net.pantasystem.milktea.common.Logger
import net.pantasystem.milktea.common.PageableState
import net.pantasystem.milktea.common.paginator.EntityConverter
Expand All @@ -24,36 +24,36 @@ class InstanceSuggestionsPagingModel @Inject constructor(
private val instancesInfoAPIBuilder: InstanceInfoAPIBuilder,
private val loggerFactory: Logger.Factory,
) : StateLocker,
PaginationState<InstanceInfosResponse.InstanceInfo>,
PreviousLoader<InstanceInfosResponse.InstanceInfo>,
EntityConverter<InstanceInfosResponse.InstanceInfo, InstanceInfosResponse.InstanceInfo> {
PaginationState<SimpleInstanceInfo>,
PreviousLoader<SimpleInstanceInfo>,
EntityConverter<SimpleInstanceInfo, SimpleInstanceInfo> {

private val logger by lazy {
loggerFactory.create("InstanceSuggestionsPagingModel")
}
private var _offset = 0
private var _name: String = ""
private val _state =
MutableStateFlow<PageableState<List<InstanceInfosResponse.InstanceInfo>>>(PageableState.Loading.Init())
MutableStateFlow<PageableState<List<SimpleInstanceInfo>>>(PageableState.Loading.Init())

private var _job: Job? = null

override suspend fun convertAll(list: List<InstanceInfosResponse.InstanceInfo>): List<InstanceInfosResponse.InstanceInfo> {
override suspend fun convertAll(list: List<SimpleInstanceInfo>): List<SimpleInstanceInfo> {
return list
}

override val state: Flow<PageableState<List<InstanceInfosResponse.InstanceInfo>>>
override val state: Flow<PageableState<List<SimpleInstanceInfo>>>
get() = _state

override fun getState(): PageableState<List<InstanceInfosResponse.InstanceInfo>> {
override fun getState(): PageableState<List<SimpleInstanceInfo>> {
return _state.value
}

override fun setState(state: PageableState<List<InstanceInfosResponse.InstanceInfo>>) {
override fun setState(state: PageableState<List<SimpleInstanceInfo>>) {
_state.value = state
}

override suspend fun loadPrevious(): Result<List<InstanceInfosResponse.InstanceInfo>> =
override suspend fun loadPrevious(): Result<List<SimpleInstanceInfo>> =
runCancellableCatching {
instancesInfoAPIBuilder.build().getInstances(
offset = _offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo
import net.pantasystem.milktea.auth.suggestions.InstanceSuggestionsPagingModel
import net.pantasystem.milktea.common.*
import net.pantasystem.milktea.model.instance.InstanceInfoService
Expand All @@ -16,7 +16,6 @@ import javax.inject.Inject
class SignUpViewModel @Inject constructor(
private val instanceInfoService: InstanceInfoService,
private val instancePagingModel: InstanceSuggestionsPagingModel,
loggerFactory: Logger.Factory,
) : ViewModel() {

private var _keyword = MutableStateFlow("")
Expand Down Expand Up @@ -98,7 +97,7 @@ class SignUpViewModel @Inject constructor(
}
}

fun onSelected(instancesInfosResponse: InstanceInfosResponse.InstanceInfo) {
fun onSelected(instancesInfosResponse: SimpleInstanceInfo) {
_selectedInstanceUrl.value = instancesInfosResponse.url
}

Expand All @@ -111,7 +110,7 @@ data class SignUpUiState(
val keyword: String = "",
val selectedUrl: String? = "misskey.io",
val instanceInfo: ResultState<InstanceInfoType> = ResultState.Loading(StateContent.NotExist()),
val instancesInfosResponse: List<InstanceInfosResponse.InstanceInfo> = emptyList(),
val instancesInfosResponse: List<SimpleInstanceInfo> = emptyList(),
) {

val filteredInfos = instancesInfosResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.pantasystem.milktea.auth.viewmodel.app

import net.pantasystem.milktea.api.misskey.infos.InstanceInfosResponse
import net.pantasystem.milktea.api.misskey.infos.SimpleInstanceInfo
import net.pantasystem.milktea.common.ResultState
import net.pantasystem.milktea.common.StateContent
import net.pantasystem.milktea.common.runCancellableCatching
Expand Down Expand Up @@ -79,7 +79,7 @@ data class AuthUiState(
StateContent.NotExist()
),
val clientId: String = "",
val misskeyInstanceInfosResponse: List<InstanceInfosResponse.InstanceInfo>,
val misskeyInstanceInfosResponse: List<SimpleInstanceInfo>,
) {
val isProgress by lazy {
metaState is ResultState.Loading || waiting4ApproveState is ResultState.Loading
Expand Down

0 comments on commit e13102b

Please sign in to comment.