mirror of
https://github.com/Team4388/RidgeScout.git
synced 2026-06-09 00:37:59 -06:00
Make settings better, work on ftp transfer
This commit is contained in:
@@ -55,10 +55,8 @@ public class FieldsFragment extends Fragment {
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
binding = FragmentDataFieldsBinding.inflate(inflater, container, false);
|
||||
|
||||
binding.revertVersionButton.setVisibility(View.VISIBLE);
|
||||
binding.valueEditScrollview.setOnTouchListener((v, event) -> true);
|
||||
|
||||
|
||||
binding.saveButton.setVisibility(View.GONE);
|
||||
binding.cancelEditButton.setVisibility(View.GONE);
|
||||
binding.editButton.setVisibility(View.GONE);
|
||||
@@ -124,6 +122,25 @@ public class FieldsFragment extends Fragment {
|
||||
tr.setBackgroundColor(unfocused_background_color);
|
||||
}
|
||||
}
|
||||
|
||||
if(values.length > 1) {
|
||||
binding.revertVersionButton.setVisibility(View.VISIBLE);
|
||||
binding.revertVersionButton.setOnClickListener(v -> {
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||
alert.setTitle("Warning!");
|
||||
alert.setMessage("If there is any data set this version, it will be deleted!");
|
||||
alert.setPositiveButton("OK", (dialog, which) -> {
|
||||
inputType[][] newArr = new inputType[values.length - 1][];
|
||||
System.arraycopy(values, 0, newArr, 0, values.length - 1);
|
||||
if(fields.save(filename, newArr))
|
||||
AlertManager.toast("Saved");
|
||||
load_field_menu();
|
||||
});
|
||||
alert.setNegativeButton("Cancel", null);
|
||||
alert.setCancelable(true);
|
||||
alert.create().show();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void display_fields(inputType[] version_values) {
|
||||
@@ -182,10 +199,7 @@ public class FieldsFragment extends Fragment {
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||
alert.setTitle("Warning!");
|
||||
alert.setMessage("Changing or removing some values will result in lost data!\nBut this will create a new field version, and you can revert at any time.");
|
||||
alert.setPositiveButton("OK", null);
|
||||
alert.setNegativeButton("Cancel", null);
|
||||
alert.setCancelable(true);
|
||||
alert.setOnDismissListener(b -> {
|
||||
alert.setPositiveButton("OK", (dialog, which) -> {
|
||||
inputType[][] currentValues = fields.load(filename);
|
||||
assert currentValues != null;
|
||||
inputType[][] newValues = new inputType[currentValues.length+1][];
|
||||
@@ -198,11 +212,13 @@ public class FieldsFragment extends Fragment {
|
||||
}
|
||||
// newValues[newValues.length-1] = values[values.length-1];
|
||||
|
||||
boolean saved = fields.save(filename, newValues);
|
||||
AlertManager.alert("Saved", String.valueOf(saved));
|
||||
if(fields.save(filename, newValues))
|
||||
AlertManager.toast("Saved");
|
||||
|
||||
Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_data_fields_to_navigation_data_fields_chooser);
|
||||
});
|
||||
alert.setNegativeButton("Cancel", null);
|
||||
alert.setCancelable(true);
|
||||
alert.create().show();
|
||||
}
|
||||
|
||||
@@ -503,7 +519,7 @@ public class FieldsFragment extends Fragment {
|
||||
newValues[newValues.length-1] = field;
|
||||
values[values.length-1] = newValues;
|
||||
|
||||
AlertManager.alert("Test", String.valueOf(binding.fieldsArea.getReorderedIndexes()));
|
||||
// AlertManager.alert("Test", String.valueOf(binding.fieldsArea.getReorderedIndexes()));
|
||||
|
||||
//TableRow tr = getTableRow(field);
|
||||
//binding.fieldsArea.addView(tr);
|
||||
|
||||
@@ -310,8 +310,14 @@ public class MatchScoutingFragment extends Fragment {
|
||||
default_fields();
|
||||
set_indicator_color(unsaved_color);
|
||||
}else{
|
||||
get_fields();
|
||||
set_indicator_color(saved_color);
|
||||
try {
|
||||
get_fields();
|
||||
set_indicator_color(saved_color);
|
||||
} catch (Exception e){
|
||||
AlertManager.error(e);
|
||||
default_fields();
|
||||
set_indicator_color(unsaved_color);
|
||||
}
|
||||
}
|
||||
|
||||
asm.start();
|
||||
|
||||
@@ -119,8 +119,14 @@ public class PitScoutingFragment extends Fragment {
|
||||
default_fields();
|
||||
set_indicator_color(unsaved_color);
|
||||
}else{
|
||||
get_fields();
|
||||
set_indicator_color(saved_color);
|
||||
try {
|
||||
get_fields();
|
||||
set_indicator_color(saved_color);
|
||||
} catch (Exception e){
|
||||
AlertManager.error(e);
|
||||
default_fields();
|
||||
set_indicator_color(unsaved_color);
|
||||
}
|
||||
}
|
||||
|
||||
asm.start();
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
package com.ridgebotics.ridgescout.ui.settings;
|
||||
|
||||
import static android.text.InputType.TYPE_CLASS_NUMBER;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.AllyPosKey;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.FTPEnabled;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.FTPServer;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.SelEVCodeKey;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.TeamNumKey;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.UnameKey;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.WifiModeKey;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.defaults;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.getEditor;
|
||||
import static com.ridgebotics.ridgescout.utility.settingsManager.prefs;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -13,11 +26,14 @@ import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TableRow;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.google.android.material.divider.MaterialDivider;
|
||||
import com.ridgebotics.ridgescout.databinding.FragmentSettingsBinding;
|
||||
import com.ridgebotics.ridgescout.types.data.intType;
|
||||
import com.ridgebotics.ridgescout.utility.fileEditor;
|
||||
@@ -27,9 +43,14 @@ import com.skydoves.powerspinner.IconSpinnerAdapter;
|
||||
import com.skydoves.powerspinner.IconSpinnerItem;
|
||||
import com.skydoves.powerspinner.OnSpinnerItemSelectedListener;
|
||||
import com.skydoves.powerspinner.PowerSpinnerView;
|
||||
import com.skydoves.powerspinner.SpinnerGravity;
|
||||
|
||||
import org.checkerframework.checker.units.qual.C;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
public class settingsFragment extends Fragment {
|
||||
@@ -37,9 +58,21 @@ public class settingsFragment extends Fragment {
|
||||
private android.widget.ScrollView ScrollArea;
|
||||
private android.widget.TableLayout Table;
|
||||
|
||||
private void setDropdownItems(Spinner dropdown, String[] items){
|
||||
ArrayAdapter<String> adapter = new ArrayAdapter<>(requireActivity(), android.R.layout.simple_spinner_item, items);
|
||||
dropdown.setAdapter(adapter);
|
||||
// private void setDropdownItems(Spinner dropdown, String[] items){
|
||||
// ArrayAdapter<String> adapter = new ArrayAdapter<>(requireActivity(), android.R.layout.simple_spinner_item, items);
|
||||
// dropdown.setAdapter(adapter);
|
||||
// }
|
||||
|
||||
private View[] concatArrays(View[] a, View[] b){
|
||||
return Stream.of(a, b).flatMap(Stream::of).toArray(View[]::new);
|
||||
}
|
||||
|
||||
|
||||
private View[] addViews(View[] a){
|
||||
for(int i = 0; i < a.length; i++){
|
||||
binding.SettingsTable.addView(a[i]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
private int safeToInt(String num){
|
||||
@@ -52,6 +85,144 @@ public class settingsFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
private View[] createHeading(String name){
|
||||
TextView tv = new TextView(getContext());
|
||||
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
||||
TableRow.LayoutParams params = new TableRow.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
params.topMargin = 100;
|
||||
tv.setLayoutParams(params);
|
||||
tv.setTextSize(20);
|
||||
tv.setText(name);
|
||||
|
||||
View divider = new MaterialDivider(getContext());
|
||||
|
||||
return new View[]{tv, divider};
|
||||
}
|
||||
|
||||
private View[] addStringEdit(String name, String key){
|
||||
View[] heading = createHeading(name);
|
||||
EditText et = new EditText(getContext());
|
||||
et.setText(prefs.getString(key, (String) defaults.get(key)));
|
||||
|
||||
et.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
getEditor().putString(key, s.toString()).apply();
|
||||
}
|
||||
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
});
|
||||
return concatArrays(heading, new View[]{et});
|
||||
}
|
||||
|
||||
private View[] addNumberEdit(String name, String key){
|
||||
View[] heading = createHeading(name);
|
||||
EditText et = new EditText(getContext());
|
||||
et.setText(String.valueOf(prefs.getInt(key, (Integer) defaults.get(key))));
|
||||
et.setInputType(TYPE_CLASS_NUMBER);
|
||||
|
||||
et.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
getEditor().putInt(key, safeToInt(s.toString())).apply();
|
||||
}
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
});
|
||||
return concatArrays(heading, new View[]{et});
|
||||
}
|
||||
|
||||
private PowerSpinnerView addDropdownEdit(String name, String[] options, String key){
|
||||
PowerSpinnerView dropdown = new PowerSpinnerView(getContext());
|
||||
|
||||
List<IconSpinnerItem> iconSpinnerItems = new ArrayList<>();
|
||||
for(int i = 0; i < options.length; i++){
|
||||
iconSpinnerItems.add(new IconSpinnerItem(options[i]));
|
||||
}
|
||||
IconSpinnerAdapter iconSpinnerAdapter = new IconSpinnerAdapter(dropdown);
|
||||
|
||||
dropdown.setGravity(Gravity.CENTER);
|
||||
|
||||
dropdown.setSpinnerAdapter(iconSpinnerAdapter);
|
||||
dropdown.setItems(iconSpinnerItems);
|
||||
dropdown.setHint("Unselected");
|
||||
|
||||
dropdown.setPadding(10,20,10,20);
|
||||
dropdown.setBackgroundColor(0xf0000000);
|
||||
dropdown.setTextColor(0xff00ff00);
|
||||
dropdown.setTextSize(14.5f);
|
||||
dropdown.setArrowGravity(SpinnerGravity.END);
|
||||
dropdown.setArrowPadding(8);
|
||||
dropdown.setSpinnerPopupElevation(14);
|
||||
|
||||
return dropdown;
|
||||
}
|
||||
|
||||
private View[] addDropdownByString(String name, String[] options, String key){
|
||||
View[] heading = createHeading(name);
|
||||
PowerSpinnerView dropdown = addDropdownEdit(name, options, key);
|
||||
int index = Arrays.asList(options).indexOf(prefs.getString(key, (String) defaults.get(key)));
|
||||
System.out.println(index);
|
||||
|
||||
if(options.length != 0 && index != -1){
|
||||
dropdown.selectItemByIndex(index);
|
||||
}
|
||||
|
||||
dropdown.setOnSpinnerItemSelectedListener(
|
||||
(OnSpinnerItemSelectedListener<IconSpinnerItem>)
|
||||
(oldIndex, oldItem, newIndex, newItem) -> getEditor().putString(key, newItem.getText().toString()).apply()
|
||||
);
|
||||
|
||||
return concatArrays(heading, new View[]{dropdown});
|
||||
}
|
||||
|
||||
private View[] addDropdownByIndex(String name, String[] options, String key){
|
||||
View[] heading = createHeading(name);
|
||||
PowerSpinnerView dropdown = addDropdownEdit(name, options, key);
|
||||
|
||||
int index = prefs.getInt(key, (Integer) defaults.get(key));
|
||||
|
||||
if(dropdown.length() != 0 && index != -1){
|
||||
dropdown.selectItemByIndex(index);
|
||||
}
|
||||
|
||||
dropdown.setOnSpinnerItemSelectedListener(
|
||||
(OnSpinnerItemSelectedListener<IconSpinnerItem>)
|
||||
(oldIndex, oldItem, newIndex, newItem) -> getEditor().putInt(key, newIndex).apply()
|
||||
);
|
||||
|
||||
return concatArrays(heading, new View[]{dropdown});
|
||||
}
|
||||
|
||||
private View[] addCheckbox(String name, String key, View[] dependency){
|
||||
CheckBox cb = new CheckBox(getContext());
|
||||
cb.setText(name);
|
||||
cb.setTextSize(22);
|
||||
boolean checked = prefs.getBoolean(key, (Boolean) defaults.get(key));
|
||||
cb.setChecked(checked);
|
||||
|
||||
if(dependency != null && !checked){
|
||||
for(int i = 0; i < dependency.length; i++){
|
||||
dependency[i].setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
cb.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
getEditor().putBoolean(key, isChecked).apply();
|
||||
if(dependency != null){
|
||||
for(int i = 0; i < dependency.length; i++){
|
||||
dependency[i].setVisibility(isChecked ? View.VISIBLE : View.GONE);
|
||||
System.out.println(dependency[i]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return new View[]{new MaterialDivider(getContext()), cb};
|
||||
}
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
@@ -59,172 +230,19 @@ public class settingsFragment extends Fragment {
|
||||
binding = FragmentSettingsBinding.inflate(inflater, container, false);
|
||||
View root = binding.getRoot();
|
||||
|
||||
EditText username = binding.username;
|
||||
username.setText(settingsManager.getUsername());
|
||||
username.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
settingsManager.setUsername(username.getText().toString());
|
||||
}
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
PowerSpinnerView spinnerView = binding.eventDropdown;
|
||||
|
||||
List<IconSpinnerItem> iconSpinnerItems = new ArrayList<>();
|
||||
|
||||
String target_event_name = settingsManager.getEVCode();
|
||||
int target_index = -1;
|
||||
|
||||
ArrayList<String> evlist = fileEditor.getEventList();
|
||||
for(int i = 0; i < evlist.size(); i++){
|
||||
if(evlist.get(i).equals(target_event_name)){
|
||||
target_index = i;
|
||||
}
|
||||
iconSpinnerItems.add(new IconSpinnerItem(evlist.get(i)));
|
||||
}
|
||||
|
||||
IconSpinnerAdapter iconSpinnerAdapter = new IconSpinnerAdapter(spinnerView);
|
||||
spinnerView.setSpinnerAdapter(iconSpinnerAdapter);
|
||||
spinnerView.setItems(iconSpinnerItems);
|
||||
// spinnerView.setLifecycleOwner(this);
|
||||
|
||||
if(!iconSpinnerItems.isEmpty() && target_index != -1){
|
||||
spinnerView.selectItemByIndex(target_index);
|
||||
}
|
||||
|
||||
spinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<IconSpinnerItem>() {
|
||||
@Override
|
||||
public void onItemSelected(int oldIndex, @Nullable IconSpinnerItem oldItem, int newIndex,
|
||||
IconSpinnerItem newItem) {
|
||||
settingsManager.setEVCode(newItem.getText().toString());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
PowerSpinnerView alliance_pos_spinnerView = binding.alliancePosDropdown;
|
||||
|
||||
List<IconSpinnerItem> alliance_pos_iconSpinnerItems = new ArrayList<>();
|
||||
|
||||
String target_alliance_pos = settingsManager.getAllyPos();
|
||||
int alliance_pos_target_index = -1;
|
||||
|
||||
String[] alliance_pos_list = new String[]{"red-1", "red-2", "red-3",
|
||||
"blue-1", "blue-2", "blue-3"};
|
||||
|
||||
for(int i = 0; i < alliance_pos_list.length; i++){
|
||||
if(alliance_pos_list[i].equals(target_alliance_pos)){
|
||||
alliance_pos_target_index = i;
|
||||
}
|
||||
alliance_pos_iconSpinnerItems.add(new IconSpinnerItem(alliance_pos_list[i]));
|
||||
}
|
||||
addViews(addStringEdit("Username", UnameKey));
|
||||
addViews(addDropdownByString("Event Code", fileEditor.getEventList().toArray(new String[0]), SelEVCodeKey));
|
||||
addViews(addDropdownByString("Alliance Position", alliance_pos_list, AllyPosKey));
|
||||
addViews(addNumberEdit("Team Number", TeamNumKey));
|
||||
|
||||
IconSpinnerAdapter alliance_pos_iconSpinnerAdapter = new IconSpinnerAdapter(alliance_pos_spinnerView);
|
||||
alliance_pos_spinnerView.setSpinnerAdapter(alliance_pos_iconSpinnerAdapter);
|
||||
alliance_pos_spinnerView.setItems(alliance_pos_iconSpinnerItems);
|
||||
alliance_pos_spinnerView.setLifecycleOwner(this);
|
||||
|
||||
if(alliance_pos_target_index != -1){
|
||||
alliance_pos_spinnerView.selectItemByIndex(alliance_pos_target_index);
|
||||
}
|
||||
|
||||
alliance_pos_spinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<IconSpinnerItem>() {
|
||||
@Override
|
||||
public void onItemSelected(int oldIndex, @Nullable IconSpinnerItem oldItem, int newIndex,
|
||||
IconSpinnerItem newItem) {
|
||||
settingsManager.setAllyPos(newItem.getText().toString());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// CheckBox practice_mode = binding.practiceMode;
|
||||
// practice_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
// @Override
|
||||
// public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
|
||||
// latestSettings.settings.set_practice_mode(isChecked);
|
||||
// }
|
||||
//
|
||||
// });
|
||||
//
|
||||
// practice_mode.setChecked(latestSettings.settings.get_practice_mode());
|
||||
|
||||
|
||||
|
||||
EditText team_num = binding.teamNumber;
|
||||
team_num.setText(String.valueOf(settingsManager.getTeamNum()));
|
||||
team_num.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
settingsManager.setTeamNum(safeToInt(team_num.getText().toString()));
|
||||
}
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
CheckBox wifi_mode = binding.wifiMode;
|
||||
wifi_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
|
||||
settingsManager.setWifiMode(isChecked);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
wifi_mode.setChecked(settingsManager.getWifiMode());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Button reset_button = binding.resetButton;
|
||||
reset_button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||
alert.setTitle("Warning");
|
||||
alert.setMessage("Do you really want to reset settings?");
|
||||
alert.setCancelable(true);
|
||||
|
||||
alert.setPositiveButton("Ok", (dialog, which) -> {
|
||||
settingsManager.resetSettings();
|
||||
username.setText(settingsManager.getUsername());
|
||||
spinnerView.clearSelectedItem();
|
||||
// practice_mode.setChecked(latestSettings.settings.get_practice_mode());
|
||||
wifi_mode.setChecked(settingsManager.getWifiMode());
|
||||
alliance_pos_spinnerView.selectItemByIndex(0);
|
||||
team_num.setText(String.valueOf(settingsManager.getTeamNum()));
|
||||
});
|
||||
|
||||
alert.setNegativeButton("Cancel", null);
|
||||
alert.create().show();
|
||||
}
|
||||
});
|
||||
View[] FTPDependency = addStringEdit("FTP Server", FTPServer);
|
||||
View[] WifiDependency = addCheckbox("FTP Enabled", FTPEnabled, FTPDependency);
|
||||
addViews(addCheckbox("Wifi Mode", WifiModeKey, concatArrays(FTPDependency, WifiDependency)));
|
||||
addViews(WifiDependency);
|
||||
addViews(FTPDependency);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package com.ridgebotics.ridgescout.ui.transfer;
|
||||
|
||||
import static com.ridgebotics.ridgescout.utility.DataManager.evcode;
|
||||
import static com.ridgebotics.ridgescout.utility.fileEditor.baseDir;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.ridgebotics.ridgescout.utility.AlertManager;
|
||||
import com.ridgebotics.ridgescout.utility.DataManager;
|
||||
import com.ridgebotics.ridgescout.utility.fileEditor;
|
||||
import com.ridgebotics.ridgescout.utility.settingsManager;
|
||||
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.checkerframework.checker.units.qual.C;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FTPSync extends Thread {
|
||||
private static final String remoteBasePath = "/RidgeScout/";
|
||||
|
||||
public interface onResult {
|
||||
void onResult(boolean error);
|
||||
}
|
||||
|
||||
public onResult onResult;
|
||||
|
||||
public static void sync(onResult onResult){
|
||||
DataManager.reload_event();
|
||||
FTPSync ftpSync = new FTPSync();
|
||||
ftpSync.onResult = onResult;
|
||||
ftpSync.start();
|
||||
}
|
||||
|
||||
private class FileDate {
|
||||
public String filename;
|
||||
public Calendar lastModified;
|
||||
}
|
||||
|
||||
public boolean sendFile(FTPClient ftpClient, String filename) throws Exception{
|
||||
FileInputStream fin = new FileInputStream(baseDir + filename);
|
||||
boolean worked = ftpClient.storeFile(remoteBasePath + filename, fin);
|
||||
fin.close();
|
||||
return worked;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
InetAddress address = InetAddress.getByName(settingsManager.getFTPServer());
|
||||
ftpClient.connect(address);
|
||||
ftpClient.login("anonymous", null);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
// ftpClient.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE)
|
||||
|
||||
FTPFile[] remoteFilestmp = ftpClient.listFiles(remoteBasePath);
|
||||
String[] localFilestmp = fileEditor.getEventFiles(evcode);
|
||||
|
||||
FileDate[] remoteFiles = new FileDate[remoteFilestmp.length];
|
||||
for(int i = 0; i < remoteFilestmp.length; i++){
|
||||
// System.out.println(remoteFilestmp[i].getName());
|
||||
remoteFiles[i] = new FileDate();
|
||||
remoteFiles[i].filename = remoteFilestmp[i].getName();
|
||||
System.out.println(remoteFiles[i].filename);
|
||||
remoteFiles[i].lastModified = remoteFilestmp[i].getTimestamp();
|
||||
}
|
||||
|
||||
FileDate[] localFiles = new FileDate[localFilestmp.length];
|
||||
for(int i = 0; i < localFilestmp.length; i++){
|
||||
// sendFile(localFilestmp[i]);
|
||||
String filename = "matches.fields";
|
||||
File f = new File(baseDir + filename);
|
||||
// File f = new File(baseDir + localFilestmp[i]);
|
||||
// System.out.println(f.exists());
|
||||
// FileInputStream fin = new FileInputStream(f);
|
||||
// System.out.println(f.getName() + ", " + f.exists() + ", " + sendFile(ftpClient, f));
|
||||
// fin.close();
|
||||
|
||||
// System.out.println(ftpClient.getStatus());
|
||||
|
||||
|
||||
localFiles[i] = new FileDate();
|
||||
localFiles[i].filename = localFilestmp[i];
|
||||
localFiles[i].lastModified = fileEditor.getLastModified(localFilestmp[i]);
|
||||
}
|
||||
|
||||
FileAction[] localActions = compareDates(localFiles, fd2map(remoteFiles), false);
|
||||
System.out.println(localActions.length);
|
||||
for(int i = 0 ; i < localActions.length; i++){
|
||||
|
||||
System.out.println(localActions[i].filename + ", " + localActions[i].action);
|
||||
switch (localActions[i].action){
|
||||
case SEND:
|
||||
sendFile(ftpClient, localActions[i].filename);
|
||||
fileEditor.setLastModified(localActions[i].filename, localActions[i].otherTimestamp);
|
||||
break;
|
||||
case RECIEVE:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
AlertManager.error(e);
|
||||
onResult.onResult(true);
|
||||
AlertManager.toast("Error Syncing!");
|
||||
}
|
||||
// } finally {
|
||||
// AlertManager.toast("Synced!");
|
||||
// onResult.onResult(false);
|
||||
// }
|
||||
}
|
||||
|
||||
private Map<String, Calendar> fd2map(FileDate[] fdarr){
|
||||
Map<String, Calendar> map = new HashMap<>();
|
||||
for(int i = 0; i < fdarr.length; i++)
|
||||
map.put(fdarr[i].filename, fdarr[i].lastModified);
|
||||
return map;
|
||||
}
|
||||
|
||||
private enum SyncAction {
|
||||
SEND,
|
||||
RECIEVE,
|
||||
NONE
|
||||
}
|
||||
|
||||
private class FileAction {
|
||||
String filename;
|
||||
SyncAction action;
|
||||
Calendar otherTimestamp;
|
||||
public FileAction(String filename, SyncAction action, Calendar otherTimestamp){
|
||||
this.filename = filename;
|
||||
this.action = action;
|
||||
this.otherTimestamp = otherTimestamp;
|
||||
}
|
||||
}
|
||||
|
||||
private FileAction[] compareDates(FileDate[] files, Map<String, Calendar> refrence, boolean reverse){
|
||||
FileAction[] actions = new FileAction[files.length];
|
||||
for(int i = 0; i < files.length; i++){
|
||||
Calendar ref = refrence.get(files[i].filename);
|
||||
|
||||
System.out.println(ref);
|
||||
|
||||
if(ref == null || files[i].lastModified.after(ref)) {
|
||||
actions[i] = new FileAction(files[i].filename, !reverse ? SyncAction.SEND : SyncAction.RECIEVE, ref);
|
||||
}else if(files[i].lastModified.before(ref)){
|
||||
actions[i] = new FileAction(files[i].filename, !reverse ? SyncAction.RECIEVE : SyncAction.SEND, ref);
|
||||
}else {
|
||||
actions[i] = new FileAction(files[i].filename, SyncAction.NONE, ref);
|
||||
}
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,7 @@ public class TransferFragment extends Fragment {
|
||||
binding.uploadButton.setEnabled(false);
|
||||
binding.CSVButton.setEnabled(false);
|
||||
binding.downloadButton.setEnabled(true);
|
||||
binding.SyncButton.setEnabled(false);
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
@@ -106,8 +107,19 @@ public class TransferFragment extends Fragment {
|
||||
builder.show();
|
||||
});
|
||||
|
||||
if(!settingsManager.getWifiMode())
|
||||
if(!settingsManager.getWifiMode()) {
|
||||
binding.TBAButton.setEnabled(false);
|
||||
binding.SyncButton.setEnabled(false);
|
||||
}
|
||||
|
||||
if(!settingsManager.getFTPEnabled()) {
|
||||
binding.SyncButton.setEnabled(false);
|
||||
}
|
||||
|
||||
binding.SyncButton.setOnClickListener(v -> {
|
||||
binding.SyncButton.setEnabled(false);
|
||||
FTPSync.sync(error -> getActivity().runOnUiThread(() -> binding.SyncButton.setEnabled(true)));
|
||||
});
|
||||
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user