From 8e9e001c4b7e1eae1e0d32dce855c8f56fec163b Mon Sep 17 00:00:00 2001 From: norangebit Date: Sat, 30 Nov 2019 14:43:52 +0100 Subject: [PATCH] refactoring --- .../app/data/database/BoardDatabase.java | 30 +++++++++++++ .../app/data/database/BoardDatabase.kt | 35 --------------- .../wedroid/app/view/BoardsListsActivity.kt | 6 +-- .../app/viewmodel/BoardListViewModel.kt | 25 ----------- .../app/viewmodel/BoardsListViewModel.java | 44 +++++++++++++++++++ 5 files changed, 77 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.java delete mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.kt delete mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardListViewModel.kt create mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.java new file mode 100644 index 0000000..07181ba --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.java @@ -0,0 +1,30 @@ +package it.unisannio.ding.ids.wedroid.app.data.database; + +import android.content.Context; + +import androidx.room.Database; +import androidx.room.Room; +import androidx.room.RoomDatabase; + +import it.unisannio.ding.ids.wedroid.app.data.dao.BoardDao; +import it.unisannio.ding.ids.wedroid.app.data.entity.Board; + +@Database(entities = Board.class, version = 1, exportSchema = false) +public abstract class BoardDatabase extends RoomDatabase { + private static volatile BoardDatabase INSTANCE; + public abstract BoardDao boardDao(); + + public static BoardDatabase getDatabase(Context context) { + if (INSTANCE != null) + return INSTANCE; + synchronized (BoardDatabase.class) { + INSTANCE = Room.databaseBuilder( + context.getApplicationContext(), + BoardDatabase.class, + "board_database" + ).build(); + + return INSTANCE; + } + } +} diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.kt deleted file mode 100644 index e99fc50..0000000 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/database/BoardDatabase.kt +++ /dev/null @@ -1,35 +0,0 @@ -package it.unisannio.ding.ids.wedroid.app.data.database - -import android.content.Context -import androidx.room.Database -import androidx.room.Room -import androidx.room.RoomDatabase -import it.unisannio.ding.ids.wedroid.app.data.dao.BoardDao -import it.unisannio.ding.ids.wedroid.app.data.entity.Board - -@Database(entities = [Board::class], version = 1, exportSchema = false) -abstract class BoardDatabase : RoomDatabase() { - - abstract fun boardDao(): BoardDao - - companion object { - @Volatile - private var INSTANCE: BoardDatabase? = null - - fun getDatabase(context: Context): BoardDatabase { - val tempInstance = INSTANCE - if (tempInstance != null) { - return tempInstance - } - synchronized(this) { - val instance = Room.databaseBuilder( - context.applicationContext, - BoardDatabase::class.java, - "board_database" - ).build() - INSTANCE = instance - return instance - } - } - } -} \ No newline at end of file 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 702e7ac..6c36ccd 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 @@ -7,19 +7,19 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager 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.BoardListViewModel +import it.unisannio.ding.ids.wedroid.app.viewmodel.BoardsListViewModel import kotlinx.android.synthetic.main.activity_boards_lists.* import kotlinx.android.synthetic.main.content_boards_lists.* class BoardsListsActivity : AppCompatActivity() { - private lateinit var viewModel: BoardListViewModel + private lateinit var viewModel: BoardsListViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_boards_lists) setSupportActionBar(toolbar) - viewModel = ViewModelProvider(this).get(BoardListViewModel::class.java) + viewModel = ViewModelProvider(this).get(BoardsListViewModel::class.java) val adapter = BoardsListAdapter(this) boardList.adapter = adapter diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardListViewModel.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardListViewModel.kt deleted file mode 100644 index 50f7161..0000000 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardListViewModel.kt +++ /dev/null @@ -1,25 +0,0 @@ -package it.unisannio.ding.ids.wedroid.app.viewmodel - -import android.app.Application -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.LiveData -import it.unisannio.ding.ids.wedroid.app.util.ServicesFactory -import it.unisannio.ding.ids.wedroid.app.data.database.BoardDatabase -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.DumReader - -class BoardListViewModel(application: Application) : AndroidViewModel(application) { - - private val repository: BoardRepository = BoardRepository( - BoardDatabase.getDatabase(application).boardDao(), - ServicesFactory.getInstance(TODO()).boardService, - TODO() - ) - - val allBoards: LiveData> = repository.allBoards - - fun insertBoard(title: String) = repository.insertBoard(title) - - fun deleteBoard(id: String) = repository.deleteBoard(id) -} \ No newline at end of file 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 new file mode 100644 index 0000000..f8f3a9f --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java @@ -0,0 +1,44 @@ +package it.unisannio.ding.ids.wedroid.app.viewmodel; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; + +import java.util.List; + +import it.unisannio.ding.ids.wedroid.app.data.database.BoardDatabase; +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; + +public class BoardsListViewModel extends AndroidViewModel { + private BoardRepository repository; + private LiveData> allBoards; + + public BoardsListViewModel(@NonNull Application application) { + super(application); + PreferenceReader reader = null; //TODO + repository = new BoardRepository( + BoardDatabase.getDatabase(application).boardDao(), + ServicesFactory.Companion.getInstance(reader).getBoardService(), + reader + ); + + allBoards = repository.getAllBoards(); + } + + public LiveData> getAllBoards() { + return allBoards; + } + + public void insertBoard(String title) { + repository.insertBoard(title); + } + + public void deleteBoard(String id) { + repository.deleteBoard(id); + } +}