Settings now loads existing settings.

This commit is contained in:
Nicolás Sánchez 2024-12-10 10:30:32 -03:00
parent 5936e12c5b
commit 981ba3286f
2 changed files with 33 additions and 2 deletions

View File

@ -38,6 +38,21 @@ public class SettingsFragment extends Fragment {
Button buttonSaveSettings = root.findViewById(R.id.buttonSaveSettings); Button buttonSaveSettings = root.findViewById(R.id.buttonSaveSettings);
//Load settings if settings.json exists
SettingsData settingsData = settingsViewModel.loadSettings(getContext());
if (settingsData != null) {
editProfileName.setText(settingsData.profileName);
editApiUrl.setText(settingsData.apiUrl);
editApiKey.setText(settingsData.apiKey);
checkBox.setChecked(settingsData.useTelegram);
editBotToken.setText(settingsData.botToken);
editChatId.setText(settingsData.chatId);
editTimeBetweenQueries.setText(String.valueOf(settingsData.timeBetweenQueries));
editAmountOfLogLines.setText(String.valueOf(settingsData.amountOfLogLines));
editAmountOfLastTrades.setText(String.valueOf(settingsData.amountOfLastTrades));
}
// Add listener to the checkbox so we can enable or disable the editTexts if the checkbox // Add listener to the checkbox so we can enable or disable the editTexts if the checkbox
// is checked or unchecked respectively // is checked or unchecked respectively
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> { checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {

View File

@ -7,14 +7,15 @@ import android.content.Context;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
public class SettingsViewModel extends ViewModel { public class SettingsViewModel extends ViewModel {
//private Context context;
public String profileName; public String profileName;
public String apiUrl; public String apiUrl;
public String apiKey; public String apiKey;
@ -73,4 +74,19 @@ public class SettingsViewModel extends ViewModel {
e.printStackTrace(); e.printStackTrace();
} }
} }
public SettingsData loadSettings(Context context) {
Gson gson = new Gson();
File file = new File(context.getFilesDir(), "settings.json");
if (file.exists()) {
try {
FileReader reader = new FileReader(file);
return gson.fromJson(reader, SettingsData.class);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
} }