Skip to content

Commit

Permalink
decoupling mergin api from activeProject and rebuilding connections
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Jan 15, 2025
1 parent e0f23b2 commit 9cd5999
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 48 deletions.
31 changes: 4 additions & 27 deletions app/activeproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ ActiveProject::ActiveProject( AppSettings &appSettings
, ActiveLayer &activeLayer
, LayersProxyModel &recordingLayerPM
, LocalProjectsManager &localProjectsManager
, MerginApi *merginApi
, QObject *parent ) :

QObject( parent )
, mAppSettings( appSettings )
, mActiveLayer( activeLayer )
, mRecordingLayerPM( recordingLayerPM )
, mLocalProjectsManager( localProjectsManager )
, mMerginApi( merginApi )
, mProjectLoadingLog( "" )
{
// we used to have our own QgsProject instance, but unfortunately few pieces of qgis_core
Expand Down Expand Up @@ -76,24 +74,6 @@ ActiveProject::ActiveProject( AppSettings &appSettings
setAutosyncEnabled( mAppSettings.autosyncAllowed() );

QObject::connect( &mAppSettings, &AppSettings::autosyncAllowedChanged, this, &ActiveProject::setAutosyncEnabled );

QObject::connect(
mMerginApi,
&MerginApi::projectMetadataRoleUpdated,
this, [this]( const QString & projectFullName, const QString & role )
{
if ( projectFullName == this->projectFullName() )
{
setProjectRole( role );
}
} );

QObject::connect(
mMerginApi,
&MerginApi::authChanged,
this,
&ActiveProject::updateUserRoleInActiveProject
);
}

ActiveProject::~ActiveProject() = default;
Expand Down Expand Up @@ -204,11 +184,14 @@ bool ActiveProject::forceLoad( const QString &filePath, bool force )
CoreUtils::log( QStringLiteral( "Project load" ), QStringLiteral( "Could not find project in local projects: " ) + filePath );
}

QString role = MerginProjectMetadata::fromCachedJson( CoreUtils::getProjectMetadataPath( mLocalProject.projectDir ) ).role;
qDebug() << "ROLE 1 : " << role;
setProjectRole( role );

updateMapTheme();
updateRecordingLayers();
updateActiveLayer();
updateMapSettingsLayers();
updateUserRoleInActiveProject();

emit localProjectChanged( mLocalProject );
emit projectReloaded( mQgsProject );
Expand Down Expand Up @@ -589,9 +572,3 @@ void ActiveProject::setProjectRole( const QString &role )
emit projectRoleChanged();
}
}

void ActiveProject::updateUserRoleInActiveProject()
{
// update user's role each time a project is opened or auth changes, following #3174
mMerginApi->updateProjectMetadataRole( projectFullName() );
}
8 changes: 6 additions & 2 deletions app/activeproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class ActiveProject: public QObject
, ActiveLayer &activeLayer
, LayersProxyModel &recordingLayerPM
, LocalProjectsManager &localProjectsManager
, MerginApi *mMerginApi
, QObject *parent = nullptr );

virtual ~ActiveProject();
Expand Down Expand Up @@ -131,7 +130,12 @@ class ActiveProject: public QObject
/**
* Calls Mergin API to update current project’s role
*/
Q_INVOKABLE void updateUserRoleInActiveProject();
void updateUserRoleInActiveProject();

/**
* Update current project’s role
*/
void onProjectRoleUpdated( const QString &projectFullName, const QString &role );

signals:
void qgsProjectChanged();
Expand Down
24 changes: 23 additions & 1 deletion app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ int main( int argc, char *argv[] )
LayersProxyModel recordingLpm( &lm, LayerModelTypes::ActiveLayerSelection );

ActiveLayer al;
ActiveProject activeProject( as, al, recordingLpm, localProjectsManager, ma.get() );
ActiveProject activeProject( as, al, recordingLpm, localProjectsManager );

std::unique_ptr<VariablesManager> vm( new VariablesManager( ma.get() ) );
vm->registerInputExpressionFunctions();

Expand Down Expand Up @@ -569,6 +570,27 @@ int main( int argc, char *argv[] )
syncManager.syncProject( project, SyncOptions::Authorized, SyncOptions::Retry );
} );

QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
merginApi->reloadProjectRole( activeProject.projectFullName() );
} );

QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
if ( activeProject.isProjectLoaded() )
{
merginApi->reloadProjectRole( activeProject.projectFullName() );
}
} );

QObject::connect( ma.get(), &MerginApi::projectRoleUpdated, &activeProject, [&activeProject]( const QString & projectFullName, const QString & role )
{
if ( projectFullName == activeProject.projectFullName() )
{
activeProject.setProjectRole( role );
}
} );

