-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
284 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
app/src/androidTest/java/com/cybershark/linkmanager/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/cybershark/linkmanager/repository/IRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.cybershark.linkmanager.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
import com.cybershark.linkmanager.repository.room.entities.LinkEntity | ||
|
||
interface IRepository { | ||
val allLinks: LiveData<List<LinkEntity>> | ||
|
||
suspend fun insertLink(linkEntity: LinkEntity): Result<Unit> | ||
|
||
suspend fun deleteAllLinks(): Result<Unit> | ||
|
||
suspend fun updateLink(linkEntity: LinkEntity): Result<Unit> | ||
|
||
suspend fun deleteLinkById(linkId: Int): Result<Unit> | ||
|
||
suspend fun getAllLinksAsString(): Result<String> | ||
} |
63 changes: 57 additions & 6 deletions
63
app/src/main/java/com/cybershark/linkmanager/repository/Repository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,68 @@ | ||
package com.cybershark.linkmanager.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
import com.cybershark.linkmanager.repository.room.dao.LinkDao | ||
import com.cybershark.linkmanager.repository.room.entities.LinkEntity | ||
import com.cybershark.linkmanager.ui.links.viewmodels.LinksViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class Repository(private val linksDao: LinkDao) { | ||
class Repository(private val linksDao: LinkDao) : IRepository { | ||
|
||
val allLinks = linksDao.getAllLinks() | ||
override val allLinks: LiveData<List<LinkEntity>> = linksDao.getAllLinks() | ||
|
||
suspend fun insertLink(linkEntity: LinkEntity) = linksDao.insertLink(linkEntity) | ||
override suspend fun insertLink(linkEntity: LinkEntity): Result<Unit> = | ||
withContext(Dispatchers.IO) { | ||
try { | ||
linksDao.insertLink(linkEntity) | ||
return@withContext Result.success(Unit) | ||
} catch (e: Exception) { | ||
return@withContext Result.failure(e) | ||
} | ||
} | ||
|
||
suspend fun deleteAllLinks() = linksDao.deleteAllLinks() | ||
override suspend fun deleteAllLinks(): Result<Unit> = withContext(Dispatchers.IO) { | ||
try { | ||
linksDao.deleteAllLinks() | ||
return@withContext Result.success(Unit) | ||
} catch (e: Exception) { | ||
return@withContext Result.failure(e) | ||
} | ||
} | ||
|
||
suspend fun updateLink(linkEntity: LinkEntity) = linksDao.updateLink(linkEntity) | ||
override suspend fun updateLink(linkEntity: LinkEntity): Result<Unit> = | ||
withContext(Dispatchers.IO) { | ||
try { | ||
linksDao.updateLink(linkEntity) | ||
return@withContext Result.success(Unit) | ||
} catch (e: Exception) { | ||
return@withContext Result.failure(e) | ||
} | ||
} | ||
|
||
suspend fun deleteLinkById(linkId: Int) = linksDao.deleteLinkById(linkId) | ||
override suspend fun deleteLinkById(linkId: Int): Result<Unit> = withContext(Dispatchers.IO) { | ||
try { | ||
linksDao.deleteLinkById(linkId) | ||
return@withContext Result.success(Unit) | ||
} catch (e: Exception) { | ||
return@withContext Result.failure(e) | ||
} | ||
} | ||
|
||
override suspend fun getAllLinksAsString(): Result<String> = withContext(Dispatchers.Default) { | ||
try { | ||
if (allLinks.value.isNullOrEmpty()) { | ||
return@withContext Result.failure(Exception("No links added!")) | ||
} | ||
val string = buildString { | ||
append(LinksViewModel.START_MESSAGE) | ||
allLinks.value?.forEach { | ||
append("${it.linkName} - ${it.linkURL}\n") | ||
} | ||
} | ||
return@withContext Result.success(string) | ||
} catch (e: Exception) { | ||
return@withContext Result.failure(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.