Skip to content

Commit

Permalink
Edit Server address in settings (#223)
Browse files Browse the repository at this point in the history
* Sign out user on Server change

* Use server address from settings in UserAPI

* Add settings menu to Sign in and Sign up
  • Loading branch information
Tom-stack3 committed Jun 24, 2022
1 parent 465c052 commit 0ebda18
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class AppContext {
SharedPreferences preferences;
SharedPreferences.Editor editor;

public SharedPreferences.Editor getEditor() {
return editor;
}

public AppContext() {
preferences = SignInActivity.context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
editor = preferences.edit();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.makore.api;

import androidx.preference.PreferenceManager;

import com.example.makore.auth.SignInActivity;

import java.util.Map;

import retrofit2.Call;
Expand All @@ -11,8 +15,10 @@ public class UserAPI {
UserServiceAPI userServiceAPI;

public UserAPI() {
// Get server address from settings
String apiAddress = "http://" + PreferenceManager.getDefaultSharedPreferences(SignInActivity.context).getString("server", "10.0.2.2:54321");
retrofit = new Retrofit.Builder().
baseUrl("http://10.0.2.2:54321").
baseUrl(apiAddress).
addConverterFactory(GsonConverterFactory.create()).
build();
userServiceAPI = retrofit.create(UserServiceAPI.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -17,6 +19,7 @@
import com.example.makore.MainActivity;
import com.example.makore.R;
import com.example.makore.api.UserAPI;
import com.example.makore.chat.SettingsActivity;
import com.example.makore.databinding.ActivitySignInBinding;

import java.util.Map;
Expand Down Expand Up @@ -92,6 +95,27 @@ public void onFailure(@NonNull Call<Map<String, String>> call, @NonNull Throwabl
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_auth, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
// Navigate to the settings activity
Intent intent = new Intent(SignInActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -22,6 +24,7 @@
import com.example.makore.MainActivity;
import com.example.makore.R;
import com.example.makore.api.UserAPI;
import com.example.makore.chat.SettingsActivity;
import com.example.makore.databinding.ActivitySignUpBinding;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -165,6 +168,27 @@ public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_auth, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
// Navigate to the settings activity
Intent intent = new Intent(SignUpActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.room.Room;

import com.example.makore.AppContext;
import com.example.makore.R;
import com.example.makore.auth.SignInActivity;
import com.example.makore.entities.AppDB;

public class SettingsActivity extends AppCompatActivity {
private SharedPreferences settingsSharedPreferences;
private AppDB db;

private void initDB() {
// Create Room database
db = Room.databaseBuilder(getApplicationContext(),
AppDB.class, AppDB.DATABASE_NAME).allowMainThreadQueries().build();
}

private void changeTheme(boolean isNightMode) {
if (isNightMode) {
Expand All @@ -24,6 +36,20 @@ private void changeTheme(boolean isNightMode) {
}
}

private void changeServer(String server) {
// Sign out the user
AppContext appContext = new AppContext();
appContext.set("username", "");
appContext.getEditor().clear();
appContext.getEditor().apply();
// Clear the database
db.clearAllTables();
// Navigate to the sign in activity
Intent intent = new Intent(SettingsActivity.this, SignInActivity.class);
startActivity(intent);
finish();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -35,17 +61,23 @@ protected void onCreate(Bundle savedInstanceState) {
.replace(R.id.settings, new SettingsFragment())
.commit();
}
// Display back button
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
settingsSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.OnSharedPreferenceChangeListener listener = (sharedPreferences1, key) -> {
// Set default values for the preferences (before creating a listener!)
PreferenceManager.setDefaultValues(this, R.xml.root_preferences, true);
SharedPreferences.OnSharedPreferenceChangeListener listener = (preferences, key) -> {
if (key.equals("dark_mode")) {
changeTheme(sharedPreferences1.getBoolean(key, false));
changeTheme(preferences.getBoolean(key, false));
} else if (key.equals("server")) {
changeServer(preferences.getString(key, getString(R.string.default_server_address)));
}
};
settingsSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
initDB();
}

public static class SettingsFragment extends PreferenceFragmentCompat {
Expand Down
10 changes: 10 additions & 0 deletions android-client/app/src/main/res/menu/menu_auth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions android-client/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<string name="cV_description_profile_picture">Profile picture</string>
<string name="description_send_message_button">Send message button</string>
<string name="type_a_message">Type a message…</string>
<string name="default_server_address">10.0.2.2:54321</string>
<string name="connection_error">Error connecting to server</string>
<string name="invalid_credentials">Invalid username or password</string>
<string name="app_toolbar_logo_description">App toolbar logo</string>
Expand Down
1 change: 1 addition & 0 deletions android-client/app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<EditTextPreference
app:key="server"
app:title="@string/settings_server_title"
app:defaultValue="@string/default_server_address"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>
Expand Down

0 comments on commit 0ebda18

Please sign in to comment.