232 lines
8.9 KiB
Java
232 lines
8.9 KiB
Java
package it.unisannio.ding.ids.wedroid.app.view;
|
|
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.Button;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.appcompat.widget.Toolbar;
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Objects;
|
|
|
|
import it.unisannio.ding.ids.wedroid.app.R;
|
|
import it.unisannio.ding.ids.wedroid.app.util.ServicesFactory;
|
|
import it.unisannio.ding.ids.wedroid.app.util.SharedPreferenceHelper;
|
|
import it.unisannio.ding.ids.wedroid.wrapper.entity.Board;
|
|
import it.unisannio.ding.ids.wedroid.wrapper.entity.User;
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
public class BoardViewActivity extends AppCompatActivity {
|
|
|
|
static final int WLISTS_REQUEST = 30;
|
|
|
|
private String idBoard, username, boardTitle;
|
|
private int boardColor;
|
|
private TextView description, members, permission, creationDate, lastModificationDate;
|
|
private View divider1, divider2, divider3;
|
|
private ListView listView;
|
|
private Button getListsButton;
|
|
private SharedPreferenceHelper sp;
|
|
private ServicesFactory service;
|
|
private Toolbar myToolbar;
|
|
private Board board;
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
@Override
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_board_view);
|
|
|
|
findViews();
|
|
|
|
setSupportActionBar(myToolbar);
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
Intent i = getIntent();
|
|
idBoard = i.getStringExtra("idBoard");
|
|
|
|
sp = new SharedPreferenceHelper(this);
|
|
sp.setBoardId(idBoard);
|
|
|
|
initializeUI(idBoard);
|
|
|
|
getListsButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
Intent i = new Intent(getApplicationContext(), WListsListActivity.class);
|
|
i.putExtra("idBoard", idBoard);
|
|
i.putExtra("barTitle", boardTitle);
|
|
i.putExtra("barColor", boardColor);
|
|
startActivityForResult(i, WLISTS_REQUEST);
|
|
}
|
|
});
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
@Override
|
|
public void onRefresh() {
|
|
initializeUI(idBoard);
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void findViews() {
|
|
myToolbar = findViewById(R.id.my_toolbar);
|
|
getListsButton = findViewById(R.id.getLists);
|
|
description = findViewById(R.id.descriptionTxt);
|
|
members = findViewById(R.id.membersTxt);
|
|
permission = findViewById(R.id.permissionTxt);
|
|
creationDate = findViewById(R.id.createdDate);
|
|
lastModificationDate = findViewById(R.id.modifiedDate);
|
|
divider1 = findViewById(R.id.divider1);
|
|
divider2 = findViewById(R.id.divider2);
|
|
divider3 = findViewById(R.id.divider3);
|
|
listView = findViewById(R.id.listViewID);
|
|
swipeRefreshLayout = findViewById(R.id.pullToRefresh);
|
|
}
|
|
|
|
private void initializeUI(String idBoard) {
|
|
service = new ServicesFactory(sp);
|
|
service.getBoardService().getBoard(idBoard).enqueue(new Callback<Board>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<Board> call, @NotNull Response<Board> response) {
|
|
assert response.body() != null;
|
|
board = response.body();
|
|
if (board.getId() == null) {
|
|
Toast.makeText(
|
|
getApplicationContext(),
|
|
getApplicationContext().getString(R.string.board_deleted),
|
|
Toast.LENGTH_LONG)
|
|
.show();
|
|
getListsButton.setEnabled(false);
|
|
} else {
|
|
Objects.requireNonNull(getSupportActionBar()).setTitle(board.getTitle());
|
|
myToolbar.setBackgroundColor(Color.parseColor(encodeColor(
|
|
board.getBackgroundColor().toString())));
|
|
|
|
boardTitle = board.getTitle();
|
|
|
|
boardColor = Color.parseColor(encodeColor(
|
|
board.getBackgroundColor().toString()));
|
|
|
|
Drawable background = getListsButton.getBackground();
|
|
background.setTint(boardColor);
|
|
getListsButton.setBackgroundDrawable(background);
|
|
|
|
description.setText(board.getDescription());
|
|
permission.setText(board.getPermission().toString());
|
|
members.setText("");
|
|
for (int i = 0; i < board.getMembers().size(); i++) {
|
|
replaceIDUserToUsername(board.getMembers().get(i).getUserId());
|
|
}
|
|
|
|
creationDate.setText(R.string.created_at);
|
|
creationDate.append("\n"
|
|
+ board.getCreatedAt());
|
|
lastModificationDate.setText(R.string.modified_at);
|
|
lastModificationDate.append("\n"
|
|
+ board.getModifiedAt());
|
|
|
|
divider1.setBackgroundColor(boardColor);
|
|
divider2.setBackgroundColor(boardColor);
|
|
divider3.setBackgroundColor(boardColor);
|
|
|
|
ArrayList<String> labelsTitle = new ArrayList<>();
|
|
for (int i = 0; i < board.getLabels().size(); i++) {
|
|
labelsTitle.add(board.getLabels().get(i).getName());
|
|
}
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
|
|
android.R.layout.simple_list_item_1, labelsTitle);
|
|
listView.setAdapter(adapter);
|
|
getListsButton.setEnabled(true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<Board> call, @NotNull Throwable t) {
|
|
Toast.makeText(getApplicationContext(),
|
|
"connection error",
|
|
Toast.LENGTH_LONG).show();
|
|
getListsButton.setEnabled(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
private String encodeColor(String color) {
|
|
String encodedColor;
|
|
if (color.equalsIgnoreCase("belize")) {
|
|
encodedColor = "#2980B9";
|
|
} else if (color.equalsIgnoreCase("nephritis")) {
|
|
encodedColor = "#27AE60";
|
|
} else if (color.equalsIgnoreCase("pomegranate")) {
|
|
encodedColor = "#C0392B";
|
|
} else if (color.equalsIgnoreCase("pumpkin")) {
|
|
encodedColor = "#E67E22";
|
|
} else if (color.equalsIgnoreCase("wisteria")) {
|
|
encodedColor = "#8E44AD";
|
|
} else if (color.equalsIgnoreCase("moderatepink")) {
|
|
encodedColor = "#CD5A91";
|
|
} else if (color.equalsIgnoreCase("strongcyan")) {
|
|
encodedColor = "#00AECC";
|
|
} else if (color.equalsIgnoreCase("dark")) {
|
|
encodedColor = "#2C3E51";
|
|
} else if (color.equalsIgnoreCase("midnight")) {
|
|
encodedColor = "#2C3E50";
|
|
} else if (color.equalsIgnoreCase("relax")) {
|
|
encodedColor = "#27AE61";
|
|
} else if (color.equalsIgnoreCase("corteza")) {
|
|
encodedColor = "#568BA2";
|
|
} else
|
|
encodedColor = "#38DF87";
|
|
|
|
return encodedColor;
|
|
}
|
|
|
|
private void replaceIDUserToUsername(String idUser) {
|
|
service = new ServicesFactory(sp);
|
|
service.getUserService().getUser(idUser).enqueue(new Callback<User>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<User> call, @NotNull Response<User> response) {
|
|
User u = response.body();
|
|
assert u != null;
|
|
username = u.getUsername();
|
|
if (u.isAdmin()) {
|
|
members.append("Admin: " + username + ";\n");
|
|
} else
|
|
members.append("Other member: " + username + ";\n");
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<User> call, @NotNull Throwable t) {
|
|
//TODO
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
if (requestCode == WLISTS_REQUEST) {
|
|
if (resultCode != WListsListActivity.RESULT_OK) {
|
|
Toast.makeText(this, "It's not possible view the lists",
|
|
Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
}
|
|
}
|