неужели что-то работает

This commit is contained in:
ityurner02@mail.ru 2023-12-21 21:02:40 +04:00
parent 0374fb1c29
commit 9a5617611d
10 changed files with 1538 additions and 11 deletions

File diff suppressed because it is too large Load Diff

View File

@ -117,7 +117,7 @@ interface ServerService{
@Path("id") id: Int, @Path("id") id: Int,
): UserRemote ): UserRemote
companion object { 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 @Volatile
private var INSTANCE: ServerService? = null private var INSTANCE: ServerService? = null

View File

@ -8,6 +8,7 @@ import androidx.paging.RemoteMediator
import androidx.room.withTransaction import androidx.room.withTransaction
import com.example.myapplication.api.ServerService import com.example.myapplication.api.ServerService
import com.example.myapplication.api.model.toAuthor 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.database.AppDatabase
import com.example.myapplication.db.model.Author import com.example.myapplication.db.model.Author
import com.example.myapplication.db.model.RemoteKeyType import com.example.myapplication.db.model.RemoteKeyType
@ -21,6 +22,7 @@ import java.io.IOException
class AuthorRemoteMediator( class AuthorRemoteMediator(
private val service: ServerService, private val service: ServerService,
private val dbAuthorRepository: OfflineAuthorRepository, private val dbAuthorRepository: OfflineAuthorRepository,
private val dbBookRepository: OfflineBookRepository,
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository, private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
private val database: AppDatabase private val database: AppDatabase
): RemoteMediator<Int, Author>() { ): RemoteMediator<Int, Author>() {
@ -53,11 +55,14 @@ class AuthorRemoteMediator(
try { try {
val authors = service.getAuthors(page, state.config.pageSize).map{it.toAuthor()} 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("Authors info", authors.toString())
Log.i("Books info", books.toString())
val endOfPaginationReached = authors.isEmpty() val endOfPaginationReached = authors.isEmpty()
database.withTransaction { database.withTransaction {
if (loadType == LoadType.REFRESH) { if (loadType == LoadType.REFRESH) {
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.AUTHOR) dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.AUTHOR)
dbBookRepository.clearAll()
dbAuthorRepository.clearAll() dbAuthorRepository.clearAll()
} }
val prevKey = if (page == 1) null else page - 1 val prevKey = if (page == 1) null else page - 1
@ -72,6 +77,10 @@ class AuthorRemoteMediator(
} }
dbRemoteKeyRepository.createRemoteKeys(keys) dbRemoteKeyRepository.createRemoteKeys(keys)
dbAuthorRepository.insertAuthors(authors) dbAuthorRepository.insertAuthors(authors)
try{
dbBookRepository.insertBooks(books)
}catch (_: Exception){ }
} }
return RemoteMediator.MediatorResult.Success(endOfPaginationReached = endOfPaginationReached) return RemoteMediator.MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
} catch (exception: IOException) { } catch (exception: IOException) {

View File

@ -7,14 +7,19 @@ import androidx.paging.PagingState
import androidx.paging.RemoteMediator import androidx.paging.RemoteMediator
import androidx.room.withTransaction import androidx.room.withTransaction
import com.example.myapplication.api.ServerService 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.toBook
import com.example.myapplication.api.model.toUser
import com.example.myapplication.api.respositories.RestAuthorRepository 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.database.AppDatabase
import com.example.myapplication.db.model.Book import com.example.myapplication.db.model.Book
import com.example.myapplication.db.model.RemoteKeyType import com.example.myapplication.db.model.RemoteKeyType
import com.example.myapplication.db.model.RemoteKeys 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.OfflineBookRepository
import com.example.myapplication.db.respository.OfflineRemoteKeyRepository import com.example.myapplication.db.respository.OfflineRemoteKeyRepository
import com.example.myapplication.db.respository.OfflineUserRepository
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -22,8 +27,9 @@ import java.io.IOException
class BookRemoteMediator( class BookRemoteMediator(
private val service: ServerService, private val service: ServerService,
private val dbBookRepository: OfflineBookRepository, private val dbBookRepository: OfflineBookRepository,
private val dbAuthorRepository: OfflineAuthorRepository,
private val dbUserRepository: OfflineUserRepository,
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository, private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
private val authorRestRepository: RestAuthorRepository,
private val database: AppDatabase private val database: AppDatabase
) : RemoteMediator<Int, Book>() { ) : RemoteMediator<Int, Book>() {
override suspend fun initialize(): InitializeAction { override suspend fun initialize(): InitializeAction {
@ -55,12 +61,16 @@ class BookRemoteMediator(
try { try {
val books = service.getBooks(page, state.config.pageSize).map { it.toBook() } 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()) Log.i("Books info", books.toString())
val endOfPaginationReached = books.isEmpty() val endOfPaginationReached = books.isEmpty()
database.withTransaction { database.withTransaction {
if (loadType == LoadType.REFRESH) { if (loadType == LoadType.REFRESH) {
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.BOOK) dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.BOOK)
dbBookRepository.clearAll() dbBookRepository.clearAll()
dbAuthorRepository.clearAll()
dbUserRepository.clearAll()
} }
val prevKey = if (page == 1) null else page - 1 val prevKey = if (page == 1) null else page - 1
val nextKey = if (endOfPaginationReached) null else page + 1 val nextKey = if (endOfPaginationReached) null else page + 1
@ -72,8 +82,13 @@ class BookRemoteMediator(
nextKey = nextKey nextKey = nextKey
) )
} }
authorRestRepository.getAll()
dbRemoteKeyRepository.createRemoteKeys(keys) dbRemoteKeyRepository.createRemoteKeys(keys)
try{
dbAuthorRepository.insertAuthors(authors)
}catch (_: Exception){ }
try{
dbUserRepository.insertUsers(users)
}catch (_: Exception){ }
dbBookRepository.insertBooks(books) dbBookRepository.insertBooks(books)
} }
return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached) return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)

View File

@ -15,11 +15,13 @@ import com.example.myapplication.api.model.toAuthor
import com.example.myapplication.api.model.toAuthorRemote import com.example.myapplication.api.model.toAuthorRemote
import com.example.myapplication.db.model.Author import com.example.myapplication.db.model.Author
import com.example.myapplication.api.respositories.Mediator.AuthorRemoteMediator import com.example.myapplication.api.respositories.Mediator.AuthorRemoteMediator
import com.example.myapplication.db.respository.OfflineBookRepository
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class RestAuthorRepository( class RestAuthorRepository(
private val service: ServerService, private val service: ServerService,
private val dbAuthorRepository: OfflineAuthorRepository, private val dbAuthorRepository: OfflineAuthorRepository,
private val dbBookRepository: OfflineBookRepository,
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository, private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
private val database: AppDatabase private val database: AppDatabase
): AuthorRepository { ): AuthorRepository {
@ -63,6 +65,7 @@ class RestAuthorRepository(
remoteMediator = AuthorRemoteMediator( remoteMediator = AuthorRemoteMediator(
service, service,
dbAuthorRepository, dbAuthorRepository,
dbBookRepository,
dbRemoteKeyRepository, dbRemoteKeyRepository,
database, database,
), ),

View File

@ -15,13 +15,16 @@ import com.example.myapplication.api.model.toBookRemote
import com.example.myapplication.api.respositories.Mediator.BookRemoteMediator import com.example.myapplication.api.respositories.Mediator.BookRemoteMediator
import com.example.myapplication.db.database.AppContainer import com.example.myapplication.db.database.AppContainer
import com.example.myapplication.db.model.Book 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 import kotlinx.coroutines.flow.Flow
class RestBookRepository( class RestBookRepository(
private val service: ServerService, private val service: ServerService,
private val dbBookRepository: OfflineBookRepository, private val dbBookRepository: OfflineBookRepository,
private val dbAuthorRepository: OfflineAuthorRepository,
private val dbUserRepository: OfflineUserRepository,
private val dbRemoteKeyRepository: OfflineRemoteKeyRepository, private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
private val authorRestRepository: RestAuthorRepository,
private val database: AppDatabase private val database: AppDatabase
):BookRepository { ):BookRepository {
override suspend fun getAll(): List<Book> { override suspend fun getAll(): List<Book> {
@ -58,8 +61,9 @@ class RestBookRepository(
remoteMediator = BookRemoteMediator( remoteMediator = BookRemoteMediator(
service, service,
dbBookRepository, dbBookRepository,
dbAuthorRepository,
dbUserRepository,
dbRemoteKeyRepository, dbRemoteKeyRepository,
authorRestRepository,
database, database,
), ),
pagingSourceFactory = pagingSourceFactory pagingSourceFactory = pagingSourceFactory

View File

@ -22,9 +22,11 @@ interface UserDao {
@Query("select * from users where login = :login and password = :password") @Query("select * from users where login = :login and password = :password")
suspend fun tryLogin(login: String, password: String): User? suspend fun tryLogin(login: String, password: String): User?
@Query("DELETE FROM users")
suspend fun clearAll()
@Insert @Insert
suspend fun insert(user: User) suspend fun insert(vararg user: User)
@Update @Update
suspend fun update(user: User) suspend fun update(user: User)

View File

@ -44,8 +44,9 @@ class AppDataContainer(private val context: Context) : AppContainer {
RestBookRepository( RestBookRepository(
ServerService.getInstance(), ServerService.getInstance(),
bookRepository, bookRepository,
authorRepository,
userRepository,
remoteKeyRepository, remoteKeyRepository,
authorRestRepository,
AppDatabase.getInstance(context) AppDatabase.getInstance(context)
) )
} }
@ -54,6 +55,7 @@ class AppDataContainer(private val context: Context) : AppContainer {
RestAuthorRepository( RestAuthorRepository(
ServerService.getInstance(), ServerService.getInstance(),
authorRepository, authorRepository,
bookRepository,
remoteKeyRepository, remoteKeyRepository,
AppDatabase.getInstance(context) AppDatabase.getInstance(context)
) )

View File

@ -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 getUserBooks(userid: Int): List<Book> = userDao.getUserBooks(userid)
override suspend fun tryLogin(login: String, password: String): User? = userDao.tryLogin(login, password) 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) override suspend fun insert(user: User) = userDao.insert(user)

View File

@ -2,6 +2,7 @@
<network-security-config> <network-security-config>
<domain-config cleartextTrafficPermitted="true"> <domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">100.87.48.148</domain> <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> </domain-config>
</network-security-config> </network-security-config>