Settings now loads existing settings.
This commit is contained in:
parent
5936e12c5b
commit
981ba3286f
|
|
@ -38,6 +38,21 @@ public class SettingsFragment extends Fragment {
|
|||
|
||||
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
|
||||
// is checked or unchecked respectively
|
||||
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
|
|
|
|||
|
|
@ -7,14 +7,15 @@ import android.content.Context;
|
|||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SettingsViewModel extends ViewModel {
|
||||
//private Context context;
|
||||
|
||||
public String profileName;
|
||||
public String apiUrl;
|
||||
public String apiKey;
|
||||
|
|
@ -73,4 +74,19 @@ public class SettingsViewModel extends ViewModel {
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue