|
| 1 | +package com.fox2code.mmm.repo; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | + |
| 6 | +import com.fox2code.mmm.MainApplication; |
| 7 | + |
| 8 | +public class CustomRepoManager { |
| 9 | + private static final boolean AUTO_RECOMPILE = true; |
| 10 | + public static final int MAX_CUSTOM_REPOS = 3; |
| 11 | + private final MainApplication mainApplication; |
| 12 | + private final RepoManager repoManager; |
| 13 | + private final String[] customRepos; |
| 14 | + private int customReposCount; |
| 15 | + boolean dirty; |
| 16 | + |
| 17 | + CustomRepoManager(MainApplication mainApplication, RepoManager repoManager) { |
| 18 | + this.mainApplication = mainApplication; |
| 19 | + this.repoManager = repoManager; |
| 20 | + this.customRepos = new String[MAX_CUSTOM_REPOS]; |
| 21 | + this.customReposCount = 0; |
| 22 | + SharedPreferences sharedPreferences = this.getSharedPreferences(); |
| 23 | + int lastFilled = 0; |
| 24 | + for (int i = 0; i < MAX_CUSTOM_REPOS; i++) { |
| 25 | + String repo = sharedPreferences.getString("repo_" + i, ""); |
| 26 | + if (!repo.isEmpty() && !RepoManager.isBuiltInRepo(repo)) { |
| 27 | + lastFilled = i; |
| 28 | + int index = AUTO_RECOMPILE ? |
| 29 | + this.customReposCount : i; |
| 30 | + this.customRepos[index] = repo; |
| 31 | + this.customReposCount++; |
| 32 | + ((CustomRepoData) this.repoManager.addOrGet(repo)) |
| 33 | + .override = "custom_repo_" + index; |
| 34 | + } |
| 35 | + } |
| 36 | + if (AUTO_RECOMPILE && (lastFilled + 1) != this.customReposCount) { |
| 37 | + SharedPreferences.Editor editor = sharedPreferences.edit().clear(); |
| 38 | + for (int i = 0; i < MAX_CUSTOM_REPOS; i++) { |
| 39 | + if (this.customRepos[i] != null) |
| 40 | + editor.putString("repo_" + i, this.customRepos[i]); |
| 41 | + } |
| 42 | + editor.apply(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private SharedPreferences getSharedPreferences() { |
| 47 | + return this.mainApplication.getSharedPreferences( |
| 48 | + "mmm_custom_repos", Context.MODE_PRIVATE); |
| 49 | + } |
| 50 | + |
| 51 | + public CustomRepoData addRepo(String repo) { |
| 52 | + if (RepoManager.isBuiltInRepo(repo)) |
| 53 | + throw new IllegalArgumentException("Can't add built-in repo to custom repos"); |
| 54 | + for (String repoEntry : this.customRepos) { |
| 55 | + if (repo.equals(repoEntry)) |
| 56 | + return (CustomRepoData) this.repoManager.get(repoEntry); |
| 57 | + } |
| 58 | + int i = 0; |
| 59 | + while (customRepos[i] != null) i++; |
| 60 | + customRepos[i] = repo; |
| 61 | + this.getSharedPreferences().edit() |
| 62 | + .putString("repo_" + i, repo).apply(); |
| 63 | + this.dirty = true; |
| 64 | + CustomRepoData customRepoData = (CustomRepoData) |
| 65 | + this.repoManager.addOrGet(repo); |
| 66 | + customRepoData.override = "custom_repo_" + i; |
| 67 | + customRepoData.updateEnabledState(); |
| 68 | + return customRepoData; |
| 69 | + } |
| 70 | + |
| 71 | + public CustomRepoData getRepo(int index) { |
| 72 | + if (index >= MAX_CUSTOM_REPOS) return null; |
| 73 | + String repo = customRepos[index]; |
| 74 | + return repo == null ? null : |
| 75 | + (CustomRepoData) this.repoManager.get(repo); |
| 76 | + } |
| 77 | + |
| 78 | + public void removeRepo(int index) { |
| 79 | + String oldRepo = customRepos[index]; |
| 80 | + if (oldRepo != null) { |
| 81 | + customRepos[index] = null; |
| 82 | + customReposCount--; |
| 83 | + CustomRepoData customRepoData = |
| 84 | + (CustomRepoData) this.repoManager.get(oldRepo); |
| 85 | + if (customRepoData != null) { |
| 86 | + customRepoData.setEnabled(false); |
| 87 | + customRepoData.override = null; |
| 88 | + } |
| 89 | + this.getSharedPreferences().edit() |
| 90 | + .remove("repo_" + index).apply(); |
| 91 | + this.dirty = true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public boolean hasRepo(String repo) { |
| 96 | + for (String repoEntry : this.customRepos) { |
| 97 | + if (repo.equals(repoEntry)) |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + public boolean canAddRepo() { |
| 104 | + return this.customReposCount < MAX_CUSTOM_REPOS; |
| 105 | + } |
| 106 | + |
| 107 | + public boolean canAddRepo(String repo) { |
| 108 | + if (RepoManager.isBuiltInRepo(repo) || |
| 109 | + this.hasRepo(repo) || !this.canAddRepo()) |
| 110 | + return false; |
| 111 | + return repo.startsWith("https://") && |
| 112 | + repo.indexOf('/', 9) != -1; |
| 113 | + } |
| 114 | + |
| 115 | + public boolean needUpdate() { |
| 116 | + boolean needUpdate = this.dirty; |
| 117 | + if (needUpdate) this.dirty = false; |
| 118 | + return needUpdate; |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments