Skip to content

Commit

Permalink
Use one save() method to add and update projects in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 5, 2021
1 parent dd866c9 commit dfcdcd4
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
Project currentProject = currentProjectProvider.getCurrentProject();
switch (preference.getKey()) {
case PROJECT_NAME_KEY:
projectsRepository.update(currentProject.copy(String.valueOf(newValue), currentProject.getIcon(), currentProject.getColor(), currentProject.getUuid()));
projectsRepository.save(currentProject.copy(String.valueOf(newValue), currentProject.getIcon(), currentProject.getColor(), currentProject.getUuid()));
break;
case PROJECT_ICON_KEY:
projectsRepository.update(currentProject.copy(currentProject.getName(), String.valueOf(newValue), currentProject.getColor(), currentProject.getUuid()));
projectsRepository.save(currentProject.copy(currentProject.getName(), String.valueOf(newValue), currentProject.getColor(), currentProject.getUuid()));
break;
case PROJECT_COLOR_KEY:
projectsRepository.update(currentProject.copy(currentProject.getName(), currentProject.getIcon(), String.valueOf(newValue), currentProject.getUuid()));
projectsRepository.save(currentProject.copy(currentProject.getName(), currentProject.getIcon(), String.valueOf(newValue), currentProject.getUuid()));
setProjectColorSummary();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AddProjectDialog : MaterialFullScreenDialogFragment() {
}

binding.addButton.setOnClickListener {
projectsRepository.add(Project(getProjectName(), getProjectIcon(), getProjectColor()))
projectsRepository.save(Project(getProjectName(), getProjectIcon(), getProjectColor()))
listener?.onProjectAdded()
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class ProjectImporter(
) {
fun importDemoProject() {
val project = Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID)
projectsRepository.add(project)
projectsRepository.save(project)
metaSettings.save(MetaKeys.CURRENT_PROJECT_ID, DEMO_PROJECT_ID)
}

// Now it does the same like importDemoProject() but it should be changed later
fun importExistingProject() {
val project = Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID)
projectsRepository.add(project)
projectsRepository.save(project)
metaSettings.save(MetaKeys.CURRENT_PROJECT_ID, DEMO_PROJECT_ID)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AddProjectDialogTest {
val scenario = RobolectricHelpers.launchDialogFragmentInContainer(AddProjectDialog::class.java)
scenario.onFragment {
onView(withText(R.string.add)).perform(click())
verify(projectsRepository).add(Project("", "", ""))
verify(projectsRepository).save(Project("", "", ""))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class ProjectImporterTest {
@Test
fun `Default project should be imported when importDemoProject() called`() {
projectImporter.importDemoProject()
verify(projectsRepository).add(Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID))
verify(projectsRepository).save(Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID))
verify(metaSettings).save(MetaKeys.CURRENT_PROJECT_ID, DEMO_PROJECT_ID)
}

@Test
fun `Existed project should be imported when importExistingProject() called`() {
projectImporter.importExistingProject()
verify(projectsRepository).add(Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID))
verify(projectsRepository).save(Project("Demo project", "D", "#3e9fcc", DEMO_PROJECT_ID))
verify(metaSettings).save(MetaKeys.CURRENT_PROJECT_ID, DEMO_PROJECT_ID)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class ProjectSettingsDialogTest {

@Test
fun `currentProjectViewModel should be notified when project switched`() {
appDependencyComponent.projectsRepository().add(Project("Project X", "X", "#cccccc", "1"))
appDependencyComponent.projectsRepository().add(Project("Project Y", "Y", "#ffffff", "2"))
appDependencyComponent.projectsRepository().save(Project("Project X", "X", "#cccccc", "1"))
appDependencyComponent.projectsRepository().save(Project("Project Y", "Y", "#ffffff", "2"))
appDependencyComponent.currentProjectProvider().setCurrentProject("1")

val scenario = RobolectricHelpers.launchDialogFragmentInContainer(ProjectSettingsDialog::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ThemeUtilsTests() {

@Before
public void setup() {
DaggerUtils.getComponent(RuntimeEnvironment.application).projectsRepository().add(new Project("Project 1", "P", "#ffffff", "1"));
DaggerUtils.getComponent(RuntimeEnvironment.application).projectsRepository().save(new Project("Project 1", "P", "#ffffff", "1"));
DaggerUtils.getComponent(RuntimeEnvironment.application).currentProjectProvider().setCurrentProject("1");

mainMenuActivity = Robolectric.setupActivity(MainMenuActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ class InMemProjectsRepository(private val uuidGenerator: UUIDGenerator) : Projec

override fun getAll() = projects

override fun add(project: Project) {
override fun save(project: Project) {
if (project.uuid == NOT_SPECIFIED_UUID) {
projects.add(project.copy(uuid = uuidGenerator.generateUUID()))
} else {
projects.add(project)
}
}

override fun update(project: Project) {
val projectIndex = projects.indexOf(get(project.uuid))
if (projectIndex != -1) {
projects.set(projectIndex, project)
val projectIndex = projects.indexOf(get(project.uuid))
if (projectIndex == -1) {
projects.add(project)
} else {
projects.set(projectIndex, project)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ interface ProjectsRepository {

fun getAll(): List<Project>

fun add(project: Project)

fun update(project: Project)
fun save(project: Project)

fun delete(uuid: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@ class SharedPreferencesProjectsRepository(
}
}

override fun add(project: Project) {
val projects = if (project.uuid == NOT_SPECIFIED_UUID) {
getAll().toMutableList().plus(project.copy(uuid = uuidGenerator.generateUUID()))
} else {
getAll().toMutableList().plus(project)
}
metaSettings.save(key, gson.toJson(projects))
}

override fun update(project: Project) {
override fun save(project: Project) {
val projects = getAll().toMutableList()
val projectIndex = projects.indexOf(get(project.uuid))
if (projectIndex != -1) {
projects.set(projectIndex, project)

if (project.uuid == NOT_SPECIFIED_UUID) {
projects.add(project.copy(uuid = uuidGenerator.generateUUID()))
} else {
val projectIndex = projects.indexOf(get(project.uuid))
if (projectIndex == -1) {
projects.add(project)
} else {
projects.set(projectIndex, project)
}
}
metaSettings.save(key, gson.toJson(projects))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ abstract class ProjectsRepositoryTest {

@Test
fun `getAll() should return all projects from storage`() {
projectsRepository.add(projectX)
projectsRepository.add(projectY)
projectsRepository.add(projectZ)
projectsRepository.save(projectX)
projectsRepository.save(projectY)
projectsRepository.save(projectZ)

val projects = projectsRepository.getAll()

Expand All @@ -37,7 +37,7 @@ abstract class ProjectsRepositoryTest {

@Test
fun `add() should save project to storage`() {
projectsRepository.add(projectX)
projectsRepository.save(projectX)

val projects = projectsRepository.getAll()

Expand All @@ -47,25 +47,25 @@ abstract class ProjectsRepositoryTest {

@Test
fun `add() should add uuid if not specified`() {
projectsRepository.add(projectX)
projectsRepository.save(projectX)
assertThat(projectsRepository.getAll()[0].uuid, `is`(not(isEmptyString())))
}

@Test
fun `add() should not add uuid if specified`() {
projectsRepository.add(projectX.copy(uuid = ""))
projectsRepository.save(projectX.copy(uuid = ""))
assertThat(projectsRepository.get(""), `is`(projectX.copy(uuid = "")))
}

@Test
fun `update() should update project`() {
projectsRepository.add(projectX)
projectsRepository.add(projectY)
projectsRepository.add(projectZ)
fun `add() should update project if already exists`() {
projectsRepository.save(projectX)
projectsRepository.save(projectY)
projectsRepository.save(projectZ)

val originalProjectX = projectsRepository.getAll()[0]
val updatedProjectX = originalProjectX.copy(name = "Project X2", "2", "#ff80ff")
projectsRepository.update(updatedProjectX)
projectsRepository.save(updatedProjectX)

val projects = projectsRepository.getAll()
assertThat(projects.size, `is`(3))
Expand All @@ -76,8 +76,8 @@ abstract class ProjectsRepositoryTest {

@Test
fun `delete() should delete project from storage for given uuid`() {
projectsRepository.add(projectX)
projectsRepository.add(projectY)
projectsRepository.save(projectX)
projectsRepository.save(projectY)

var projects = projectsRepository.getAll()
projectsRepository.delete(projects.first { it.name == "ProjectX" }.uuid)
Expand All @@ -89,8 +89,8 @@ abstract class ProjectsRepositoryTest {

@Test
fun `deleteAll() should delete all projects from storage`() {
projectsRepository.add(projectX)
projectsRepository.add(projectY)
projectsRepository.save(projectX)
projectsRepository.save(projectY)

projectsRepository.deleteAll()
val projects = projectsRepository.getAll()
Expand Down

0 comments on commit dfcdcd4

Please sign in to comment.