diff --git a/app/build.gradle b/app/build.gradle index 2777071..c4ca91a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,6 +40,7 @@ dependencies { implementation libs.lifecycle.viewmodel.ktx implementation libs.navigation.fragment implementation libs.navigation.ui + implementation libs.gson testImplementation libs.junit androidTestImplementation libs.ext.junit androidTestImplementation libs.espresso.core diff --git a/app/src/main/java/com/example/dcav2gui/MainActivity.java b/app/src/main/java/com/example/dcav2gui/MainActivity.java index fea71d3..80c04fe 100644 --- a/app/src/main/java/com/example/dcav2gui/MainActivity.java +++ b/app/src/main/java/com/example/dcav2gui/MainActivity.java @@ -37,6 +37,7 @@ public class MainActivity extends AppCompatActivity { .setAnchorView(R.id.fab).show(); } }); + DrawerLayout drawer = binding.drawerLayout; NavigationView navigationView = binding.navView; // Passing each menu ID as a set of Ids because each @@ -68,4 +69,4 @@ public class MainActivity extends AppCompatActivity { return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp(); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsData.java b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsData.java new file mode 100644 index 0000000..be275cd --- /dev/null +++ b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsData.java @@ -0,0 +1,36 @@ +// com.example.dcav2gui.ui.settings.SettingsData.java +package com.example.dcav2gui.ui.settings; + +public class SettingsData { + public String profileName; + public String apiUrl; + public String apiKey; + public boolean useTelegram; + public String botToken; + public String chatId; + public float timeBetweenQueries; + public int amountOfLogLines; + public int amountOfLastTrades; + + public SettingsData( + String profileName, + String apiUrl, + String apiKey, + boolean useTelegram, + String botToken, + String chatId, + float timeBetweenQueries, + int amountOfLogLines, + int amountOfLastTrades + ) { + this.profileName = profileName; + this.apiUrl = apiUrl; + this.apiKey = apiKey; + this.useTelegram = useTelegram; + this.botToken = botToken; + this.chatId = chatId; + this.timeBetweenQueries = timeBetweenQueries; + this.amountOfLogLines = amountOfLogLines; + this.amountOfLastTrades = amountOfLastTrades; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsFragment.java b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsFragment.java new file mode 100644 index 0000000..c54c463 --- /dev/null +++ b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsFragment.java @@ -0,0 +1,72 @@ +// com.example.dcav2gui.ui.settings.SettingsFragment.java +package com.example.dcav2gui.ui.settings; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; + +import com.example.dcav2gui.R; + +import java.util.Objects; + +public class SettingsFragment extends Fragment { + + private SettingsViewModel settingsViewModel; + + public View onCreateView(@NonNull LayoutInflater inflater, + ViewGroup container, Bundle savedInstanceState) { + settingsViewModel = new ViewModelProvider(this).get(SettingsViewModel.class); + + View root = inflater.inflate(R.layout.fragment_settings, container, false); + + EditText editProfileName = root.findViewById(R.id.editProfileName); + EditText editApiUrl = root.findViewById(R.id.editApiUrl); + EditText editApiKey = root.findViewById(R.id.editApiKey); + CheckBox checkBox = root.findViewById(R.id.useTelegram); + EditText editBotToken = root.findViewById(R.id.editBotToken); + EditText editChatId = root.findViewById(R.id.editChatId); + EditText editTimeBetweenQueries = root.findViewById(R.id.editTimeBetweenQueries); + EditText editAmountOfLogLines = root.findViewById(R.id.editAmountOfLogLines); + EditText editAmountofLastTrades = root.findViewById(R.id.editAmountOfLastTrades); + + Button buttonSaveSettings = root.findViewById(R.id.buttonSaveSettings); + + buttonSaveSettings.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String profileName = editProfileName.getText().toString(); + String apiUrl = editApiUrl.getText().toString(); + String apiKey = editApiKey.getText().toString(); + boolean useTelegram = checkBox.isChecked(); + String botToken = editBotToken.getText().toString(); + String chatId = editChatId.getText().toString(); + int timeBetweenQueries = Integer.parseInt(editTimeBetweenQueries.getText().toString()); + int amountOfLogLines = Integer.parseInt(editAmountOfLogLines.getText().toString()); + int amountOfLastTrades = Integer.parseInt(editAmountofLastTrades.getText().toString()); + settingsViewModel.saveSettings( + getContext(), + profileName, + apiUrl, + apiKey, + useTelegram, + botToken, + chatId, + timeBetweenQueries, + amountOfLogLines, + amountOfLastTrades); + Toast.makeText(getContext(), "Settings saved", Toast.LENGTH_SHORT).show(); + } + }); + + return root; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsViewModel.java b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsViewModel.java new file mode 100644 index 0000000..c77efec --- /dev/null +++ b/app/src/main/java/com/example/dcav2gui/ui/settings/SettingsViewModel.java @@ -0,0 +1,76 @@ +// com.example.dcav2gui.ui.settings.SettingsViewModel.java +package com.example.dcav2gui.ui.settings; + + +import android.content.Context; + +import androidx.lifecycle.ViewModel; + +import com.google.gson.Gson; + +import java.io.File; +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; + public boolean useTelegram; + public String botToken; + public String chatId; + public float timeBetweenQueries; + public int amountOfLogLines; + public int amountOfLastTrades; + + public void saveSettings( + Context context, + String profileName, + String apiUrl, + String apiKey, + boolean useTelegram, + String botToken, + String chatId, + float timeBetweenQueries, + int amountOfLogLines, + int amountOfLastTrades + ) { + Context appContext = context.getApplicationContext(); + + this.profileName = profileName; + this.apiUrl = apiUrl; + this.apiKey = apiKey; + this.useTelegram = useTelegram; + this.botToken = botToken; + this.chatId = chatId; + this.timeBetweenQueries = timeBetweenQueries; + this.amountOfLogLines = amountOfLogLines; + this.amountOfLastTrades = amountOfLastTrades; + + // Logic to save settings to JSON file + Gson gson = new Gson(); + SettingsData settingsData = new SettingsData( + profileName, + apiUrl, + apiKey, + useTelegram, + botToken, + chatId, + timeBetweenQueries, + amountOfLogLines, + amountOfLastTrades + ); + + try { + File file = new File(context.getFilesDir(), "settings.json"); + FileWriter writer = new FileWriter(file); + gson.toJson(settingsData, writer); + writer.flush(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml new file mode 100644 index 0000000..467da6b --- /dev/null +++ b/app/src/main/res/layout/fragment_settings.xml @@ -0,0 +1,172 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +