This commit is contained in:
parent
c06bd99fd0
commit
8e9e001c4b
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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<List<Board>> = repository.allBoards
|
||||
|
||||
fun insertBoard(title: String) = repository.insertBoard(title)
|
||||
|
||||
fun deleteBoard(id: String) = repository.deleteBoard(id)
|
||||
}
|
@ -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<List<Board>> 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<List<Board>> getAllBoards() {
|
||||
return allBoards;
|
||||
}
|
||||
|
||||
public void insertBoard(String title) {
|
||||
repository.insertBoard(title);
|
||||
}
|
||||
|
||||
public void deleteBoard(String id) {
|
||||
repository.deleteBoard(id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user