Distinguish IsNewRepoAndNewMirror in UI

This commit is contained in:
Thore Goebel 2024-04-14 16:51:23 +02:00
parent 9d80a5c07c
commit f645f1dd6e
4 changed files with 46 additions and 12 deletions

View File

@ -48,6 +48,7 @@ import org.fdroid.index.v2.FileV2
import org.fdroid.repo.FetchResult.IsExistingMirror
import org.fdroid.repo.FetchResult.IsExistingRepository
import org.fdroid.repo.FetchResult.IsNewMirror
import org.fdroid.repo.FetchResult.IsNewRepoAndNewMirror
import org.fdroid.repo.FetchResult.IsNewRepository
import org.fdroid.repo.Fetching
@ -64,7 +65,10 @@ fun RepoPreviewScreen(paddingValues: PaddingValues, state: Fetching, onAddRepo:
item {
RepoPreviewHeader(state, onAddRepo, localeList)
}
if (state.fetchResult == null || state.fetchResult is IsNewRepository) {
if (state.fetchResult == null
|| state.fetchResult is IsNewRepository
|| state.fetchResult is IsNewRepoAndNewMirror
) {
item {
Row(
verticalAlignment = CenterVertically,
@ -129,12 +133,17 @@ fun RepoPreviewHeader(
}
}
if (state.isMirror) Text(
text = stringResource(R.string.repo_mirror_add_info),
text = when (state.fetchResult) {
is IsNewRepoAndNewMirror -> stringResource(R.string.repo_and_mirror_add_both_info)
is IsNewMirror, IsExistingMirror -> stringResource(R.string.repo_mirror_add_info)
else -> error("Unexpected fetch state: ${state.fetchResult}")
},
style = MaterialTheme.typography.body2,
)
if (state.canAdd) FDroidButton(
text = when (state.fetchResult) {
is IsNewRepository -> stringResource(R.string.repo_add_new_title)
is IsNewRepoAndNewMirror -> stringResource(R.string.repo_add_repo_and_mirror)
is IsNewMirror -> stringResource(R.string.repo_add_mirror)
else -> error("Unexpected fetch state: ${state.fetchResult}")
},
@ -254,6 +263,18 @@ fun RepoPreviewScreenNewMirrorPreview() {
}
}
@Composable
@Preview
fun RepoPreviewScreenNewRepoAndNewMirrorPreview() {
val repo = FDroidApp.createSwapRepo("https://example.org", "foo bar")
FDroidContent {
RepoPreviewScreen(
PaddingValues(0.dp),
Fetching("https://mirror.example.org", repo, emptyList(), IsNewRepoAndNewMirror)
) {}
}
}
@Preview
@Composable
fun RepoPreviewScreenExistingRepoPreview() {

View File

@ -188,7 +188,7 @@ This often occurs with apps installed via Google Play or other sources, if they
<string name="repo_add_new_title">Add repository</string>
<string name="repo_add_add">Add</string>
<string name="repo_add_mirror">Add mirror</string>
<string name="repo_add_new_mirror">Add as new mirror</string>
<string name="repo_add_repo_and_mirror">Add repository and mirror</string>
<string name="links">Links</string>
<string name="versions">Versions</string>
<string name="more">More</string>
@ -225,6 +225,7 @@ This often occurs with apps installed via Google Play or other sources, if they
<string name="repo_share_not_found">Could not find repo address in shared text.</string>
<string name="repo_mirror_exists">This mirror was already added.</string>
<string name="repo_mirror_add_info">The URL you are trying to add is a mirror of an existing repository.</string>
<string name="repo_and_mirror_add_both_info">The URL you are trying to add is a mirror of a new repository. Both the original repository and the mirror will be added.</string>
<string name="bad_fingerprint">Bad fingerprint</string>
<string name="invalid_url">This is not a valid URL.</string>
<string name="malformed_repo_uri">Ignoring malformed repo URI: %s</string>

View File

@ -69,7 +69,11 @@ public class Fetching(
public val isMirror: Boolean = repo != null
&& fetchResult != null
&& (fetchResult is FetchResult.IsNewMirror || fetchResult is FetchResult.IsExistingMirror)
&& (
fetchResult is FetchResult.IsNewMirror
|| fetchResult is FetchResult.IsExistingMirror
|| fetchResult is FetchResult.IsNewRepoAndNewMirror
)
override fun toString(): String {
return "Fetching(fetchUrl=$fetchUrl, repo=${repo?.address}, apps=${apps.size}, " +
@ -98,6 +102,7 @@ public data class AddRepoError(
public sealed class FetchResult {
public data object IsNewRepository : FetchResult()
public data object IsNewRepoAndNewMirror : FetchResult()
public data class IsNewMirror(internal val existingRepoId: Long) : FetchResult()
public data object IsExistingRepository : FetchResult()
@ -239,7 +244,13 @@ internal class RepoAdder(
val existingRepo = repositoryDao.getRepository(cert)
return if (existingRepo == null) {
FetchResult.IsNewRepository
val isUserMirror = url.trimEnd('/') != repo.address.trimEnd('/')
&& repo.mirrors.find { url.trimEnd('/') == it.url.trimEnd('/') } == null
if (isUserMirror) {
FetchResult.IsNewRepoAndNewMirror
} else {
FetchResult.IsNewRepository
}
} else if (existingRepo.address.trimEnd('/') == url) {
FetchResult.IsExistingRepository
} else {
@ -273,7 +284,7 @@ internal class RepoAdder(
val modifiedRepo: Repository = when (fetchResult) {
is FetchResult.IsExistingRepository -> error("Repo exists: $fetchResult")
is FetchResult.IsExistingMirror -> error("Mirror exists: $fetchResult")
is FetchResult.IsNewRepository -> {
is FetchResult.IsNewRepository, is FetchResult.IsNewRepoAndNewMirror -> {
// reset the timestamp of the actual repo,
// so a following repo update will pick this up
val newRepo = NewRepository(
@ -286,11 +297,12 @@ internal class RepoAdder(
password = repo.password,
)
db.runInTransaction<Repository> {
// add the repo
val repoId = repositoryDao.insert(newRepo)
// add user mirror, if URL is not the repo address and not a known mirror
if (state.fetchUrl != repo.address.trimEnd('/') &&
repo.mirrors.find { state.fetchUrl == it.url.trimEnd('/') } == null
) {
// add user mirror
// this can happen if the user was adding a mirror URL, and they originally had neither the repo nor the mirror added
if (fetchResult is FetchResult.IsNewRepoAndNewMirror) {
val userMirrors = listOf(state.fetchUrl)
repositoryDao.updateUserMirrors(repoId, userMirrors)
}

View File

@ -144,7 +144,7 @@ internal class RepoAdderTest {
// repo not in DB
every { repoDao.getRepository(any<String>()) } returns null
expectMinRepoPreview(repoName, FetchResult.IsNewRepository) {
expectMinRepoPreview(repoName, FetchResult.IsNewRepoAndNewMirror) {
repoAdder.fetchRepository(url = url, proxy = null)
}
@ -786,7 +786,7 @@ internal class RepoAdderTest {
// repo not in DB
every { repoDao.getRepository(any<String>()) } returns null
expectMinRepoPreview(repoName, FetchResult.IsNewRepository) {
expectMinRepoPreview(repoName, FetchResult.IsNewRepoAndNewMirror) {
repoAdder.fetchRepository(url = url, proxy = null)
}