1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Minor refactoring.

This commit is contained in:
Felix Ableitner 2014-06-22 22:52:32 +02:00
parent dff9156d77
commit d0e7f57812
7 changed files with 24 additions and 32 deletions

View file

@ -28,7 +28,6 @@ import android.widget.ListView;
import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
/**

View file

@ -6,9 +6,8 @@ import android.widget.AdapterView;
import android.widget.ListView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.NodeAdapter;
import com.nutomic.syncthingandroid.util.NodesAdapter;
import java.util.Timer;
import java.util.TimerTask;
@ -19,7 +18,7 @@ import java.util.TimerTask;
public class NodesFragment extends LoadingListFragment implements
SyncthingService.OnApiAvailableListener, ListView.OnItemClickListener {
private NodeAdapter mAdapter;
private NodesAdapter mAdapter;
private Timer mTimer;
@ -27,7 +26,7 @@ public class NodesFragment extends LoadingListFragment implements
@Override
public void onInitAdapter(MainActivity activity) {
mAdapter = new NodeAdapter(activity);
mAdapter = new NodesAdapter(activity);
mAdapter.add(activity.getApi().getNodes());
setListAdapter(mAdapter, R.string.nodes_list_empty);
mInitialized = true;

View file

@ -110,7 +110,7 @@ public class RepoSettingsActivity extends PreferenceActivity
else if (getIntent().getAction().equals(ACTION_EDIT)) {
setTitle(R.string.edit_repo);
mRepositoryId.setEnabled(false);
List<RestApi.Repository> repos = mSyncthingService.getApi().getRepositories();
List<RestApi.Repository> repos = mSyncthingService.getApi().getRepos();
for (int i = 0; i < repos.size(); i++) {
if (repos.get(i).ID.equals(getIntent().getStringExtra(KEY_REPO_ID))) {
mRepo = repos.get(i);
@ -174,7 +174,7 @@ public class RepoSettingsActivity extends PreferenceActivity
Toast.makeText(this, R.string.repo_path_required, Toast.LENGTH_LONG).show();
return true;
}
mSyncthingService.getApi().editRepository(mRepo, true);
mSyncthingService.getApi().editRepo(mRepo, true);
finish();
return true;
}
@ -258,7 +258,7 @@ public class RepoSettingsActivity extends PreferenceActivity
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSyncthingService.getApi().deleteRepository(mRepo);
mSyncthingService.getApi().deleteRepo(mRepo);
finish();
}
})
@ -271,7 +271,7 @@ public class RepoSettingsActivity extends PreferenceActivity
private void repositoryUpdated() {
if (getIntent().getAction().equals(ACTION_EDIT)) {
mSyncthingService.getApi().editRepository(mRepo, false);
mSyncthingService.getApi().editRepo(mRepo, false);
}
}

View file

@ -5,7 +5,6 @@ import android.view.View;
import android.widget.AdapterView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.ReposAdapter;
@ -27,7 +26,7 @@ public class ReposFragment extends LoadingListFragment implements
@Override
public void onInitAdapter(MainActivity activity) {
mAdapter = new ReposAdapter(activity);
mAdapter.add(activity.getApi().getRepositories());
mAdapter.add(activity.getApi().getRepos());
setListAdapter(mAdapter, R.string.repositories_list_empty);
mInitialized = true;
}
@ -69,4 +68,5 @@ public class ReposFragment extends LoadingListFragment implements
intent.putExtra(RepoSettingsActivity.KEY_REPO_ID, mAdapter.getItem(i).ID);
startActivity(intent);
}
}

View file

@ -14,11 +14,9 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@ -386,7 +384,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Returns a list of all existing repositores.
*/
public List<Repository> getRepositories() {
public List<Repository> getRepos() {
if (mConfig == null)
return new ArrayList<Repository>();
@ -610,7 +608,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Updates or creates the given node.
*/
public void editRepository(Repository repository, boolean create) {
public void editRepo(Repository repo, boolean create) {
try {
JSONArray repos = mConfig.getJSONArray("Repositories");
JSONObject r = null;
@ -621,18 +619,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
else {
for (int i = 0; i < repos.length(); i++) {
JSONObject json = repos.getJSONObject(i);
if (repository.ID.equals(json.getString("ID"))) {
if (repo.ID.equals(json.getString("ID"))) {
r = repos.getJSONObject(i);
break;
}
}
}
r.put("Directory", repository.Directory);
r.put("ID", repository.ID);
r.put("Directory", repo.Directory);
r.put("ID", repo.ID);
r.put("IgnorePerms", true);
r.put("ReadOnly", repository.ReadOnly);
r.put("ReadOnly", repo.ReadOnly);
JSONArray nodes = new JSONArray();
for (Node n : repository.Nodes) {
for (Node n : repo.Nodes) {
JSONObject element = new JSONObject();
element.put("Addresses", n.Addresses);
element.put("Name", n.Name);
@ -641,30 +639,30 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
}
r.put("Nodes", nodes);
JSONObject versioning = new JSONObject();
versioning.put("Type", repository.Versioning.getType());
versioning.put("Type", repo.Versioning.getType());
JSONObject params = new JSONObject();
versioning.put("Params", params);
for (String key : repository.Versioning.getParams().keySet()) {
params.put(key, repository.Versioning.getParams().get(key));
for (String key : repo.Versioning.getParams().keySet()) {
params.put(key, repo.Versioning.getParams().get(key));
}
r.put("Versioning", versioning);
configUpdated();
}
catch (JSONException e) {
Log.w(TAG, "Failed to edit repo " + repository.ID + " at " + repository.Directory, e);
Log.w(TAG, "Failed to edit repo " + repo.ID + " at " + repo.Directory, e);
}
}
/**
* Deletes the given repository from syncthing.
*/
public void deleteRepository(Repository repository) {
public void deleteRepo(Repository repo) {
try {
JSONArray repos = mConfig.getJSONArray("Repositories");
for (int i = 0; i < repos.length(); i++) {
JSONObject json = repos.getJSONObject(i);
if (repository.ID.equals(json.getString("ID"))) {
if (repo.ID.equals(json.getString("ID"))) {
mConfig.remove("Repositories");
mConfig.put("Repositories", delete(repos, repos.getJSONObject(i)));
break;

View file

@ -1,9 +1,6 @@
package com.nutomic.syncthingandroid.util;
import android.util.Log;
import android.util.Pair;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

View file

@ -6,7 +6,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
@ -20,13 +19,13 @@ import java.util.Map;
/**
* Generates item views for node items.
*/
public class NodeAdapter extends ArrayAdapter<RestApi.Node>
public class NodesAdapter extends ArrayAdapter<RestApi.Node>
implements RestApi.OnReceiveConnectionsListener {
private Map<String, RestApi.Connection> mConnections =
new HashMap<String, RestApi.Connection>();
public NodeAdapter(Context context) {
public NodesAdapter(Context context) {
super(context, R.layout.node_list_item);
}