10

有幾個問題我有,使用匕首在Android mvp上實現GoogleApiClient?

第一,因爲我讀了一些文章,我應該實現LocationListener的,ConnectionCallback,在活動OnConnectionFailedListener接口,

是不是要單獨實施這些類在不同的文件中?

like below?

public class LocationListener implements 
      com.google.android.gms.location.LocationListener { 
@Inject 
Location mLastLocation; 
@Override 
public void onLocationChanged(Location location) { 
    // Assign the new location 
    mLastLocation = location; 
    // Displaying the new location on UI 
} 
} 

在我的活動中是否正確我處理顯示mLastLocation屬性?

//Fields 
@Inject 
GoogleApiClient client; 
Location mLastLocation; 
//Fields 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client); 

秒,我應該如何爲它編寫提供者方法?,我的猜測會是這樣的,你們會推薦什麼?

//Constructor 
public LocationModule(Context context, GoogleApiClient.ConnectionCallbacks callback, GoogleApiClient.OnConnectionFailedListener listener) { 
    this.context = context; 
    this.callback = callback; 
    this.listener = listener; 
} 
@Provides 
@Singleton 
GoogleApiClient providesGoogleApi() { 
    return new GoogleApiClient.Builder(context) 
      .addOnConnectionFailedListener(listener) 
      .addConnectionCallbacks(callback) 
      .addApi(LocationServices.API) 
      .build(); 
} 

最後,我應該在哪裏辦理的Android 6臺及以上設備的權限?,它是在視圖上,還是在演示者?

我聽說View必須如此愚蠢以至於你不需要測試它,我應該如何保持這個原則?

如果任何人都可以給我一個參考,或github示例代碼,這與我的案例相匹配,那將非常棒。

回答

5

最後一件事,首先,您可以將MVP視圖層視爲純Android模塊,這意味着任何與Android操作系統(如請求權限)的通信都必須使用此層進行處理,結果會返回給演示者,接下來做。

關於分離這些類的實現,當我正在尋找一些類的代碼時,我自己喜歡分離類以獲得更清晰的可視化!我不認爲任何人都可以提出最佳做法,因爲這取決於您的模塊和實施。根據Clean Code book,在這種類型的決策情況下,您必須考慮更多關於代碼可讀性的內容。

最後,關於LocationModule,這是完全正確的,但如果我是你的鞋子我甚至會在一個較高的水平組件(例如ApplicationComponent)問來龍去脈和LocationModule構造函數中刪除。

//Constructor 
public LocationModule(GoogleApiClient.ConnectionCallbacks callback, GoogleApiClient.OnConnectionFailedListener listener) { 
    this.callback = callback; 
    this.listener = listener; 
} 

@Provides 
@Singleton 
GoogleApiClient providesGoogleApi(Context context) { 
    return new GoogleApiClient.Builder(context) 
      .addOnConnectionFailedListener(listener) 
      .addConnectionCallbacks(callback) 
      .addApi(LocationServices.API) 
      .build(); 
} 

上下文可以使用它的相關提供者在更高的模塊中提供。

下面是一個簡單的回購可能真正幫助你在這方面:

https://github.com/mmirhoseini/fyber_mobile_offers