diff --git a/app/build.gradle b/app/build.gradle index 352ef63..7de7ce6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,7 +57,6 @@ dependencies { implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" - // TESTING testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' diff --git a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt index dae8ff8..2655621 100644 --- a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt +++ b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt @@ -62,15 +62,29 @@ class BoardDaoTest { } } + @Test + fun replaceOnConflict() { + val board0 = Board("id", "title0") + val board1 = Board("id", "title1") + + runBlocking { + dao.insert(board0) + dao.insert(board1) + } + + dao.getAllBoard().observeOnce { + assertEquals(1, it.size) + assertEquals("title1", it[0].title) + } + } + + @Test fun getInAscendingOrder() { val board0 = Board("id0", "title0") val board1 = Board("id1", "title1") runBlocking { dao.insert(board1) - } - - runBlocking { dao.insert(board0) } @@ -81,14 +95,12 @@ class BoardDaoTest { } } + @Test fun delete() { val board = Board("id", "title") runBlocking { dao.insert(board) - } - - runBlocking { dao.delete(board) } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e589c5d..591040a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,7 @@ - + + - + android:theme="@style/AppTheme.NoActionBar"> diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt index 22179c0..8e7675d 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt @@ -7,6 +7,7 @@ import it.unisannio.ding.ids.wedroid.app.data.entity.convert import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader import it.unisannio.ding.ids.wedroid.wrapper.api.BoardService import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor +import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardPermission import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardPrototype import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -28,12 +29,15 @@ class BoardRepository( synchronize() } - fun insertBoard(title: String) { + fun insertBoard(title: String, isPrivate: Boolean, color: BoardBackgroundColor) { + val permission = if (isPrivate) BoardPermission.PRIVATE else BoardPermission.PUBLIC + service.newBoard( BoardPrototype.Builder() .setOwner(reader.userId) .setTitle(title) - .setBackgroundColor(BoardBackgroundColor.LIMEGREEN) + .setBackgroundColor(color) + .setBoardPermission(permission) .build() ).enqueue(object : Callback { override fun onFailure( diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt index 0e1064f..5b4681a 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt @@ -1,6 +1,8 @@ package it.unisannio.ding.ids.wedroid.app.view +import android.content.Intent import android.os.Bundle +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider @@ -10,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView import it.unisannio.ding.ids.wedroid.app.R import it.unisannio.ding.ids.wedroid.app.view.adapter.BoardsListAdapter import it.unisannio.ding.ids.wedroid.app.viewmodel.BoardsListViewModel +import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor import kotlinx.android.synthetic.main.activity_boards_lists.* import kotlinx.android.synthetic.main.content_boards_lists.* @@ -37,7 +40,10 @@ class BoardsListsActivity : AppCompatActivity() { }) fab.setOnClickListener { - viewModel.insertBoard("New board") + startActivityForResult( + Intent(this, NewBoardActivity::class.java), + NEW_BOARD_CODE + ) } pullToRefresh.setOnRefreshListener { @@ -67,5 +73,35 @@ class BoardsListsActivity : AppCompatActivity() { swipeToDelete.attachToRecyclerView(boardList) } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + NEW_BOARD_CODE -> if (data != null) addBoard(resultCode, data) + } + } + + private fun addBoard(resultCode: Int, data: Intent) { + if (resultCode != NewBoardActivity.RESULT_OK) { + Toast.makeText(this, R.string.on_add_new_board_error, Toast.LENGTH_LONG) + .show() + return + } + + val title = data.getStringExtra(NewBoardActivity.BOARD_NAME) + if (title == null) { + Toast.makeText(this, R.string.on_null_new_board_name, Toast.LENGTH_LONG) + .show() + return + } + + val isPrivate = data.getBooleanExtra(NewBoardActivity.BOARD_PRIVATE, true) + + viewModel.insertBoard(title, isPrivate, BoardBackgroundColor.LIMEGREEN) //TODO + } + + companion object { + const val NEW_BOARD_CODE = 17 + } } diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/NewBoardActivity.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/NewBoardActivity.java new file mode 100644 index 0000000..b0a48dd --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/NewBoardActivity.java @@ -0,0 +1,45 @@ +package it.unisannio.ding.ids.wedroid.app.view; + +import android.content.Intent; +import android.os.Bundle; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; + +import android.view.View; +import android.widget.EditText; +import android.widget.Switch; + +import it.unisannio.ding.ids.wedroid.app.R; + +public class NewBoardActivity extends AppCompatActivity { + private EditText boardName; + private Switch isPrivate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_new_board); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + boardName = findViewById(R.id.newBoardName); + isPrivate = findViewById(R.id.newBoardPermission); + } + + public void onDone(View v) { + Intent data = new Intent(); + data.putExtra(BOARD_NAME, boardName.getText().toString()); + data.putExtra(BOARD_PRIVATE, isPrivate.isChecked()); + setResult(RESULT_OK, data); + finish(); + } + + public void onCancel(View v) { + finish(); + } + + public static final int RESULT_OK = 17; + public static final String BOARD_NAME = "BOARD_NAME"; + public static final String BOARD_PRIVATE = "BOARD_PRIVATE"; +} diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java index 79ab9c2..05364cf 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java @@ -13,6 +13,7 @@ import it.unisannio.ding.ids.wedroid.app.data.entity.Board; import it.unisannio.ding.ids.wedroid.app.data.repository.BoardRepository; import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader; import it.unisannio.ding.ids.wedroid.app.util.ServicesFactory; +import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor; public class BoardsListViewModel extends AndroidViewModel { private BoardRepository repository; @@ -34,12 +35,15 @@ public class BoardsListViewModel extends AndroidViewModel { return allBoards; } - public void insertBoard(String title) { - repository.insertBoard(title); + public void insertBoard(String title, boolean isPrivate, BoardBackgroundColor color) { + repository.insertBoard(title, isPrivate, color); } public void deleteBoard(int position) { - repository.deleteBoard(allBoards.getValue().get(position).getId()); + List boards = allBoards.getValue(); + + if (boards != null) + repository.deleteBoard(boards.get(position).getId()); } public void refresh() { diff --git a/app/src/main/res/layout/activity_new_board.xml b/app/src/main/res/layout/activity_new_board.xml new file mode 100644 index 0000000..d32de4d --- /dev/null +++ b/app/src/main/res/layout/activity_new_board.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_new_board.xml b/app/src/main/res/layout/content_new_board.xml new file mode 100644 index 0000000..ad7e6da --- /dev/null +++ b/app/src/main/res/layout/content_new_board.xml @@ -0,0 +1,62 @@ + + + + + + + +