неужели что-то работает
This commit is contained in:
parent
0374fb1c29
commit
9a5617611d
1493
Server/data.json
1493
Server/data.json
File diff suppressed because it is too large
Load Diff
@ -117,7 +117,7 @@ interface ServerService{
|
||||
@Path("id") id: Int,
|
||||
): UserRemote
|
||||
companion object {
|
||||
private const val BASE_URL = "http://100.87.48.148:8079/"
|
||||
private const val BASE_URL = "http://92.252.221.42:8079/"
|
||||
|
||||
@Volatile
|
||||
private var INSTANCE: ServerService? = null
|
||||
|
@ -8,6 +8,7 @@ import androidx.paging.RemoteMediator
|
||||
import androidx.room.withTransaction
|
||||
import com.example.myapplication.api.ServerService
|
||||
import com.example.myapplication.api.model.toAuthor
|
||||
import com.example.myapplication.api.model.toBook
|
||||
import com.example.myapplication.db.database.AppDatabase
|
||||
import com.example.myapplication.db.model.Author
|
||||
import com.example.myapplication.db.model.RemoteKeyType
|
||||
@ -21,6 +22,7 @@ import java.io.IOException
|
||||
class AuthorRemoteMediator(
|
||||
private val service: ServerService,
|
||||
private val dbAuthorRepository: OfflineAuthorRepository,
|
||||
private val dbBookRepository: OfflineBookRepository,
|
||||
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
|
||||
private val database: AppDatabase
|
||||
): RemoteMediator<Int, Author>() {
|
||||
@ -53,11 +55,14 @@ class AuthorRemoteMediator(
|
||||
|
||||
try {
|
||||
val authors = service.getAuthors(page, state.config.pageSize).map{it.toAuthor()}
|
||||
val books = service.getBooks().map { it.toBook() }
|
||||
Log.i("Authors info", authors.toString())
|
||||
Log.i("Books info", books.toString())
|
||||
val endOfPaginationReached = authors.isEmpty()
|
||||
database.withTransaction {
|
||||
if (loadType == LoadType.REFRESH) {
|
||||
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.AUTHOR)
|
||||
dbBookRepository.clearAll()
|
||||
dbAuthorRepository.clearAll()
|
||||
}
|
||||
val prevKey = if (page == 1) null else page - 1
|
||||
@ -72,6 +77,10 @@ class AuthorRemoteMediator(
|
||||
}
|
||||
dbRemoteKeyRepository.createRemoteKeys(keys)
|
||||
dbAuthorRepository.insertAuthors(authors)
|
||||
|
||||
try{
|
||||
dbBookRepository.insertBooks(books)
|
||||
}catch (_: Exception){ }
|
||||
}
|
||||
return RemoteMediator.MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
|
||||
} catch (exception: IOException) {
|
||||
|
@ -7,14 +7,19 @@ import androidx.paging.PagingState
|
||||
import androidx.paging.RemoteMediator
|
||||
import androidx.room.withTransaction
|
||||
import com.example.myapplication.api.ServerService
|
||||
import com.example.myapplication.api.model.toAuthor
|
||||
import com.example.myapplication.api.model.toBook
|
||||
import com.example.myapplication.api.model.toUser
|
||||
import com.example.myapplication.api.respositories.RestAuthorRepository
|
||||
import com.example.myapplication.api.respositories.RestUserRepository
|
||||
import com.example.myapplication.db.database.AppDatabase
|
||||
import com.example.myapplication.db.model.Book
|
||||
import com.example.myapplication.db.model.RemoteKeyType
|
||||
import com.example.myapplication.db.model.RemoteKeys
|
||||
import com.example.myapplication.db.respository.OfflineAuthorRepository
|
||||
import com.example.myapplication.db.respository.OfflineBookRepository
|
||||
import com.example.myapplication.db.respository.OfflineRemoteKeyRepository
|
||||
import com.example.myapplication.db.respository.OfflineUserRepository
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
|
||||
@ -22,8 +27,9 @@ import java.io.IOException
|
||||
class BookRemoteMediator(
|
||||
private val service: ServerService,
|
||||
private val dbBookRepository: OfflineBookRepository,
|
||||
private val dbAuthorRepository: OfflineAuthorRepository,
|
||||
private val dbUserRepository: OfflineUserRepository,
|
||||
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
|
||||
private val authorRestRepository: RestAuthorRepository,
|
||||
private val database: AppDatabase
|
||||
) : RemoteMediator<Int, Book>() {
|
||||
override suspend fun initialize(): InitializeAction {
|
||||
@ -55,12 +61,16 @@ class BookRemoteMediator(
|
||||
|
||||
try {
|
||||
val books = service.getBooks(page, state.config.pageSize).map { it.toBook() }
|
||||
val authors = service.getAuthors().map { it.toAuthor() }
|
||||
val users = service.getUsers().map { it.toUser() }
|
||||
Log.i("Books info", books.toString())
|
||||
val endOfPaginationReached = books.isEmpty()
|
||||
database.withTransaction {
|
||||
if (loadType == LoadType.REFRESH) {
|
||||
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.BOOK)
|
||||
dbBookRepository.clearAll()
|
||||
dbAuthorRepository.clearAll()
|
||||
dbUserRepository.clearAll()
|
||||
}
|
||||
val prevKey = if (page == 1) null else page - 1
|
||||
val nextKey = if (endOfPaginationReached) null else page + 1
|
||||
@ -72,8 +82,13 @@ class BookRemoteMediator(
|
||||
nextKey = nextKey
|
||||
)
|
||||
}
|
||||
authorRestRepository.getAll()
|
||||
dbRemoteKeyRepository.createRemoteKeys(keys)
|
||||
try{
|
||||
dbAuthorRepository.insertAuthors(authors)
|
||||
}catch (_: Exception){ }
|
||||
try{
|
||||
dbUserRepository.insertUsers(users)
|
||||
}catch (_: Exception){ }
|
||||
dbBookRepository.insertBooks(books)
|
||||
}
|
||||
return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
|
||||
|
@ -15,11 +15,13 @@ import com.example.myapplication.api.model.toAuthor
|
||||
import com.example.myapplication.api.model.toAuthorRemote
|
||||
import com.example.myapplication.db.model.Author
|
||||
import com.example.myapplication.api.respositories.Mediator.AuthorRemoteMediator
|
||||
import com.example.myapplication.db.respository.OfflineBookRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class RestAuthorRepository(
|
||||
private val service: ServerService,
|
||||
private val dbAuthorRepository: OfflineAuthorRepository,
|
||||
private val dbBookRepository: OfflineBookRepository,
|
||||
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
|
||||
private val database: AppDatabase
|
||||
): AuthorRepository {
|
||||
@ -63,6 +65,7 @@ class RestAuthorRepository(
|
||||
remoteMediator = AuthorRemoteMediator(
|
||||
service,
|
||||
dbAuthorRepository,
|
||||
dbBookRepository,
|
||||
dbRemoteKeyRepository,
|
||||
database,
|
||||
),
|
||||
|
@ -15,13 +15,16 @@ import com.example.myapplication.api.model.toBookRemote
|
||||
import com.example.myapplication.api.respositories.Mediator.BookRemoteMediator
|
||||
import com.example.myapplication.db.database.AppContainer
|
||||
import com.example.myapplication.db.model.Book
|
||||
import com.example.myapplication.db.respository.OfflineAuthorRepository
|
||||
import com.example.myapplication.db.respository.OfflineUserRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class RestBookRepository(
|
||||
private val service: ServerService,
|
||||
private val dbBookRepository: OfflineBookRepository,
|
||||
private val dbAuthorRepository: OfflineAuthorRepository,
|
||||
private val dbUserRepository: OfflineUserRepository,
|
||||
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
|
||||
private val authorRestRepository: RestAuthorRepository,
|
||||
private val database: AppDatabase
|
||||
):BookRepository {
|
||||
override suspend fun getAll(): List<Book> {
|
||||
@ -58,8 +61,9 @@ class RestBookRepository(
|
||||
remoteMediator = BookRemoteMediator(
|
||||
service,
|
||||
dbBookRepository,
|
||||
dbAuthorRepository,
|
||||
dbUserRepository,
|
||||
dbRemoteKeyRepository,
|
||||
authorRestRepository,
|
||||
database,
|
||||
),
|
||||
pagingSourceFactory = pagingSourceFactory
|
||||
|
@ -22,9 +22,11 @@ interface UserDao {
|
||||
|
||||
@Query("select * from users where login = :login and password = :password")
|
||||
suspend fun tryLogin(login: String, password: String): User?
|
||||
@Query("DELETE FROM users")
|
||||
suspend fun clearAll()
|
||||
|
||||
@Insert
|
||||
suspend fun insert(user: User)
|
||||
suspend fun insert(vararg user: User)
|
||||
|
||||
@Update
|
||||
suspend fun update(user: User)
|
||||
|
@ -44,8 +44,9 @@ class AppDataContainer(private val context: Context) : AppContainer {
|
||||
RestBookRepository(
|
||||
ServerService.getInstance(),
|
||||
bookRepository,
|
||||
authorRepository,
|
||||
userRepository,
|
||||
remoteKeyRepository,
|
||||
authorRestRepository,
|
||||
AppDatabase.getInstance(context)
|
||||
)
|
||||
}
|
||||
@ -54,6 +55,7 @@ class AppDataContainer(private val context: Context) : AppContainer {
|
||||
RestAuthorRepository(
|
||||
ServerService.getInstance(),
|
||||
authorRepository,
|
||||
bookRepository,
|
||||
remoteKeyRepository,
|
||||
AppDatabase.getInstance(context)
|
||||
)
|
||||
|
@ -13,6 +13,10 @@ class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
|
||||
override suspend fun getUserBooks(userid: Int): List<Book> = userDao.getUserBooks(userid)
|
||||
|
||||
override suspend fun tryLogin(login: String, password: String): User? = userDao.tryLogin(login, password)
|
||||
suspend fun insertUsers(users: List<User>) =
|
||||
userDao.insert(*users.toTypedArray())
|
||||
|
||||
suspend fun clearAll() = userDao.clearAll()
|
||||
|
||||
override suspend fun insert(user: User) = userDao.insert(user)
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
<network-security-config>
|
||||
<domain-config cleartextTrafficPermitted="true">
|
||||
<domain includeSubdomains="true">100.87.48.148</domain>
|
||||
<domain includeSubdomains="true">192.168.56.1</domain>
|
||||
<domain includeSubdomains="true">192.168.43.198</domain>
|
||||
<domain includeSubdomains="true">92.252.221.42</domain>
|
||||
</domain-config>
|
||||
</network-security-config>
|
Loading…
Reference in New Issue
Block a user