Code Refactor

This commit is contained in:
Umberto Furno 2020-01-05 19:53:43 +01:00
parent 6c62cc412b
commit 99b0690e20
4 changed files with 76 additions and 26 deletions

View File

@ -61,6 +61,35 @@ class WListRepository(
removeOldWListsFromDb(wLists)
}
fun deleteWList(idBoard: String, idWList:String) {
service.deleteList(idBoard, idWList).enqueue(
object : Callback<it.unisannio.ding.ids.wedroid.wrapper.entity.WList> {
override fun onFailure(call: Call<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>, t: Throwable) {
logNetworkError(t.message)
}
override fun onResponse(
call: Call<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>,
response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>
) {
CoroutineScope(Dispatchers.IO).launch {
deleteWListCallBack(response,idWList)
}
}
})
}
private fun deleteWListCallBack(response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>,
id: String) {
if (!response.isSuccessful) {
logNetworkError("${response.code()} ${response.message()}")
return
}
dao.delete(WList(id))
}
private suspend fun addNewWListToDb(wLists: Collection<WList>) {
wLists.forEach {
dao.insert(it)

View File

@ -9,6 +9,7 @@ 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;
@ -30,16 +31,18 @@ import retrofit2.Response;
public class BoardViewActivity extends AppCompatActivity {
String idBoard, username, boardTitle;
int boardColor;
TextView description, members, permission, creationDate, lastModificationDate;
View divider1, divider2, divider3;
ListView listView;
Button getListsButton;
SharedPreferenceHelper sp;
ServicesFactory service;
Toolbar myToolbar;
Board board;
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;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -50,6 +53,7 @@ public class BoardViewActivity extends AppCompatActivity {
Intent i = getIntent();
idBoard= i.getStringExtra("idBoard");
getListsButton = findViewById(R.id.getLists);
description = findViewById(R.id.descriptionTxt);
members = findViewById(R.id.membersTxt);
@ -66,7 +70,6 @@ public class BoardViewActivity extends AppCompatActivity {
initializeUI(idBoard);
getListsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -74,7 +77,7 @@ public class BoardViewActivity extends AppCompatActivity {
i.putExtra("idBoard", idBoard);
i.putExtra("barTitle", boardTitle);
i.putExtra("barColor", boardColor);
startActivity(i);
startActivityForResult(i, WLISTS_REQUEST);
}
});
}
@ -112,7 +115,6 @@ public class BoardViewActivity extends AppCompatActivity {
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());
@ -123,8 +125,7 @@ public class BoardViewActivity extends AppCompatActivity {
}
@Override
public void onFailure(Call<Board> call, Throwable t) {
//boardTitle.setText(t.toString());
public void onFailure(@NotNull Call<Board> call, @NotNull Throwable t) {
//TODO
}
});
@ -187,9 +188,20 @@ public class BoardViewActivity extends AppCompatActivity {
}
@Override
public void onFailure(Call<User> call, Throwable t) {
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();
}
}
}
}

View File

@ -1,9 +1,7 @@
package it.unisannio.ding.ids.wedroid.app.view;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
@ -18,7 +16,6 @@ import java.util.Objects;
import it.unisannio.ding.ids.wedroid.app.R;
import it.unisannio.ding.ids.wedroid.app.data.entity.WList;
import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader;
import it.unisannio.ding.ids.wedroid.app.util.SharedPreferenceHelper;
import it.unisannio.ding.ids.wedroid.app.view.adapter.WListsAdapter;
import it.unisannio.ding.ids.wedroid.app.viewModel.WListsListViewModel;
@ -26,8 +23,10 @@ import it.unisannio.ding.ids.wedroid.app.viewModel.WListsListViewModel;
public class WListsListActivity extends AppCompatActivity {
Toolbar myToolbar;
String boardTitle;
int barColor;
WListsListViewModel viewModel;
RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -35,24 +34,27 @@ public class WListsListActivity extends AppCompatActivity {
setContentView(R.layout.activity_wlists_view);
myToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
Intent i = getIntent();
boardTitle = i.getStringExtra("barTitle");
Intent i = getIntent();
String boardTitle = i.getStringExtra("barTitle");
Objects.requireNonNull(getSupportActionBar()).setTitle(boardTitle);
barColor = i.getIntExtra("barColor", 0);
setResult(WListsListActivity.RESULT_OK,i);
myToolbar.setBackgroundColor(barColor);
PreferenceReader reader =new SharedPreferenceHelper(this);
recyclerView = findViewById(R.id.recyclerviewWList);
initializeUi();
}
RecyclerView recyclerView = findViewById(R.id.recyclerviewWList);
private void initializeUi() {
final WListsAdapter adapter = new WListsAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
//linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//recyclerView.setHasFixedSize(true);
WListsListViewModel viewModel = new ViewModelProvider(this).get(WListsListViewModel.class);
viewModel = new ViewModelProvider(this).get(WListsListViewModel.class);
viewModel.getAllWLists().observe(this, new Observer<List<WList>>() {
@Override

View File

@ -34,6 +34,13 @@ public class WListsListViewModel extends AndroidViewModel {
return allWLists;
}
public void deleteWList(int position, String idBoard) {
List<WList> wList = allWLists.getValue();
if (wList != null)
wListRepository.deleteWList(idBoard, wList.get(position).getId());
}
public void refresh() {
wListRepository.synchronize();
}