it is no longer necessary to go through settings to initialize an external repo

This commit is contained in:
Mohamed Zenadi 2015-05-03 22:49:44 +02:00
parent a21dd84c2b
commit 7fe81e9c27

View file

@ -51,34 +51,6 @@ public class PasswordStore extends AppCompatActivity {
@Override @Override
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
File dir = null;
if (settings.getBoolean("git_external", false)) {
if (settings.getString("git_external_repo", null) != null) {
dir = new File(settings.getString("git_external_repo", null));
}
} else {
dir = new File(getFilesDir() + "/store");
}
// temp for debug
if (dir == null) {
Intent intent = new Intent(this, UserPreference.class);
intent.putExtra("operation", "git_external");
startActivity(intent);
return;
}
// uninitialize the repo if the dir does not exist or is absolutely empty
if (!dir.exists() || !dir.isDirectory() || FileUtils.listFiles(dir, null, false).isEmpty()) {
settings.edit().putBoolean("repository_initialized", false).apply();
}
if (!PasswordRepository.getPasswords(dir).isEmpty()) {
settings.edit().putBoolean("repository_initialized", true).apply();
}
// create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git"));
checkLocalRepository(); checkLocalRepository();
} }
@ -239,6 +211,17 @@ public class PasswordStore extends AppCompatActivity {
} }
public void initializeRepositoryInfo() { public void initializeRepositoryInfo() {
if (settings.getBoolean("git_external", false) && settings.getString("git_external_repo", null) != null) {
File dir = new File(settings.getString("git_external_repo", null));
if (dir.exists() && dir.isDirectory() && !FileUtils.listFiles(dir, null, true).isEmpty() &&
!PasswordRepository.getPasswords(dir).isEmpty()) {
PasswordRepository.closeRepository();
checkLocalRepository();
return; // if not empty, just show me the passwords!
}
}
final String keyId = settings.getString("openpgp_key_ids", ""); final String keyId = settings.getString("openpgp_key_ids", "");
if (keyId != null && keyId.isEmpty()) if (keyId != null && keyId.isEmpty())
@ -263,6 +246,35 @@ public class PasswordStore extends AppCompatActivity {
} }
private void checkLocalRepository() { private void checkLocalRepository() {
File dir = null;
if (settings.getBoolean("git_external", false)) {
if (settings.getString("git_external_repo", null) != null) {
dir = new File(settings.getString("git_external_repo", null));
}
} else {
dir = new File(getFilesDir() + "/store");
}
// temp for debug
if (dir == null) {
Intent intent = new Intent(this, UserPreference.class);
intent.putExtra("operation", "git_external");
startActivity(intent);
return;
}
// uninitialize the repo if the dir does not exist or is absolutely empty
if (!dir.exists() || !dir.isDirectory() || FileUtils.listFiles(dir, null, false).isEmpty()) {
settings.edit().putBoolean("repository_initialized", false).apply();
}
if (!PasswordRepository.getPasswords(dir).isEmpty()) {
settings.edit().putBoolean("repository_initialized", true).apply();
}
// create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git"));
checkLocalRepository(PasswordRepository.getWorkTree()); checkLocalRepository(PasswordRepository.getWorkTree());
} }
@ -446,6 +458,17 @@ public class PasswordStore extends AppCompatActivity {
initializeRepositoryInfo(); initializeRepositoryInfo();
break; break;
case CLONE_REPO_BUTTON: case CLONE_REPO_BUTTON:
// duplicate code
if (settings.getBoolean("git_external", false) && settings.getString("git_external_repo", null) != null) {
File dir = new File(settings.getString("git_external_repo", null));
if (dir.exists() && dir.isDirectory() && !FileUtils.listFiles(dir, null, true).isEmpty() &&
!PasswordRepository.getPasswords(dir).isEmpty()) {
PasswordRepository.closeRepository();
checkLocalRepository();
return; // if not empty, just show me the passwords!
}
}
Intent intent = new Intent(activity, GitActivity.class); Intent intent = new Intent(activity, GitActivity.class);
intent.putExtra("Operation", GitActivity.REQUEST_CLONE); intent.putExtra("Operation", GitActivity.REQUEST_CLONE);
startActivityForResult(intent, GitActivity.REQUEST_CLONE); startActivityForResult(intent, GitActivity.REQUEST_CLONE);
@ -467,6 +490,7 @@ public class PasswordStore extends AppCompatActivity {
intent.putExtra("operation", "git_external"); intent.putExtra("operation", "git_external");
startActivityForResult(intent, operation); startActivityForResult(intent, operation);
} else { } else {
PasswordRepository.closeRepository();
checkLocalRepository(); checkLocalRepository();
} }
} }