QObject::connect( ma.get(), &MerginApi::notifyInfo, &lambdaContext, [&notificationModel]( const QString & message )
{
notificationModel.addInfo( message );
Expand Down
6 changes: 3 additions & 3 deletions app/test/testactiveproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void TestActiveProject::testProjectValidations()
ActiveLayer al;
LayersModel lm;
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );

QSignalSpy spyReportIssues( &activeProject, &ActiveProject::reportIssue );
QSignalSpy spyErrorsFound( &activeProject, &ActiveProject::loadingErrorFound );
Expand All @@ -66,7 +66,7 @@ void TestActiveProject::testProjectLoadFailure()
ActiveLayer al;
LayersModel lm;
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );

mApi->localProjectsManager().addLocalProject( projectdir, projectname );

Expand All @@ -88,7 +88,7 @@ void TestActiveProject::testPositionTrackingFlag()
ActiveLayer al;
LayersModel lm;
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );

// project "planes" - tracking not enabled
QString projectDir = TestUtils::testDataDir() + "/planes/";
Expand Down
2 changes: 1 addition & 1 deletion app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ void TestMerginApi::testAutosync()

MapThemesModel mtm; AppSettings as; ActiveLayer al;
LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );

mApi->localProjectsManager().addLocalProject( projectdir, projectname );

Expand Down
8 changes: 8 additions & 0 deletions core/coreutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,11 @@ QString CoreUtils::bytesToHumanSize( double bytes )
return QString::number( bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0, 'f', precision ) + " TB";
}
}

QString CoreUtils::getProjectMetadataPath( QString projectDir )
{
if ( projectDir.isEmpty() )
return QString();

return projectDir + "/.mergin/mergin.json";
}
5 changes: 5 additions & 0 deletions core/coreutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class CoreUtils
*/
static QString bytesToHumanSize( double bytes );

/**
* Returns path to project metadata file for a given project directory
*/
static QString getProjectMetadataPath( QString projectDir );

private:
static QString sLogFile;
static int CHECKSUM_CHUNK_SIZE;
Expand Down
15 changes: 5 additions & 10 deletions core/merginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3984,27 +3984,22 @@ DownloadQueueItem::DownloadQueueItem( const QString &fp, qint64 s, int v, qint64
tempFileName = CoreUtils::uuidWithoutBraces( QUuid::createUuid() );
}

void MerginApi::updateProjectMetadataRole( const QString &projectFullName )
void MerginApi::reloadProjectRole( const QString &projectFullName )
{
if ( projectFullName.isEmpty() )
{
return;
}

QString cachedRole = MerginApi::getCachedProjectRole( projectFullName );

QNetworkReply *reply = getProjectInfo( projectFullName );
if ( !reply )
{
emit projectMetadataRoleUpdated( projectFullName, cachedRole );
return;
}

reply->request().setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );
connect( reply, &QNetworkReply::finished, this, &MerginApi::updateProjectMetadataRoleReplyFinished );
connect( reply, &QNetworkReply::finished, this, &MerginApi::reloadProjectRoleReplyFinished );
}

void MerginApi::updateProjectMetadataRoleReplyFinished()
void MerginApi::reloadProjectRoleReplyFinished()
{
QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
Q_ASSERT( r );
Expand All @@ -4022,7 +4017,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()
{
if ( updateCachedProjectRole( projectFullName, role ) )
{
emit projectMetadataRoleUpdated( projectFullName, role );
emit projectRoleUpdated( projectFullName, role );
}
else
{
Expand All @@ -4032,7 +4027,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()
}
else
{
emit projectMetadataRoleUpdated( projectFullName, cachedRole );
emit projectRoleUpdated( projectFullName, cachedRole );
}

r->deleteLater();
Expand Down
8 changes: 4 additions & 4 deletions core/merginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ class MerginApi: public QObject
bool apiSupportsWorkspaces();

/**
* Updates project metadata role by fetching latest information from server.
* Reloads project metadata role by fetching latest information from server.
*/
Q_INVOKABLE void updateProjectMetadataRole( const QString &projectFullName );
Q_INVOKABLE void reloadProjectRole( const QString &projectFullName );

signals:
void apiSupportsSubscriptionsChanged();
Expand Down Expand Up @@ -655,7 +655,7 @@ class MerginApi: public QObject
void apiSupportsWorkspacesChanged();

void serverWasUpgraded();
void projectMetadataRoleUpdated( const QString &projectFullName, const QString &role );
void projectRoleUpdated( const QString &projectFullName, const QString &role );

private slots:
void listProjectsReplyFinished( QString requestId );
Expand Down Expand Up @@ -795,7 +795,7 @@ class MerginApi: public QObject

bool projectFileHasBeenUpdated( const ProjectDiff &diff );

void updateProjectMetadataRoleReplyFinished();
void reloadProjectRoleReplyFinished();

bool updateCachedProjectRole( const QString &projectFullName, const QString &newRole );

Expand Down

1 comment on commit 9cd5999

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 25.1.698511 just submitted!

Please sign in to comment.