@@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.map
2222import kotlinx.coroutines.withContext
2323import org.ole.planet.myplanet.MainApplication
2424import org.ole.planet.myplanet.data.DatabaseService
25- import org.ole.planet.myplanet.data.api.ApiInterface
2625import org.ole.planet.myplanet.di.AppPreferences
2726import org.ole.planet.myplanet.di.RealmDispatcher
2827import org.ole.planet.myplanet.model.CreateTeamRequest
@@ -53,7 +52,6 @@ class TeamsRepositoryImpl @Inject constructor(
5352 private val sharedPrefManager : org.ole.planet.myplanet.services.SharedPrefManager ,
5453 private val serverUrlMapper : ServerUrlMapper ,
5554 private val dispatcherProvider : DispatcherProvider ,
56- private val apiInterface : ApiInterface ,
5755 private val userRepository : UserRepository ,
5856) : RealmRepository(databaseService, realmDispatcher), TeamsRepository {
5957 override suspend fun getTasksFlow (userId : String? ): Flow <List <RealmTeamTask >> {
@@ -1305,31 +1303,58 @@ class TeamsRepositoryImpl @Inject constructor(
13051303 }
13061304 }
13071305
1308- override fun insertTeamLog (realm : Realm , json : JsonObject ) {
1309- var tag = realm.where(RealmTeamLog ::class .java)
1310- .equalTo(" _id" , JsonUtils .getString(" _id" , json)).findFirst()
1311- if (tag == null ) {
1312- tag = realm.createObject(RealmTeamLog ::class .java, JsonUtils .getString(" _id" , json))
1306+ override suspend fun insertTeamLog (json : JsonObject ) {
1307+ executeTransaction { realm ->
1308+ var tag = realm.where(RealmTeamLog ::class .java)
1309+ .equalTo(" _id" , JsonUtils .getString(" _id" , json)).findFirst()
1310+ if (tag == null ) {
1311+ tag = realm.createObject(RealmTeamLog ::class .java, JsonUtils .getString(" _id" , json))
1312+ }
1313+ if (tag != null ) {
1314+ tag._rev = JsonUtils .getString(" _rev" , json)
1315+ tag._id = JsonUtils .getString(" _id" , json)
1316+ tag.type = JsonUtils .getString(" type" , json)
1317+ tag.user = JsonUtils .getString(" user" , json)
1318+ tag.createdOn = JsonUtils .getString(" createdOn" , json)
1319+ tag.parentCode = JsonUtils .getString(" parentCode" , json)
1320+ tag.time = JsonUtils .getLong(" time" , json)
1321+ tag.teamId = JsonUtils .getString(" teamId" , json)
1322+ tag.teamType = JsonUtils .getString(" teamType" , json)
1323+ }
13131324 }
1314- if (tag != null ) {
1315- tag._rev = JsonUtils .getString(" _rev" , json)
1316- tag._id = JsonUtils .getString(" _id" , json)
1317- tag.type = JsonUtils .getString(" type" , json)
1318- tag.user = JsonUtils .getString(" user" , json)
1319- tag.createdOn = JsonUtils .getString(" createdOn" , json)
1320- tag.parentCode = JsonUtils .getString(" parentCode" , json)
1321- tag.time = JsonUtils .getLong(" time" , json)
1322- tag.teamId = JsonUtils .getString(" teamId" , json)
1323- tag.teamType = JsonUtils .getString(" teamType" , json)
1325+ }
1326+
1327+ override suspend fun insertTeamLogs (logs : List <JsonObject >) {
1328+ executeTransaction { realm ->
1329+ for (json in logs) {
1330+ var tag = realm.where(RealmTeamLog ::class .java)
1331+ .equalTo(" _id" , JsonUtils .getString(" _id" , json)).findFirst()
1332+ if (tag == null ) {
1333+ tag = realm.createObject(RealmTeamLog ::class .java, JsonUtils .getString(" _id" , json))
1334+ }
1335+ if (tag != null ) {
1336+ tag._rev = JsonUtils .getString(" _rev" , json)
1337+ tag._id = JsonUtils .getString(" _id" , json)
1338+ tag.type = JsonUtils .getString(" type" , json)
1339+ tag.user = JsonUtils .getString(" user" , json)
1340+ tag.createdOn = JsonUtils .getString(" createdOn" , json)
1341+ tag.parentCode = JsonUtils .getString(" parentCode" , json)
1342+ tag.time = JsonUtils .getLong(" time" , json)
1343+ tag.teamId = JsonUtils .getString(" teamId" , json)
1344+ tag.teamType = JsonUtils .getString(" teamType" , json)
1345+ }
1346+ }
13241347 }
13251348 }
13261349
1327- override fun getLastVisit (realm : Realm , userName : String? , teamId : String? ): Long? {
1328- return realm.where(RealmTeamLog ::class .java)
1329- .equalTo(" type" , " teamVisit" )
1330- .equalTo(" user" , userName)
1331- .equalTo(" teamId" , teamId)
1332- .max(" time" )?.toLong()
1350+ override suspend fun getLastVisit (userName : String? , teamId : String? ): Long? {
1351+ return withRealm { realm ->
1352+ realm.where(RealmTeamLog ::class .java)
1353+ .equalTo(" type" , " teamVisit" )
1354+ .equalTo(" user" , userName)
1355+ .equalTo(" teamId" , teamId)
1356+ .max(" time" )?.toLong()
1357+ }
13331358 }
13341359
13351360 override fun serializeTeamActivities (log : RealmTeamLog , context : Context ): JsonObject {
@@ -1359,54 +1384,54 @@ class TeamsRepositoryImpl @Inject constructor(
13591384 val concatenatedLink = " $baseUrl /$link "
13601385 concatenatedLinks.add(concatenatedLink)
13611386 }
1362- org.ole.planet.myplanet.utils.DownloadUtils .openDownloadService(org.ole.planet.myplanet. MainApplication .context, ArrayList (concatenatedLinks), true )
1387+ org.ole.planet.myplanet.utils.DownloadUtils .openDownloadService(MainApplication .context, ArrayList (concatenatedLinks), true )
13631388 }
13641389
1365- override fun insertMyTeam (realm : io.realm. Realm , doc : com.google.gson. JsonObject ) {
1366- val status = org.ole.planet.myplanet.utils. JsonUtils .getString(" status" , doc)
1390+ override fun insertMyTeam (realm : Realm , doc : JsonObject ) {
1391+ val status = JsonUtils .getString(" status" , doc)
13671392 if (status == " archived" ) {
13681393 return
13691394 }
13701395
1371- val teamId = org.ole.planet.myplanet.utils. JsonUtils .getString(" _id" , doc)
1372- val docType = org.ole.planet.myplanet.utils. JsonUtils .getString(" docType" , doc)
1373- val userId = org.ole.planet.myplanet.utils. JsonUtils .getString(" userId" , doc)
1374- val teamIdField = org.ole.planet.myplanet.utils. JsonUtils .getString(" teamId" , doc)
1396+ val teamId = JsonUtils .getString(" _id" , doc)
1397+ val docType = JsonUtils .getString(" docType" , doc)
1398+ val userId = JsonUtils .getString(" userId" , doc)
1399+ val teamIdField = JsonUtils .getString(" teamId" , doc)
13751400
13761401 if (docType == " membership" && userId.isNotBlank() && teamIdField.isNotBlank()) {
13771402 // Server accepted the request (possibly as a new doc); remove any stale request records
1378- realm.where(org.ole.planet.myplanet.model. RealmMyTeam ::class .java)
1403+ realm.where(RealmMyTeam ::class .java)
13791404 .equalTo(" teamId" , teamIdField)
13801405 .equalTo(" userId" , userId)
13811406 .equalTo(" docType" , " request" )
13821407 .findAll()
13831408 .deleteAllFromRealm()
13841409 } else if (docType == " request" && userId.isNotBlank() && teamIdField.isNotBlank()) {
13851410 // Skip stale request record if the user is already a member
1386- val alreadyMember = realm.where(org.ole.planet.myplanet.model. RealmMyTeam ::class .java)
1411+ val alreadyMember = realm.where(RealmMyTeam ::class .java)
13871412 .equalTo(" teamId" , teamIdField)
13881413 .equalTo(" userId" , userId)
13891414 .equalTo(" docType" , " membership" )
13901415 .count() > 0
13911416 if (alreadyMember) return
13921417 }
13931418
1394- var myTeams = realm.where(org.ole.planet.myplanet.model. RealmMyTeam ::class .java).equalTo(" _id" , teamId).findFirst()
1419+ var myTeams = realm.where(RealmMyTeam ::class .java).equalTo(" _id" , teamId).findFirst()
13951420 if (myTeams == null ) {
1396- myTeams = realm.createObject(org.ole.planet.myplanet.model. RealmMyTeam ::class .java, teamId)
1421+ myTeams = realm.createObject(RealmMyTeam ::class .java, teamId)
13971422 }
13981423 myTeams?.let {
1399- org.ole.planet.myplanet.model. RealmMyTeam .populateTeamFields(doc, it, true )
1424+ RealmMyTeam .populateTeamFields(doc, it, true )
14001425 processDescription(it.description)
14011426 }
14021427 }
14031428
1404- override fun bulkInsertFromSync (realm : io.realm. Realm , jsonArray : com.google.gson.JsonArray ) {
1405- val documentList = ArrayList < com.google.gson. JsonObject > (jsonArray.size())
1429+ override fun bulkInsertFromSync (realm : Realm , jsonArray : com.google.gson.JsonArray ) {
1430+ val documentList = ArrayList <JsonObject >(jsonArray.size())
14061431 for (j in jsonArray) {
14071432 var jsonDoc = j.asJsonObject
1408- jsonDoc = org.ole.planet.myplanet.utils. JsonUtils .getJsonObject(" doc" , jsonDoc)
1409- val id = org.ole.planet.myplanet.utils. JsonUtils .getString(" _id" , jsonDoc)
1433+ jsonDoc = JsonUtils .getJsonObject(" doc" , jsonDoc)
1434+ val id = JsonUtils .getString(" _id" , jsonDoc)
14101435 if (! id.startsWith(" _design" )) {
14111436 documentList.add(jsonDoc)
14121437 }
@@ -1415,32 +1440,47 @@ class TeamsRepositoryImpl @Inject constructor(
14151440 insertMyTeam(realm, jsonDoc)
14161441 }
14171442 }
1418- override fun bulkInsertTasksFromSync (realm : io.realm. Realm , jsonArray : com.google.gson.JsonArray ) {
1419- val documentList = ArrayList < com.google.gson. JsonObject > (jsonArray.size())
1443+ override fun bulkInsertTasksFromSync (realm : Realm , jsonArray : com.google.gson.JsonArray ) {
1444+ val documentList = ArrayList <JsonObject >(jsonArray.size())
14201445 for (j in jsonArray) {
14211446 var jsonDoc = j.asJsonObject
1422- jsonDoc = org.ole.planet.myplanet.utils. JsonUtils .getJsonObject(" doc" , jsonDoc)
1423- val id = org.ole.planet.myplanet.utils. JsonUtils .getString(" _id" , jsonDoc)
1447+ jsonDoc = JsonUtils .getJsonObject(" doc" , jsonDoc)
1448+ val id = JsonUtils .getString(" _id" , jsonDoc)
14241449 if (! id.startsWith(" _design" )) {
14251450 documentList.add(jsonDoc)
14261451 }
14271452 }
14281453 documentList.forEach { jsonDoc ->
1429- org.ole.planet.myplanet.model. RealmTeamTask .insert(realm, jsonDoc)
1454+ RealmTeamTask .insert(realm, jsonDoc)
14301455 }
14311456 }
1432- override fun bulkInsertTeamActivitiesFromSync (realm : io.realm. Realm , jsonArray : com.google.gson.JsonArray ) {
1433- val documentList = ArrayList < com.google.gson. JsonObject > (jsonArray.size())
1457+ override fun bulkInsertTeamActivitiesFromSync (realm : Realm , jsonArray : com.google.gson.JsonArray ) {
1458+ val documentList = ArrayList <JsonObject >(jsonArray.size())
14341459 for (j in jsonArray) {
14351460 var jsonDoc = j.asJsonObject
1436- jsonDoc = org.ole.planet.myplanet.utils. JsonUtils .getJsonObject(" doc" , jsonDoc)
1437- val id = org.ole.planet.myplanet.utils. JsonUtils .getString(" _id" , jsonDoc)
1461+ jsonDoc = JsonUtils .getJsonObject(" doc" , jsonDoc)
1462+ val id = JsonUtils .getString(" _id" , jsonDoc)
14381463 if (! id.startsWith(" _design" )) {
14391464 documentList.add(jsonDoc)
14401465 }
14411466 }
1442- documentList.forEach { jsonDoc ->
1443- insertTeamLog(realm, jsonDoc)
1467+ for (json in documentList) {
1468+ var tag = realm.where(RealmTeamLog ::class .java)
1469+ .equalTo(" _id" , JsonUtils .getString(" _id" , json)).findFirst()
1470+ if (tag == null ) {
1471+ tag = realm.createObject(RealmTeamLog ::class .java, JsonUtils .getString(" _id" , json))
1472+ }
1473+ if (tag != null ) {
1474+ tag._rev = JsonUtils .getString(" _rev" , json)
1475+ tag._id = JsonUtils .getString(" _id" , json)
1476+ tag.type = JsonUtils .getString(" type" , json)
1477+ tag.user = JsonUtils .getString(" user" , json)
1478+ tag.createdOn = JsonUtils .getString(" createdOn" , json)
1479+ tag.parentCode = JsonUtils .getString(" parentCode" , json)
1480+ tag.time = JsonUtils .getLong(" time" , json)
1481+ tag.teamId = JsonUtils .getString(" teamId" , json)
1482+ tag.teamType = JsonUtils .getString(" teamType" , json)
1483+ }
14441484 }
14451485 }
14461486}
0 commit comments